1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Dockerの基本操作(よく使うコマンド書いてみた)

Last updated at Posted at 2018-07-29

##はじめに
CentOSにDockerをインストールしたので色々と操作してみました。
よく使うコマンド(忘れるので)ここに備忘録として残します。

##コマンド(主にコンテナ作成系)
1.OSイメージ取得
sudo docker search [OS名] | more

$ sudo docker search ubuntu | more
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   8067                [OK]                
~~~以下省略~~~

2.イメージの取得

$ sudo docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
c64513b74145: Pull complete 
01b8b12bad90: Pull complete 
c5d85cf7a05f: Pull complete 
b6b268720157: Pull complete 
e12192999ff1: Pull complete 
Digest: sha256:3f119dc0737f57f704ebecac8a6d8477b0f6ca1ca0332c7ee1395ed2c6a82be7
Status: Downloaded newer image for ubuntu:latest

3.ダウンロード(またはダウンロード済み)のイメージを確認する。

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              735f80812f90        2 days ago          83.5MB
centos              latest              49f7960eb7e4        7 weeks ago         200MB

4.イメージの詳細を確認する。
$ sudo docker inspect [REPOSITORY:TAG]
$ sudo docker inspect [IMAGE ID]

$ sudo docker inspect ubuntu:latest
$ sudo docker inspect 735f80812f90

5.イメージの削除
$ sudo docker rmi [REPOSITORY:TAG]
$ sudo docker rmi [IMAGE ID]

$ sudo docker rmi ubuntu:latest
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              49f7960eb7e4        7 weeks ago         200MB

6.コンテナの実行。

$ sudo docker run ubuntu:latest

7.作成したコンテナの確認
$ sudo docker ps -a

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
b955352cd55e        ubuntu:latest       "/bin/bash"         12 seconds ago       Exited (0) 11 seconds ago                           elegant_curie
e2517b4a55a6        centos:latest       "/bin/bash"         About a minute ago   Exited (0) About a minute ago                       romantic_jones

8.現在処理実行中のコンテナ確認

$ sudo docker ps -a

9.作成したコンテナの削除
$ sudo docker rm [イメージID]

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
b955352cd55e        ubuntu:latest       "/bin/bash"         3 minutes ago       Exited (0) 3 minutes ago                       elegant_curie
e2517b4a55a6        centos:latest       "/bin/bash"         5 minutes ago       Exited (0) 5 minutes ago                       romantic_jones

$ sudo docker rm e2517b4a55a6
e2517b4a55a6

10.新規にコンテナを作成してSSH接続する
$ sudo docker run -i -t centos:latest /bin/bash

[root@docker ~]# sudo docker run -i -t centos /bin/bash
[root@332d2f91b41d /]#

11.コンテナのイメージを作成する。
$ sudo docker commit [CONTAINER ID] [作成するイメージ名]

[root@docker ~]# sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
f8046a85fbcc        centos              "/bin/bash"         23 seconds ago      Exited (0) 8 seconds ago                         inspiring_neumann

[root@docker ~]# sudo docker commit f8046a85fbcc test/centos
sha256:ee7f7a6dab17ad5590a20cd7585da50e8712562669c15731a9f2fb0078bcd478

[root@docker ~]# sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/centos         latest              ee7f7a6dab17        7 seconds ago       200MB
ubuntu              latest              735f80812f90        2 days ago          83.5MB
centos              latest              49f7960eb7e4        7 weeks ago         200M

12.作成したイメージから新しいコンテナを作成する。
$ sudo docker run -i -t [作成したOSイメージ名]

[root@docker ~]# sudo docker run -i -t test/centos

※コンテナからexitする。
[root@59833df25ff3 /]# exit
exit

※状態確認
[root@docker ~]# sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
59833df25ff3        test/centos         "/bin/bash"         16 seconds ago      Exited (0) 11 seconds ago                        tender_noyce
f8046a85fbcc        centos              "/bin/bash"         4 minutes ago       Exited (0) 4 minutes ago                         inspiring_neumann

以上。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?