Help us understand the problem. What is going on with this article?

dockerコマンドのメモ

概要

docker コマンドの使い方を時々忘れるので、メモ的に置いた

imageの検索と取得

  • imageの検索
    • $ docker search <search_word>
  • imageの取得
    • $ docker pull <image_name>
  • 取得したimageの一覧
    • $ docker images

$ docker search httpd
NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
httpd                                The Apache HTTP Server Project                  2646                [OK]                
centos/httpd                                                                         25                                      [OK]
centos/httpd-24-centos7              Platform for running Apache httpd 2.4 or bui…   23                                      
...
$
$ docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
b8f262c62ec6: Already exists 
2c31b9311798: Pull complete 
7422a3cdf4e3: Pull complete 
1919d4fbf9e1: Pull complete 
60812fa1ab4c: Pull complete 
Digest: sha256:39d7d9a3ab93c0ad68ee7ea237722ed1b0016ff6974d80581022a53ec1e58797
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
$ 
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               latest              19459a872194        5 days ago          154MB
$ 

containerの生成と削除

コンテナの生成と起動、確認

  • $ docker run -d -p <host_port>:<container_port> -v <host_dir>:<container_dir> --name <container_name> <image> <exec_cmd>
    • -d
      • バックグラウンド実行
    • -p <host_port>:<container_port>
      • ホストのポートへのアクセスを、コンテナのポートに転送する
    • -v <hsot_dir>:<container_dir>
      • ホストのディレクトリを、コンテナのディレクトリとしてマウントする
    • --name <container_name> <image> <exec_cmd>
      • コンテナ名を付けて、イメージからコンテナを生成する。
      • コンテナ生成後に実行するコマンドを記載する(任意)

docker runすると、イメージからコンテナが作成され、コンテナが起動する

確認は docker ps を利用する

  • docker ps で、稼働中のコンテナを表示する
  • docker ps -a で、停止中のコンテナも表示する

ホストの/tmp/logdir を コンテナの/usr/local/apahce2/logs としてマウントし、ホストが8000ポートで受けたアクセスを コンテナの80ポートへ転送する、webserverと言う名のコンテナを作る。

$ mkdir /tmp/logdir
$ docker run -d -p 8000:80 -v /tmp/logdir:/usr/local/apache2/logs --name webserver httpd
f04d594c3020f472ba3f113e27710c10782f692a43c30386091c6afea7e65c2e
$ docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
f04d594c3020        httpd               "httpd-foreground"   8 seconds ago       Up 6 seconds        0.0.0.0:8000->80/tcp   webserver
$ 
$ telnet localhost 8000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /
<html><body><h1>It works!</h1></body></html>
Connection closed by foreign host.
$ 

containerの稼働と停止

  • コンテナの稼働
    • docker start <container_name>
  • コンテナの停止
    • docker stop <container_name>

$ docker stop webserver
webserver
$
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                     PORTS               NAMES
f04d594c3020        httpd               "httpd-foreground"   6 minutes ago       Exited (0) 4 seconds ago                       webserver
$
$ docker start webserver
webserver
$
$ docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
f04d594c3020        httpd               "httpd-foreground"   7 minutes ago       Up 3 seconds        0.0.0.0:8000->80/tcp   webserver
$

containerに対する操作

コンテナ内でコマンドを実行する

  • docker exec -it <container_name> <command>
    • -it はコマンドの入出力を手元のターミナルに出力する
    • <command> は実行するコマンド。/bin/bashなどでシェルも使える。
  • docker cp <source> <destination>
    • ファイルは、<container_name>:/var/... のように指定可能

$ docker exec -it webserver ls /
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
$
$ docker exec -it webserver /bin/bash
root@f04d594c3020:/usr/local/apache2# hostname
f04d594c3020
root@f04d594c3020:/usr/local/apache2# exit
exit
$ 
$ docker cp webserver:/usr/local/apache2/conf/httpd.conf ./
$
$ ls
httpd.conf
$ 

imageの作成とDockerehubへのアップロード

  • イメージの作成
    • docker commit <container_name> <image_name>
      • イメージ名は、<DockerHubユーザ名>/<イメージ名>:[タグ名]
  • イメージのアップロード
    • docker login で認証しておく
    • docker push <image_name>
  • コンテナの削除
    • docker rm <container_name>
  • イメージの削除
    • docker rmi <image_name>

イメージを削除する際には、利用しているコンテナを削除している必要がある。

$ docker commit webserver webimage:v0.1
sha256:20a35fc638f1cd084b5e8cd2378528013e0faade6bb2451abcd833f637455ac4
$
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
webimage            v0.1                20a35fc638f1        9 seconds ago       154MB
httpd               latest              19459a872194        5 days ago          154MB
$ 
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: YOUR-DOCKERHUB-NAME
Password: YOUR-DOCKERHUB-PASSWD
WARNING! Your password will be stored unencrypted in /home/YOURACCOUNT/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
$
$ docker push webimage
The push refers to repository [docker.io/library/webimage]
3af5ced8f83b: Preparing 
eb9b7fda09a9: Preparing 
34bee37d8bba: Preparing 
7051e05a2b9a: Preparing 
64c4da2050e7: Preparing 
2db44bce66cd: Waiting 
denied: requested access to the resource is denied
$ 
$ docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
f04d594c3020        httpd               "httpd-foreground"   29 minutes ago      Up 21 minutes       0.0.0.0:8000->80/tcp   webserver
$ docker stop webserver
webserver
$
$ docker rmi httpd
Error response from daemon: conflict: unable to remove repository reference "httpd" (must force) - container f04d594c3020 is using its referenced image 19459a872194
$
$ docker rm webserver
webserver
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$ docker rmi httpd
Untagged: httpd:latest
Untagged: httpd@sha256:39d7d9a3ab93c0ad68ee7ea237722ed1b0016ff6974d80581022a53ec1e58797
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
webimage            v0.1                20a35fc638f1        11 minutes ago      154MB
$ docker rmi webimage
Error: No such image: webimage
$ docker rmi webimage:v0.1
Untagged: webimage:v0.1
Deleted: sha256:20a35fc638f1cd084b5e8cd2378528013e0faade6bb2451abcd833f637455ac4
Deleted: sha256:c3f1e405036e5e8ce7ea84d7978f69067e1c177bda0a15b06fab998b65603bfc
Deleted: sha256:19459a87219415cc5751fb48e9aa37ea580b98eed1fe784e76c4bc3c9b3b0741
Deleted: sha256:ebefc32f49862ee9f4f672b338f29aefe056971bcc88912ed87968f5f75b0ae7
Deleted: sha256:fe3c18ca62ea3f45b69ad643869c6fd3ab567a5ad8207adf8d071d54c9152597
Deleted: sha256:62d97f753807d11f7ab72f8ae1911ff22617c9134e3edec13bf909493edb025c
Deleted: sha256:c4e27a02760c0a8711ac5e9f7e0fc7cffeb38b44b4041d0c6ded5087586727a8
Deleted: sha256:2db44bce66cde56fca25aeeb7d09dc924b748e3adfe58c9cc3eb2bd2f68a1b68
$

以上。

appendix.

イメージの取得、起動、破棄の流れ

$ docker search httpd
$ docker pull httpd
$ docker run -d -p 8000:80 -v /opt/logs:/usr/local/apache2/logs --name webserver httpd
$ docker stop webserver
$ docker start webserver
$ docker exec -it /bin/bash
root@ID# exit
$ docker stop webserver
$ docker commit webserver dockerUser/webimage:v0.0
$ docker push dockerUser/webimage:v0.0
$ docker rm webserver
$ docker rmi dockerUser/webimage:v0.0
$ docker rmi httpd
$ docker ps -a
$ docker images
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away