--- lang: ja-jp breaks: true --- # Dockerの操作 Utuntu 2021-05-04 ## dockerコマンドの利用 ##### docker ```shell= $ docker Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/home/kawahara/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/home/kawahara/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/home/kawahara/.docker/cert.pem") --tlskey string Path to TLS key file (default "/home/kawahara/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: app* Docker App (Docker Inc., v0.9.1-beta3) builder Manage builds buildx* Build with BuildKit (Docker Inc., v0.5.1-docker) config Manage Docker configs container Manage containers context Manage contexts image Manage images manifest Manage Docker image manifests and manifest lists network Manage networks node Manage Swarm nodes plugin Manage plugins scan* Docker Scan (Docker Inc., v0.7.0) secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. To get more help with docker, check out our guides at https://docs.docker.com/go/guides/ ``` ##### docker info ```shell= $ docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.5.1-docker) scan: Docker Scan (Docker Inc., v0.7.0) Server: ERROR: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/info: dial unix /var/run/docker.sock: connect: permission denied errors pretty printing info ``` ## dockerイメージの操作 ### Docker Hubからイメージにアクセスしダウンロードできるかどうかを確認 ##### docker run hello-world ```shell= $ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world b8dfde127a29: Pull complete Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ $ ``` ## docker イメージの検索 ```shell= $ docker search ubuntu NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 11944 [OK] dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 504 [OK] websphere-liberty WebSphere Liberty multi-architecture images … 267 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 250 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 234 [OK] ubuntu-upstart Upstart is an event-based replacement for th… 110 [OK] neurodebian NeuroDebian provides neuroscience research s… 81 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK] ubuntu-debootstrap debootstrap --variant=minbase --components=m… 44 [OK] open-liberty Open Liberty multi-architecture images based… 42 [OK] i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 24 nuagebec/ubuntu Simple always updated Ubuntu docker images w… 24 [OK] 1and1internet/ubuntu-16-apache-php-5.6 ubuntu-16-apache-php-5.6 14 [OK] 1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 11 [OK] 1and1internet/ubuntu-16-nginx-php-5.6 ubuntu-16-nginx-php-5.6 8 [OK] 1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 8 [OK] 1and1internet/ubuntu-16-apache-php-7.1 ubuntu-16-apache-php-7.1 6 [OK] 1and1internet/ubuntu-16-nginx-php-7.0 ubuntu-16-nginx-php-7.0 4 [OK] pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 4 pivotaldata/ubuntu16.04-build Ubuntu 16.04 image for GPDB compilation 2 1and1internet/ubuntu-16-php-7.1 ubuntu-16-php-7.1 1 [OK] pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 1 1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK] smartentry/ubuntu ubuntu with smartentry 1 [OK] $ ``` ## 公式のubuntu イメージをダウンロード ##### docker pull ubuntu ```shell= $ docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu 5d3b2c2d21bb: Pull complete 3fc2062ea667: Pull complete 75adf526d75b: Pull complete Digest: sha256:b4f9e18267eb98998f6130342baacaeb9553f136142d40959a1b46d6401f0f2b Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest $ ``` ## ダウンロードされたdockerイメージを表示 ```shell= $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest d1165f221234 11 days ago 13.3kB ubuntu latest 4dd97cefde62 13 days ago 72.9MB $ ``` ## Dockerイメージを削除 ```shell= $ docker rmi xxxxxxxx ``` 不要なリソース(イメージ、コンテナ、ボリューム、およびネットワーク)をクリーンアップする。 ```shell= $ docker system prune ``` さらに、停止したコンテナと(不要なイメージだけでなく)すべての未使用のイメージを削除するには、コマンドに-aフラグを追加します。 ```shell= docker system prune -a ``` ## Dockerコンテナを開始 ##### docker run -it ubuntu ```shell= $ docker run -it ubuntu root@b12784dd4ce1:/# ``` :::info :bulb: ※「b12784dd4ce1」はコンテナID。 ※開始されたコンテナ内で行った操作は、コンテナ終了後も保存されている。 ※アプリケーションのインストールは、`apt update`を行ってから`apt install vim`として行う。 ※単純に、`docker run ubuntu`を実行すると`start`しても開始されない??`attach`も出来ない。 > docker run --help > | パラメータ | 説明 | > | ----------------- | ------------------------------------ | > | -i, --interactive | アタッチされていなくても標準入力を開いておく | > | -t, --tty | 疑似端末(TTY)を割り当てる | ::: ## dockerコンテナの管理 ### アクティブなコンテナの表示 ```shell= $ docker ps ``` ### アクティブ及び非アクティブ(停止)なコンテナの表示 ```shell= $ docker ps -a ``` ### 非アクティブ(停止)なコンテナを開始するには、 docker start を使用し、その後にコンテナIDまたはコンテナの名前を指定します。 ```shell= $ docker start b12784dd4ce1 ``` ### 実行中のコンテナに接続(ログイン/アタッチ)する ```shell= $ docker attach b12784dd4ce1 ``` ### コンテナの削除 ```shell= $ docker rm youthful_curie ``` ## コンテナ内の変更をdockerイメージに適用する ###### tags: `Docker` `Ubuntu`