11
11

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 | 基本操作コマンドについて #docker

Last updated at Posted at 2014-11-25

Docker | 基本操作コマンドについて #docker

概要

Docker の基本操作コマンドについて。

※何を持って基本操作コマンドとするか、は適当

Commands

run

  • Basic Format
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • Ubuntu 1404 のイメージを指定して、コンテナを起動し、 perl の Hello world を呼び出す
$ sudo docker run ubuntu:14.04 /usr/bin/perl -e 'print "Hello world\n"'
Hello world
  • Ubuntu 1404 のイメージを指定して、コンテナを対話モードで起動する。
$ sudo docker run -i -t ubuntu:14.04
root@c7a3fa22c343:/# echo hoge
hoge
root@c7a3fa22c343:/# perl -e 'print "hoge\n"'
hoge
root@c7a3fa22c343:/# exit
  • tomcat:8.0 のイメージを指定して、ポート指定+デーモン起動する。
$ sudo docker run -d -p 8888:8080 tomcat:8.0
$ curl http://localhost:8888 | grep Congratulations
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0                    <h2>If you're seeing this, you've successfully installed Tomcat. Congratulations!</h2>
100 11230    0 11230    0     0   676k      0 --:--:-- --:--:-- --:--:--  685k

ps

コンテナの一覧を確認する

  • 起動中のコンテナの一覧を確認する
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
0d8e6ab4aafb        tomcat:8.0          "catalina.sh run"   6 minutes ago       Up 6 minutes        0.0.0.0:8888->8080/tcp   prickly_kowalevski

$ sudo docker stop prickly_kowalevski
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
  • すべてのコンテナの一覧を確認する
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
111e16dc2e22        ubuntu:14.04        "\"/usr/bin/perl -e    2 minutes ago       Exited (0) 3 seconds ago                       sick_poincare
$ sudo docker stop sick_poincare
sick_poincare
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
111e16dc2e22        ubuntu:14.04        "\"/usr/bin/perl -e    2 minutes ago       Exited (0) 12 seconds ago                       sick_poincare

logs

コンテナのログを出力する

# tomcat8 のコンテナを起動
$ sudo docker run -d -p 8888:8080 tomcat:8.0
# コンテナ一覧を確認
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
c0d27e685d09        tomcat:8.0          "catalina.sh run"   4 minutes ago       Up 4 minutes        0.0.0.0:8888->8080/tcp   romantic_goodall
# コンテナの NAME を指定してログを確認
$ sudo docker logs romantic_goodall
# 略
25-Nov-2014 06:51:07.182 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1663 ms

stop

起動中のコンテナを停止します。

  • Name を指定して 1 コンテナ停止
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
c0d27e685d09        tomcat:8.0          "catalina.sh run"   4 minutes ago       Up 4 minutes        0.0.0.0:8888->8080/tcp   romantic_goodall
$ sudo docker stop romantic_goodall
romantic_goodall
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  • 全コンテナ停止
$ sudo docker run -d -p 8888:8080 tomcat:8.0
$ sudo docker run -d -p 8889:8080 tomcat:8.0
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                        PORTS                    NAMES
ebd368bc17d9        tomcat:8.0          "catalina.sh run"   7 seconds ago        Up 6 seconds                  0.0.0.0:8889->8080/tcp   furious_turing
68108e43c1f1        tomcat:8.0          "catalina.sh run"   About a minute ago   Exited (143) 33 seconds ago                            backstabbing_feynman
$ sudo docker stop `sudo docker ps -a -q`
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                            PORTS               NAMES
ebd368bc17d9        tomcat:8.0          "catalina.sh run"   About a minute ago   Exited (143) 1 seconds ago                            furious_turing
68108e43c1f1        tomcat:8.0          "catalina.sh run"   2 minutes ago        Exited (143) About a minute ago                       backstabbing_feynman

rm

  • Name を指定して 1 コンテナ削除
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
fc588913e43e        tomcat:8.0          "catalina.sh run"   2 minutes ago       Exited (143) 2 seconds ago                       prickly_darwin
$ sudo docker rm prickly_darwin
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  • 全コンテナ削除(起動中のコンテナがある場合はエラー)
$ sudo docker run -d -p 8888:8080 tomcat:8.0
$ sudo docker run -d -p 8889:8080 tomcat:8.0
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                        PORTS                    NAMES
ebd368bc17d9        tomcat:8.0          "catalina.sh run"   7 seconds ago        Up 6 seconds                  0.0.0.0:8889->8080/tcp   furious_turing
68108e43c1f1        tomcat:8.0          "catalina.sh run"   About a minute ago   Exited (143) 33 seconds ago                            backstabbing_feynman
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                            PORTS               NAMES
ebd368bc17d9        tomcat:8.0          "catalina.sh run"   About a minute ago   Exited (143) 1 seconds ago                            furious_turing
68108e43c1f1        tomcat:8.0          "catalina.sh run"   2 minutes ago        Exited (143) About a minute ago                       backstabbing_feynman
$ sudo docker rm `sudo docker ps -a -q`
ebd368bc17d9
68108e43c1f1
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
11
11
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
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?