Dockerを試す
- ゴール
- 実行環境
- Dockerの概要
- Dockerのインストール
- hello-worldのコンテナを試す
- Apacheのコンテナを試す
- Dockerfileを試す
- docker-composeを試す
- より深く理解するために
ゴール
- Dockerの概念を理解する
- Dockerの使い方をざっくり理解する
実行環境
- OS:Windows 10
- Docker Desktop:4.2.0
Dockerの概要
Dockerのインストール
Docker Desktop on Windowsのインストール
公式サイトからインストーラをダウンロードして、ウィザードに従ってインストール。
インストール後にPCの再起動を求められるので再起動。
WSLのインストール
WSLのインストールを求められたので、以下から更新プログラムをダウンロードしてインストール。
Dockerのバージョン確認
PS C:\work> docker version Cloud integration: v1.0.20 API version: 1.41 Git commit: b485636 Built: Mon Oct 25 07:47:53 2021 OS/Arch: windows/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.10 API version: 1.41 (minimum version 1.12) Go version: go1.16.9 Git commit: e2f740d Built: Mon Oct 25 07:41:30 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.11 GitCommit: 5b46e404f6b9f661a205e28d59c982d3634148f8 runc: Version: 1.0.2 docker-init: GitCommit: de40ad0
hello-worldのコンテナを試す
PS C:\work> docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685 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/ PS C:\work> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8c8a78aa96b7 hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago elated_ptolemy
Apacheのコンテナを試す
Apacheコンテナの起動
PS C:\work> docker run --name myapache -d -it -p 80:80 -v C:\work\docker\myapache:/usr/local/apache2/htdocs httpd Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd eff15d958d66: Pull complete ba1caf8ba86c: Pull complete ab86dc02235d: Pull complete 0d58b11d2867: Pull complete e88da7cb925c: Pull complete Digest: sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02 Status: Downloaded newer image for httpd:latest bc8af3cb770b48ca37d721cccc530908a8bbf8b748cf828231bd198573cf7104 PS C:\work> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bc8af3cb770b httpd "httpd-foreground" 37 seconds ago Up 34 seconds 0.0.0.0:80->80/tcp myapache
Apacheの動作確認
PS C:\work> cat .\docker\myapache\index.html <html> <body> <h1>Test Container</h1> </body> </html> PS C:\work> curl.exe http://localhost <html> <body> <h1>Test Container</h1> </body> </html>
コンテナの停止
PS C:\work> docker stop myapache myapache PS C:\work> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bc8af3cb770b httpd "httpd-foreground" 17 minutes ago Exited (0) 17 seconds ago myapache 8c8a78aa96b7 hello-world "/hello" 2 days ago Exited (0) 2 days ago elated_ptolemy
コンテナ内の操作
PS C:\work> docker exec -it myapache /bin/bash root@bc8af3cb770b:/usr/local/apache2# ls bin build cgi-bin conf error htdocs icons include logs modules root@bc8af3cb770b:/usr/local/apache2# ls htdocs/ index.html root@bc8af3cb770b:/usr/local/apache2# cat htdocs/index.html <html> <body> <h1>Test Container</h1> </body> </html> </html>root@bc8af3cb770b:/usr/local/apache2# exit exit
コンテナの削除
PS C:\work> docker stop myapache myapache PS C:\work> docker rm myapache myapache PS C:\work> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8c8a78aa96b7 hello-world "/hello" 2 days ago Exited (0) 2 days ago elated_ptolemy P
Dockerfileを試す
Dockerfileの作成
PS C:\work\docker\testphpimg> ls ディレクトリ: C:\work\docker\testphpimg Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2021/12/3 7:43 127 Dockerfile PS C:\work\docker\testphpimg> cat .\Dockerfile FROM centos:centos7 RUN yum -y install httpd php COPY test.php /var/www/html/ PS C:\work\docker\testphpimg> cat .\test.php <?php echo "hoge"; ?>
イメージの作成
PS C:\work\docker\testphpimg> docker build -t testphpimg . [+] Building 2.0s (8/8) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 31B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:centos7 1.9s => [internal] load build context 0.0s => => transferring context: 29B 0.0s => [1/3] FROM docker.io/library/centos:centos7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 0.0s => CACHED [2/3] RUN yum -y install httpd php 0.0s => CACHED [3/3] COPY test.php /var/www/html/ 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:d97dd2efb45779bc4525f9d499fb394072fa608c67374abac52dfb5cd0e32556 0.0s => => naming to docker.io/library/testphpimg 0.0s Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them PS C:\work\docker\testphpimg> docker images REPOSITORY TAG IMAGE ID CREATED SIZE testphpimg latest d97dd2efb457 5 minutes ago 403MB httpd latest ad17c88403e2 13 days ago 143MB hello-world latest feb5d9fea6a5 2 months ago 13.3kB
コンテナの起動
PS C:\work\docker\testphpimg> docker run -d -p 8080:80 --name testweb testphpimg 5b1f3d65bf0c35e968d627392d40f477093b7a8c8f0915c2d1223a12e6c49c6d PS C:\work\docker\testphpimg> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5b1f3d65bf0c testphpimg "/usr/sbin/httpd -DF…" 13 seconds ago Up 11 seconds 0.0.0.0:8080->80/tcp testweb
動作確認
PS C:\work\docker\testphpimg> curl.exe http://localhost:8080/test.php hoge
キャッシュ(レイヤー)を意識して、こるべくコマンドをまとめる
docker-composeを試す
docker-compose.ymlの作成
PS C:\work\docker\wordpress> cat .\docker-compose.yml version: '3' services: mysql: image: mysql:8.0.20 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: wordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - mysql image: wordpress:php7.4-apache ports: - "80:80" restart: always environment: WORDPRESS_DB_HOST: mysql:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress volumes: db_data:
docker-composeの起動
PS C:\work\docker\wordpress> docker-compose up -d Creating network "wordpress_default" with the default driver Creating volume "wordpress_db_data" with default driver Pulling mysql (mysql:8.0.20)... 8.0.20: Pulling from library/mysql 8559a31e96f4: Pull complete d51ce1c2e575: Pull complete c2344adc4858: Pull complete fcf3ceff18fc: Pull complete 16da0c38dc5b: Pull complete b905d1797e97: Pull complete 4b50d1c6b05c: Pull complete c75914a65ca2: Pull complete 1ae8042bdd09: Pull complete 453ac13c00a3: Pull complete 9e680cd72f08: Pull complete a6b5dc864b6c: Pull complete Status: Downloaded newer image for mysql:8.0.20 Pulling wordpress (wordpress:php7.4-apache)... php7.4-apache: Pulling from library/wordpress e5ae68f74026: Pull complete 99c3c1c4d556: Pull complete 2c23b6beb07a: Pull complete 9874148cff9a: Pull complete 52defac98629: Pull complete 585eed6f5399: Pull complete aa83ee0498ff: Pull complete 356035aff0a0: Pull complete 72fcc4ec7a35: Pull complete e0903fae56b0: Pull complete 27d2ad3b821d: Pull complete a3bf4a72a8eb: Pull complete 5066fa6c34bb: Pull complete 8dbc8886c1b4: Pull complete 06c5ee2768f5: Pull complete 32dbd5d28bf9: Pull complete 903bc701dbea: Pull complete 4fa20fe4bcdb: Pull complete cfb6ac74b18d: Pull complete 8aea2def1cba: Pull complete 7c4a06a0fac8: Pull complete Digest: sha256:18040d7da75c99fd94de4765e8a2c2eba4cf95c826b32850e8680f68b1bf7a2a Status: Downloaded newer image for wordpress:php7.4-apache Creating wordpress_mysql_1 ... done Creating wordpress_wordpress_1 ... done PS C:\work\docker\wordpress> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e653ef3fb109 wordpress:php7.4-apache "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp wordpress_wordpress_1 fd7151d61c0c mysql:8.0.20 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 3306/tcp, 33060/tcp wordpress_mysql_1
動作確認
コンテナの停止
PS C:\work\docker\wordpress> docker-compose stop Stopping wordpress_wordpress_1 ... done Stopping wordpress_mysql_1 ... done PS C:\work\docker\wordpress> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
コンテナの削除
PS C:\work\docker\wordpress> docker-compose rm Going to remove wordpress_wordpress_1, wordpress_mysql_1 Are you sure? [yN] y Removing wordpress_wordpress_1 ... done Removing wordpress_mysql_1 ... done PS C:\work\docker\wordpress> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES