6
4

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 2015-10-26

osx上でdockerを使うたびに毎度調べてるで備忘録です。

準備

docker Quickstart Terminal を起動する

Docker image 取得

docker hub から取得したいimageの[REPOSITORY:TAG]を調べて

docker pull [REPOSIToRY:TAG]

$ docker pull centos:latest
$ docker pull centos:centos6

取得済み(ローカル保存の)imageの確認

docker images

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos6             1a895dd3954a        12 days ago         190.6 MB
centos              latest              e9fa5d3a0d0e        12 days ago         172.3 MB

コンテナ 起動

docker run -it [REPOSITORY:TAG] /bin/bash

$ docker run -it centos:latest /bin/bash
[root@1f203f4c08d5 /]#

コンテナ から終了せずに抜ける

ctrl-p ctrl-q

起動中のコンテナ確認

docker ps

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
1f203f4c08d5        centos:latest       "/bin/bash"         About a minute ago   Up About a minute                       tender_brattain

起動中のコンテナに再接続(Attach)

docker attach [CONTAINER ID]

$ docker attach 1f203f4c08d5
# ここでリターンが必要
[root@1f203f4c08d5 /]#

コンテナ終了

普通にexit

[root@1f203f4c08d5 /]# exit
exit

終了したコンテナの確認

docker ps -a

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1f203f4c08d5        centos:latest       "/bin/bash"         4 minutes ago       Exited (0) 52 seconds ago                       tender_brattain

コンテナの削除

docker rm [CONTAINER ID]

$ docker rm 1f203f4c08d5
1f203f4c08d5

イメージの削除

docker rmi [REPOSITORY:TAG]

$ docker rmi centos:latest

終了したコンテナの再起動

docker start -a [CONTAINER ID]

$ docker start -a 2ce874ed5e0d
または
$ docker start 2ce874ed5e0d
$ docker attach 2ce874ed5e0d

コンテナのイメージ化

docker commit [CONTAINER ID] [REPOSITORY:TAG]

$ docker ps  -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
2ce874ed5e0d        centos:centos6      "/bin/bash"         4 minutes ago       Exited (0) 37 seconds ago                       stoic_wilson
$ docker commit 2ce874ed5e0d hiyuzawa:httpd_installed
f965e9b961577eb1bb7d788037dd81c4362897ea799617a2e878e113ac40e20d
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hiyuzawa            httpd_installed     f965e9b96157        10 seconds ago      287.8 MB
centos              centos6             1a895dd3954a        12 days ago         190.6 MB
centos              latest              e9fa5d3a0d0e        12 days ago         172.3 MB

ホストOSのPortマッピング付きのrun と接続方法

docker run -it -p [HostPort]:[GuestPort] [REPOSITORY:TAG] /bin/bash

$ docker run -it -p 8080:80 hiyuzawa:httpd_installed /bin/bash
[root@cfcf5b0322c0 /]# service httpd start
Starting httpd:       [  OK  ]
[root@cfcf5b0322c0 /]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.6  0.1  13028  3148 ?        Ss   15:00   0:00 /bin/bash
root        28  0.0  0.3 175336  6260 ?        Ss   15:00   0:00 /usr/sbin/httpd
apache      30  0.0  0.1 175336  3688 ?        S    15:00   0:00 /usr/sbin/httpd
apache      31  0.0  0.1 175336  3688 ?        S    15:00   0:00 /usr/sbin/httpd
apache      32  0.0  0.1 175336  3688 ?        S    15:00   0:00 /usr/sbin/httpd
apache      33  0.0  0.1 175336  3688 ?        S    15:00   0:00 /usr/sbin/httpd
apache      34  0.0  0.1 175336  3688 ?        S    15:00   0:00 /usr/sbin/httpd
apache      35  0.0  0.1 175336  3688 ?        S    15:00   0:00 /usr/sbin/httpd
apache      36  0.0  0.1 175336  3688 ?        S    15:00   0:00 /usr/sbin/httpd
apache      37  0.0  0.1 175336  3688 ?        S    15:00   0:00 /usr/sbin/httpd
root        38  0.0  0.1  14912  2236 ?        R+   15:00   0:00 ps aux

mac os上で localhost:8080としても繋がらない。
なぜなら、ここでいうホストosはdocker-machineでありosxではないから

$ docker-machine ip default
192.168.99.100

と docker-machine のIPを確認して

192.168.99.100:8080 にアクセスすればコンテナへ接続できる。

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?