Ubuntu 18.04 LTS で、Docker使えるようにする

Ubuntu 18.04 LTS 環境に、
Dockerをインストールする手順を記載します。
docker-composeもついでにインストールしているので、確認してみてください。

実は昨日、趣味のオンプレ環境にDockerインストールしたものが欲しくて
CentOS7環境でDockerインストールしたものを作ってたんですが、
なんだかんだで結局 Ubuntu 18.04 LTS を使うことにしました。

やっぱり、個人で使う分にはUbuntuの方が使いやすいよね!
デフォルトでインストールされるソフトもほぼ最新のものが入るし。

今回の記事は Ubuntu18.04 LTS 環境ですが、
他の環境向けにも記事があるので、リンク貼っておきます。

CentOS7で、docker使えるようにする 。

Ubuntu 16.04LTS に Docker をインストールする

Docker インストール

必須パッケージ

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

docker コマンドのインストール

sudo apt-get update
apt-cache madison docker-ce
sudo apt-get install docker-ce

hello-world

上記の手順を実行すれば、dockerコマンドが実行できるようになっているはずです。
試しに、hello-worldというイメージを起動してみましょう。
sudo docker run hello-world

下記のように、”Hello from Docker!”と出力されていればOKです!
ちゃんと動いています。

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
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/

sudoなしでも実行できるように

インストールした状態だと、sudoをつけないと実行権限が〜!といわれてしまいます。

$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get htt
p://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied

dockerコマンドをsudoなしでも実行できるように権限付与していきます。

docker グループを追加

sudo groupadd docker

おそらく、実行すると既にグループはあるはずなので、
以下のようなメッセージが出る場合はとくに気にする必要はありません。

$ sudo groupadd docker
groupadd: group 'docker' already exists

ログインユーザーをdockerグループに所属させる。

sudo usermod -aG docker $USER

dockerグループの権限を与えていることによって、
sudoなしでもコマンドが実行可能になります。

※一度ログアウトして、再度ログインしましょう。
sudo をつけなくても、docker ps -a など、
dockerコマンドが自由に使えるはずです。

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
64c0af37569b        hello-world         "/hello"            32 minutes ago      Exited (0) 32 minutes ago                       thirsty_borg

docker-composeコマンドのインストール

コンテナを複数組み合わせて使う際には、便利なdocker-composeを使いましょう。
k8sのような規模にまで大げさにしなくても、手軽にコンテナを連携させて使うことができます。

docker-compose ダウンロード

公式のドキュメントに書いてある内容ですが、
curlで/usr/local/bin 配下にダウンロードしてきます。

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

docker-compose 実行権限付与

実行するための権限を付与

sudo chmod +x /usr/local/bin/docker-compose

docker-composeコマンドが使える状態になります。

$ docker-compose --version
docker-compose version 1.23.1, build b02f1306

docker-composeを入れたので、SSL化する場合は、こちらの記事もご参考に〜!

docker-compose、nginxプロキシでhttps対応する。

公式ドキュメント

1件のコメント

コメントを残す