Ubuntu18.04 LTSにDockerとdocker-composeをインストールする。
apt-getでのインストール
公式に詳細が書いてあるので、簡単に記載する。
https://docs.docker.com/install/linux/docker-ce/ubuntu/
1. 古いバージョンのアンインストール
$ sudo apt-get remove docker docker-engine docker.io containerd runc
2. 必要なパッケージをインストール
$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
3. Dockerの公式GPGキーの入手
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4. 安定版リポジトリの入手
CPUアーキテクチャで異なるが、基本的にはx86_64/amd64だと思うので、下記で問題ないはず。
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
5. Dockerのインストール
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
6. 動作確認
$ sudo docker run hello-world
$ sudo docker ps -a
hello-worldコンテナが停止していることがわかる
$ sudo docker images
hello-worldイメージを確認できる
$ sudo docker rm コンテナID
コンテナを削除する
コンテナIDはsudo docker ps -aで確認できる
$ sudo docker rmi イメージID
イメージを削除する
イメージIDはsudo docker imagesで確認できる
7. sudoなしでの実行
dockerグループに所属させることで、sudoなしでdockerコマンドを使える。
$ sudo adduser $USER docker
$ newgrp docker
snapでのインストール
1. dockerグループの作成・所属
$ sudo addgroup --system docker
$ sudo adduser $USER docker
$ newgrp docker
2. dockerのインストール
snapではこのとき、docker-composeもインストールされる。
$ sudo snap install docker
3. 動作確認
apt-getでのインストール - 6. 動作確認 と同様。
$ sudo docker run hello-world
$ sudo docker ps -a
hello-worldコンテナが停止していることがわかる
$ sudo docker images
hello-worldイメージを確認できる
$ sudo docker rm コンテナID
コンテナを削除する
コンテナIDはsudo docker ps -aで確認できる
$ sudo docker rmi イメージID
イメージを削除する
イメージIDはsudo docker imagesで確認できる
docker-composeのインストール
snapでインストールした場合は不要。
詳細は下記を参照。https://docs.docker.com/compose/install/
1. docker-composeの入手
最新版であるか、URLを要確認。1.25.3が変わっている恐れがある。
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
2. docker-composeに実行権を付与
sudo chmod +x /usr/local/bin/docker-compose
3. 動作確認
$ mkdir test
$ cd test
$ vi docker-compose.yml
# 下記をdocker-compose.ymlに書き込む
version: '3'
services:
test:
image: hello-world
$ docker-compose up
# 下記のように出力されるはず。
Pulling hello-world (hello-world:)...
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Creating test_hello-world_1 ... done
Attaching to test_hello-world_1
hello-world_1 |
hello-world_1 | Hello from Docker!
hello-world_1 | This message shows that your installation appears to be working correctly.
hello-world_1 |
hello-world_1 | To generate this message, Docker took the following steps:
hello-world_1 | 1. The Docker client contacted the Docker daemon.
hello-world_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
hello-world_1 | (amd64)
hello-world_1 | 3. The Docker daemon created a new container from that image which runs the
hello-world_1 | executable that produces the output you are currently reading.
hello-world_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it
hello-world_1 | to your terminal.
hello-world_1 |
hello-world_1 | To try something more ambitious, you can run an Ubuntu container with:
hello-world_1 | $ docker run -it ubuntu bash
hello-world_1 |
hello-world_1 | Share images, automate workflows, and more with a free Docker ID:
hello-world_1 | https://hub.docker.com/
hello-world_1 |
hello-world_1 | For more examples and ideas, visit:
hello-world_1 | https://docs.docker.com/get-started/
hello-world_1 |
test_hello-world_1 exited with code 0
snapで動作しない...
ルートディレクトリにディレクトリを作って、docker-compose upすると、docker-compose.ymlを見つけられない...
stack overflowで質問中。
https://ja.stackoverflow.com/questions/63172/snapでdockerをインストールした場合-docker-composeを実行するとdocker-compose-ymlを見つけられない
ERROR:
Can't find a suitable configuration file in this directory or any
parent. Are you in the right directory?
Supported filenames: docker-compose.yml, docker-compose.yaml