UbuntuにDockerとdocker-composeを動かすための手順

UbuntuにDockerとdocker-composeをインストールする手順です。

# インストール手順

**公式のDockerインストール手順**
[Get Docker CE for Ubuntu](https://docs.docker.com/install/linux/docker-ce/ubuntu/)

公式を見ながら進めます。
以下、コマンド全部記載

## 1. Dockerインストール手順
```shell:install_docker
# Dockerインストール
$ 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"
$ sudo apt-get update
$ sudo apt-get install docker-ce

# sudoなくてもdockerコマンドを実行できるようにグループにアカウントを追加
$ sudo groupadd docker
$ sudo adduser $(whoami) docker

# 試しにhello-worldコンテナーを作成してみる。
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:083de497cff944f969d8499ab94f07134c50bcf5e6b9559b27182d3fa80ce3f7
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
```
以上でDockerインストール完了

## 2. docker-composeインストール手順

**最新バージョンとインストール手順の確認**
[docker-composeバージョン確認](https://github.com/docker/compose/releases)

公式のinstall手順に従います。

```shell:公式より
$ curl -L https://github.com/docker/compose/releases/download/1.20.0-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
```
unameの部分をこんな感じで編集します。

```shell:install_docker-compose
# docker-composeのインストール
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.12.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 実行権限を付与
$ sudo chmod +x /usr/local/bin/docker-compose

#バージョン確認
$ docker-compose version
docker-compose version 1.12.0, build b31ff33
docker-py version: 2.2.1
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
```

## 3. docker-compose お試し起動

**公式のdocker-composeサンプル**
[Get started with Docker Compose](https://docs.docker.com/compose/gettingstarted/#step-2-create-a-dockerfile)

公式の通りに以下のファイルを作成していきます。

- app.py
- requirements.txt
- Dockerfile
- docker-compose.yml

```shell
$ docker-compose up
Creating network "composetest_default" with the default driver
Building web
Step 1/5 : FROM python:3.4-alpine
3.4-alpine: Pulling from library/python
81033e7c1d6a: Pull complete
9b61101706a6: Pull complete
415e2a07c89b: Pull complete
f22df7a3f000: Pull complete
8c16bf19c1f9: Pull complete
Digest: sha256:fe436cb066394d81cf49448a04dec7c765082445a500bc44f1ae5e8a455793bd
Status: Downloaded newer image for python:3.4-alpine
 ---> 5c72717ec319
Step 2/5 : ADD . /code
 ---> 73b557e94c6c
Step 3/5 : WORKDIR /code
Removing intermediate container 01944f211f11
 ---> 685ec84d93af
Step 4/5 : RUN pip install -r requirements.txt
 ---> Running in c410e19a1af2
Collecting flask (from -r requirements.txt (line 1))
  Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
Collecting redis (from -r requirements.txt (line 2))
  Downloading redis-2.10.6-py2.py3-none-any.whl (64kB)
Collecting Jinja2>=2.4 (from flask->-r requirements.txt (line 1))
  Downloading Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting itsdangerous>=0.21 (from flask->-r requirements.txt (line 1))
  Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting Werkzeug>=0.7 (from flask->-r requirements.txt (line 1))
  Downloading Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Collecting click>=2.0 (from flask->-r requirements.txt (line 1))
  Downloading click-6.7-py2.py3-none-any.whl (71kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask->-r requirements.txt (line 1))
  Downloading MarkupSafe-1.0.tar.gz
Building wheels for collected packages: itsdangerous, MarkupSafe
  Running setup.py bdist_wheel for itsdangerous: started
  Running setup.py bdist_wheel for itsdangerous: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
  Running setup.py bdist_wheel for MarkupSafe: started
  Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/88/a7/30/e39a54a87bcbe25308fa3ca64e8ddc75d9b3e5afa21ee32d57
Successfully built itsdangerous MarkupSafe
Installing collected packages: MarkupSafe, Jinja2, itsdangerous, Werkzeug, click, flask, redis
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.14.1 click-6.7 flask-0.12.2 itsdangerous-0.24 redis-2.10.6
Removing intermediate container c410e19a1af2
 ---> d25bed972164
Step 5/5 : CMD ["python", "app.py"]
 ---> Running in 6e371ef549ae
Removing intermediate container 6e371ef549ae
 ---> 9924f6855f9a
Successfully built 9924f6855f9a
Successfully tagged composetest_web:latest
```

アクセスして以下がでればOKですね。

curlならこちら

```
$ curl 0.0.0.0:5000
Hello World! I have been seen 1 times.
```
ブラウザならこちら

![image.png](https://qiita-image-store.s3.amazonaws.com/0/30329/a16fdbf8-11b0-c3c6-ee38-045ebcebe20c.png)

以上です。