やったこと
事前準備
講座・ハンズオン
- Dockerの仕組みのかんたんな紹介
- Dockerを使うために最低限理解しておくこと
- Step.1 公開されているイメージを使う
- MySQL5.6
- MySQL5.7
- Step.2 Dockerfileからイメージを作成し使う
- サンプルのDockerfileからphpを実行するAPコンテナを作成
- Step.3 イメージを組み合わせて使う
- サンプルのDockerfileからWebサーバ環境(コンテナの組合せ)を作成
インストール
1. アンインストール
- まず、インストールしてしまったdocker-engineをアンインストール
sudo apt-get purge docker-engine sudo rm -rf /var/lib/docker
2. SET UP THE REPOSITORY
dockerの公式ドキュメントの通りにインストール
https://docs.docker.com/install/linux/docker-ce/ubuntu/#uninstall-docker-ce
- Update the apt package index:
sudo apt-get update
- install packages to allow apt to use a repository over HTTPS:
$ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common
- Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Verify that you now have the key with the fingerprint
sudo apt-key fingerprint 0EBFCD88 pub 4096R/0EBFCD88 2017-02-22 Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 uid Docker Release (CE deb) <docker@docker.com> sub 4096R/F273FCD8 2017-02-22
- Set up the stable repository
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
3.INSTALL DOCKER CE
- Update the apt package index.
sudo apt-get update
- Install the latest version of Docker CE
sudo apt-get install docker-ce
- Verify that Docker CE is installed correctly by running the hello-world image.
sudo docker container run hello-world
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535 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/
4. non-root userでDockerをwork betterにするための設定
以下の理由より、インストールしただけだとsudoを常にしないいけなにので、めんどう
The Docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The Docker daemon always runs as the root user.
- Create the docker group
sudo groupadd docker
- Add your user to the docker group
sudo usermod -aG docker $USER
Log out and log back in so that your group membership is re-evaluated
Verify that you can run docker commands without sudo
docker run hello-world
基本コマンド
systemctlコマンド
- docker service起動
❯ systemctl start docker.service
- docker service停止
❯ systemctl stop docker.service
- docker service ステータス確認
❯ systemctl status docker.service
dockerコマンド
- Dockerfileからイメージをビルド
${PATH}
はDockerfileがあるパス
docker build -t ${REPOSITORY}[:${TAG}] ${PATH}
- コンテナ起動、停止、再起動
docker start | stop | restart ${コンテナID}
- 起動中のコンテナプロセス表示(-aオプションで停止中のプロセスも表示)
docker ps
- コンテナのログを表示
docker logs ${コンテナID}
- コンテナ内でコマンド実行
docker exec ${コンテナID} ${コマンド}
- コンテナへのログイン
exec
はすでに起動しているコンテナでコマンドを実行する。run
コマンドは新たにコンテナを起動してからそのコンテナでコマンドを実行する。
docker exec -it ${コンテナID} bash
- コンテナ削除
- 削除前に対象コンテナの停止が必要
docker rm ${コンテナID}
- イメージを削除
docker rmi ${イメージID}
- Run a Docker Container
- runコマンドはコマンド実行中のみコンテナを起動。コマンド実行終了後にはコンテナはendする。
docker run ${イメージ名 || イメージID} ${command to run into container}
- e.g. catコマンドを実行
docker run ubuntu cat /etc/issue
- 初回の動作確認
❯ sudo docker run hello-world 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/
- Run an Interactive Session into a Container
docker run -it ubuntu bash
- 起動しているコンテナのセッションからhostのターミナルに戻るには2通りある。
- 1 exit
exit
はコンテナの全てのセッションを終了して、そのコンテナ自体も停止させる
- 2
Ctrl+p
orCtrl+q
- セッションを維持しつつ、hostのターミナルに戻りたい場合はこのキーを使う
- 新規セッションを作ってこのキーで出たりを繰り返すと、その分だけコンテナが起動しセッションも生成されてしまうので注意。元のセッションへの戻り方は次の項目に記載
- 1 exit
❯ docker run -it ubuntu bash root@7519130e117b:/# exit exit ~ ❯ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ~ ❯ docker run -it ubuntu bash root@f0dffe57d9ce:/# ~ 27s ❯ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f0dffe57d9ce ubuntu "bash" 31 seconds ago Up 29 seconds friendly_stonebraker
- 再度、コンテナに戻る方法
- まず、
docker ps
でID or nameを確認する。 - そして以下のコマンドで元のセッションのコンテナに接続し直す
exec
でコンテナに入るのと同じこと?
- まず、
docker attach ${コンテナID or name}
- 稼働しているコンテナをホストから停止する
docker kill ${コンテナID or name}
docker-composeコマンド
- 複数イメージを組合せたコンテナ群(docker-compose.yml)を起動
docker-compose up -d
- docker-composeで起動した各コンテナの状態を確認
docker-compose ps
- コンテナ群の起動、停止、再起動
docker-compose start | stop | restart
- コンテナを停止して、削除
docker-compose down
Docker用語
- Dockerイメージ
- Dockerコンテナ
ハンズオン
Step.1 公開されているイメージを使うパターン
https://hub.docker.com/_/mysql
1. mysqlのdockerイメージ取得
docker pull ${REPOSITORY}[:${TAG}]
- バージョン(5.6)を指定してdockerイメージを取得
docker pull mysql:5.6
2. dockerイメージを確認
- 取得したイメージを確認する
docker images
3. 取得したdockerイメージからmysqlコンテナ起動
- dockerイメージからコンテナを起動
- 上記で確認した
${REPOSITORY}[:${TAG}]
を指定する 今回の起動コマンドの基本構文は以下の通り
基本構文
docker run -d --name ${NAME} -e MYSQL_ROOT_PASSWORD=my-secret-pw ${REPOSITORY}[:${TAG}]
${NAME}
: コンテナに割り当てたい任意の名前my-secret-pw
: MySQLのrootユーザーのパスワード${TAG}
: MySQLのバージョンを明示をする際に付ける-d
: コンテナをバックグラウンド実行例
docker run -d --name mysql5.6 -e MYSQL_ROOT_PASSWORD=password mysql:5.6
4. mysqlコンテナ内でコマンド実行
- login shellとしてbashを実行(コンテナにログイン)
docker exec -it some-mysql bash
- 他のコマンド
docker exec -it mysql5.6 ls -al
5. mysqlサーバーのログを見る
docker logs some-mysql
Step.2 Dockerfileからイメージを作成するパターン
1. Dockerfileをビルドしイメージを作成する
Dockerfileがあるディレクトリをカレントにする
構文
docker build -t ${REPOSITORY}[:${TAG}] ${PATH}
- 例
docker build -t php .
tag
: イメージのバージョンを明示する際に付ける
2. dockerイメージを確認
- 取得したイメージを確認する
docker images
3. 上記で作成したイメージからコンテナを起動する
- 基本構文
docker run -d --name ${NAME} ${REPOSITORY}[:${TAG}] -p ${HOST PORT: CONTAINER PORT}
-d
: コンテナをバックグラウンド実行例
docker run -d --name php -p 80:80 php
Step.3 docker-compose.ymlからイメージを組合せて作成するパターン
docker-compose.ymlがあるディレクトリをカレントにする
docker-composeコマンドを実行する
sudo docker-compose up –d
- ホストで使用するポートを専有している場合は、サービスを停止しておく
- 今回はMySQL(3306)が起動しており、コンテナ起動失敗した