Docker基本コマンドまとめ

はじめに

Dockerの基本的な使い方と簡単な具体例をまとめました。

それなりに数があるので、目次から探すのがいいと思います。

イメージ

Dockerfileからイメージをビルド

docker image build
docker image build: Build an image from a Dockerfile
docker image build [OPTIONS] PATH | URL | -

具体例

docker image build -t centos:hoge2 .

イメージ一覧の取得

docker image ls
docker image ls: List images
docker image ls [OPTIONS] [REPOSITORY[:TAG]]

具体例

docker image ls

未使用のイメージを削除

docker image prune
docker image prune: Remove all dangling images. If `-a` is specified, will also remove all images not referenced by any container.
docker image prune [OPTIONS]

具体例

docker image prune -f

Docker Hunやレジストリからイメージを取得

docker image pull
docker image pull: Pull an image or a repository from a registry
docker image pull [OPTIONS] NAME[:TAG|@DIGEST]

具体例

docker image pull centos:7

Docker Hubやレジストリにイメージをアップ

docker image push
docker image push: Push an image or a repository to a registry
docker image push [OPTIONS] NAME[:TAG]

具体例

docker image push localhost:5000/ubuntu:18.04

イメージを削除

docker image rm
docker image rm: Remove one or more images
docker image rm [OPTIONS] IMAGE [IMAGE...]

具体例

docker image rm centos:7

イメージにタグを付ける

docker image tag
docker image tag: Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG])

具体例

docker image tag centos:7 localhost:5000/centos:7

コンテナ

実行中のコンテナに入る

docker container attach
docker container attach: Attach local standard input, output, and error streams to a running container
docker container attach [OPTIONS] CONTAINER

具体例

docker container attach centos7

コンテナからイメージを作成

docker container commit
docker container commit: Create a new image from a container's changes
docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

具体例

docker container commit centos7 example/centos

ホストからコンテナにファイルをコピー

docker container cp
docker container cp: Copy files/folders between a container and the local filesystem Use '-' as the source to read a tar archive from stdin and extract it to a ...
docker container cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-

具体例

docker container cp sample.txt centos7:/home/user

コンテナを生成

docker container create
docker container create: Create a new container
docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]

具体例

docker container create -it --name centos7 centos:7

実行中のコンテナでコマンド実行

docker container exec
docker container exec: Run a command in a running container
docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]

具体例

docker container exec -it centos7 pwd

コンテナのログを取得

docker container logs
docker container logs: Fetch the logs of a container
docker container logs [OPTIONS] CONTAINER

具体例

docker container logs --details -f centos7

コンテナ一覧の取得

docker container ls
docker container ls: List containers
docker container ls [OPTIONS]

具体例

docker container ls -a

停止しているコンテナを削除

docker container prune
docker container prune: Removes all stopped containers.
docker container prune [OPTIONS]

具体例

docker container prune -f

コンテナの名前を変更

docker container rename
docker container rename: Rename a container
docker container rename CONTAINER NEW_NAME

具体例

docker container rename centos7 centos7.1

コンテナを再起動

docker container restart
docker container restart: Restart one or more containers
docker container restart [OPTIONS] CONTAINER [CONTAINER...]

具体例

docker container restart centos7

コンテナを削除

docker container rm
docker container rm: Remove one or more containers
docker container rm [OPTIONS] CONTAINER [CONTAINER...]

具体例

docker container rm -f centos7

コンテナの作成と実行

docker container run
docker container run: Run a command in a new container
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

具体例

docker container run -it -d --name centos7 centos:7

停止したコンテナを実行

docker container start
docker container start: Start one or more stopped containers
docker container start [OPTIONS] CONTAINER [CONTAINER...]

具体例

docker container start centos7

コンテナのリソース使用率を表示

docker container stats
docker container stats: Display a live stream of container(s) resource usage statistics
docker container stats [OPTIONS] [CONTAINER...]

具体例

docker container stats

コンテナを停止

docker container stop
docker container stop: Stop one or more running containers
docker container stop [OPTIONS] CONTAINER [CONTAINER...]

具体例

docker container stop centos7

ネットワーク

ネットワークに接続

docker network connect
docker network connect: Connects a container to a network. You can connect a container by name or by ID. Once connected, the container can communicate with othe...
docker network connect [OPTIONS] NETWORK CONTAINER

具体例

docker network connect net01 centos7

ネットワークを作成

docker network create
docker network create: Creates a new network. The `DRIVER` accepts `bridge` or `overlay` which are the built-in network drivers. If you have installed a third p...
docker network create [OPTIONS] NETWORK

具体例

docker network create --subnet 10.0.0.0/24 --attachable net01

ネットワークから接続を切る

docker network disconnect
docker network disconnect: Disconnects a container from a network. The container must be running to disconnect it from the network.
docker network disconnect [OPTIONS] NETWORK CONTAINER

具体例

docker network disconnect net01 centos7

ネットワーク一覧の取得

docker network ls
docker network ls: Lists all the networks the Engine `daemon` knows about. This includes the networks that span across multiple hosts in a cluster.
docker network ls [OPTIONS]

具体例

docker network ls

未使用のネットワークを削除

docker network prune
docker network prune: Remove all unused networks. Unused networks are those which are not referenced by any containers.
docker network prune [OPTIONS]

具体例

docker network prune -f

ネットワークの削除

docker network rm
docker network rm: Removes one or more networks by name or identifier. To remove a network, you must first disconnect any containers connected to it.
docker network rm NETWORK [NETWORK...]

具体例

docker network rm net01

ボリューム

ボリュームの作成

docker volume create
docker volume create: Creates a new volume that containers can consume and store data in. If a name is not specified, Docker generates a random name.
docker volume create [OPTIONS] [VOLUME]

具体例

docker volume create vol01

ボリューム一覧の取得

docker volume ls
docker volume ls: List all the volumes known to Docker. You can filter using the `-f` or `--filter` flag. Refer to the (#filtering) section for more information...
docker volume ls [OPTIONS]

具体例

docker volume ls

未使用のボリュームを削除

docker volume prune
docker volume prune: Remove all unused local volumes. Unused local volumes are those which are not referenced by any containers
docker volume prune [OPTIONS]

具体例

docker volume prune -f

ボリュームの削除

docker volume rm
docker volume rm: Remove one or more volumes. You cannot remove a volume that is in use by a container.
docker volume rm [OPTIONS] VOLUME [VOLUME...]

具体例

docker volume rm vol01

システム

停止しているコンテナ・未使用のイメージ/ボリューム/ネットワークの削除

docker system prune
docker system prune: Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
docker system prune [OPTIONS]

具体例

docker system prune -f

参考

タイトルとURLをコピーしました