Ubuntu
docker
dockerfile
DockerHub
0

よく使うdockerコマンド

Dockerコマンドによる状態遷移

素材.001.jpeg

コマンド使用例

  • docker pull [オプション] イメージ名[:タグ名]
$ sudo docker pull ubuntu
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              16508e5c265d        10 days ago         84.1MB
  • docker build -t [生成するイメージ名]:[タグ名] [Dockerfileの場所]
$ sudo docker build -t example  ./
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
example             latest              62f7ae565c29        About a minute ago   221MB
ubuntu              latest               16508e5c265d        10 days ago          84.1MB
#Dockerfile
FROM ubuntu:18.04

RUN apt-get update
RUN apt-get install -y apache2

EXPOSE 80
CMD ["apachectl", "-D", "FOREGROUND"]
  • docker run [オプション] イメージ名[:タグ名] [引数]
$ sudo docker run -d -p 8000:80 example
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
8ea65a33328e        example             "apachectl -D FOREGR…"   8 minutes ago       Up 8 minutes        0.0.0.0:8000->80/tcp   competent_pare
$ curl http://localhost:8000
・・・・apacheの画面・・・・
  • docker stop [オプション] コンテナ識別子 [コンテナ識別子]
$ sudo docker stop 8ea65a33328e
8ea65a33328e
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
8ea65a33328e        example             "apachectl -D FOREGR…"   40 minutes ago      Exited (137) 21 seconds ago                        frosty_cray
  • docker start [オプション] コンテナ識別子 [コンテナ識別子]
$ sudo docker start 8ea65a33328e
8ea65a33328e
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
8ea65a33328e        example             "apachectl -D FOREGR…"   11 minutes ago      Up 29 seconds       0.0.0.0:8000->80/tcp   frosty_cray
  • docker rm [オプション] コンテナ識別子 [コンテナ識別子]
$ sudo docker rm 8ea65a33328e
8ea65a33328e
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
  • docker push イメージ名[:タグ名]
$ sudo docker tag 62f7ae565c29 7vj1/example:latest
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
7vj1/example        latest              62f7ae565c29        2 hours ago         221MB
example             latest              62f7ae565c29        2 hours ago         221MB
ubuntu              latest              16508e5c265d        10 days ago         84.1MB
$ sudo docker push 7vj1/example:latest

dockerhub.png

  • docker rmi [オプション] イメージ名 [イメージ名]
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
7vj1/example        latest              62f7ae565c29        2 hours ago         221MB
example             latest              62f7ae565c29        2 hours ago         221MB
ubuntu              latest              16508e5c265d        10 days ago         84.1MB
$ sudo docker rmi example
Untagged: example:latest
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
7vj1/example        latest              62f7ae565c29        2 hours ago         221MB
ubuntu              latest              16508e5c265d        10 days ago         84.1MB
  • docker commit [オプション] コンテナ識別子 [イメージ名[:タグ名]]
$ sudo docker commit d1711394b08b example-commit
sha256:122560d92b9be311629a773e182a66ace818eafb76993866642470540d6ade4b
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
example-commit      latest              122560d92b9b        6 seconds ago       221MB
7vj1/example        latest              62f7ae565c29        3 hours ago         221MB
ubuntu              latest              16508e5c265d        10 days ago         84.1MB
  • docker exec [オプション] コンテナ識別子 実行するコマンド [引数]
$ sudo docker exec -it d1711394b08b /bin/bash
root@d1711394b08b:/# 

参考

http://docs.docker.jp/engine/reference/commandline/index.html
https://qiita.com/nimusukeroku/items/72bc48a8569a954c7aa2
https://qiita.com/TakahiRoyte/items/39dcf18da0ef2d1fe8b0