Dockerでよく使うコマンド集

# はじめに
この記事はコンテナ勉強用として試したことまとめたものです。
（ほぼ自分が使用するもの...随時追加予定）

# Dockerコマンド
* イメージのダウンロードコマンド

```
docker image pull [オプション] イメージ名 [:タグ名]
```

```shell
# docker image pull centos:7
7: Pulling from library/centos
8ba884070f61: Pull complete
Digest: sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
#
```

* イメージの一覧表示コマンド

```
docker image ls [オプション] [リポジトリ名]
```

```shell
# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              5a3221f0137b        41 hours ago        126MB
centos              7                   9f38484d220f        5 months ago        202MB
hello-world         latest              fce289e99eb9        7 months ago        1.84kB
#
```

* イメージの詳細情報

```
docker image inspect [イメージ名]
```

```shell
# docker image inspect centos:7
[
    {
        "Id": "sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1",
        "RepoTags": [
            "centos:7"
        ],
        "RepoDigests": [
            "centos@sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-03-14T21:19:53.361167852Z",
        "Container": "958baf5225f586da9c70a21e911a0a875402dd22d83133d78b3b3aa6130e7892",
        "ContainerConfig": {
            "Hostname": "958baf5225f5",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:294e8d8145287e70f07328cc09d840fad8980b801223321b983442f097aff0d8",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20190305",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "18.06.1-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:294e8d8145287e70f07328cc09d840fad8980b801223321b983442f097aff0d8",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20190305",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 201775492,
        "VirtualSize": 201775492,
        "GraphDriver": {
            "Data": {
                "DeviceId": "10",
                "DeviceName": "docker-253:0-50575843-a8cd5245187bae41a0457a7100bac56e3468e9a74d4eeab24091a5b772a899ae",
                "DeviceSize": "10737418240"
            },
            "Name": "devicemapper"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
#
```

* Dockerfileからイメージを作成

```
docker build -t [作成するイメージ名]:[タグ名] [Dockerfileの場所]
```

```shell
# docker build -t test-docker:latest ./
Sending build context to Docker daemon  3.584kB
Step 1/4 : FROM centos:centos7
 ---> 67fa590cfc1c
Step 2/4 : RUN ["yum",  "-y", "install", "httpd"]
 ---> Using cache
 ---> 37f12e15f83e
Step 3/4 : COPY index.html /var/www/html
 ---> Using cache
 ---> fbafd7674218
Step 4/4 : CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
 ---> Using cache
 ---> d59edf5b5938
Successfully built d59edf5b5938
Successfully tagged test-docker:latest
#
```

* Dockerコンテナ起動

```
docker run [オプション] [イメージ名:タグ名] [引数]
```

```shell
# docker run -it test-ubuntu-docker
root@0e0479ff474v:/#
```

* イメージの削除

```
docker rmi [オプション] [イメージID]
```

```shell
# docker rmi -f golang
Untagged: golang:latest
Untagged: golang@sha256:1b1246987ecb4939d9bd20f74eab25e8e2ad819aceea2d6b59869c82f85ef1de
Deleted: sha256:52b59e9ead8e18fafb497532ed44ee8ee4833082b9874abbf0bca61a713a4e01
Deleted: sha256:037466601c73650bce9e801284525d2cb01076d83152e3f52b58a4f1db93cd92
Deleted: sha256:bfe10948239b9bb3c686f973f558bf3b13e5a4e5836a6e2ca10160ce3cec52f0
Deleted: sha256:1daff011bfed534d3a97f224a10f0345bb856f3ee68c885515f0840875ff1502
Deleted: sha256:aeed80cc2bd89729d4743c112894e46bf3e454e23035fca42d8c2317d76e6b59
Deleted: sha256:4d46c2665dc95257b2142eff58b4ca0e01c39238ce5722c573b351a86f6db59d
Deleted: sha256:5bfb8954c951a40ba691ad40d8c6403564ab3ec406bb33693c15a145a4e6cede
Deleted: sha256:78c1b9419976227e05be9d243b7fa583bea44a5258e52018b2af4cdfe23d148d
#
```