1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWSのEC2でDockerを試す

1
Last updated at Posted at 2016-06-01

docker-machineで起動する

docker-machineは、Amazon EC2のドライバーが提供されているのでAWS上にDockerを起動

Dokerホストの起動
$ docker-machine create --driver amazonec2 \
--amazonec2-access-key XXXXXXXX \
--amazonec2-secret-key YYYYYYYY \
--amazonec2-region "ap-northeast-1" \
--amazonec2-zone b \
--amazonec2-subnet-id subnet-ZZZZZ \
--amazonec2-vpc-id vpc-VVVVVV \
aws01
```

### zoneを指定しないとエラーが出る
`--amazonec2-zone b \`を除くとデフォルトで`availabilty zone A`に作ろうとするので注意

このようなエラーが出てしまう

```
(aws02) Launching instance...
Error creating machine: Error in driver during machine creation: Error launching instance: InvalidParameterValue: Value (ap-northeast-1a) for parameter availabilityZone is invalid. Subnet 'subnet-754a7b01' is in the availability zone ap-northeast-1b
        status code: 400, request id:
```

それで`docker-machine ls`をすると

```
aws02   -        amazonec2   Error                                       Unknown   MissingParameter: The request must contain the parameter InstanceId
        status code: 400, request id:
```

が残ってしまうので強制消去の`-f`を付与して消す

```
$ docker-machine rm -f aws02
About to remove aws02
Successfully removed aws02
```


## 起動の確認

```起動の確認
$ docker-machine ls
NAME    ACTIVE   DRIVER      STATE     URL                         SWARM   DOCKER    ERRORS
aws01   *        amazonec2   Running   tcp://54.238.YYY.XXX:2376           v1.11.1
```

あとは環境変数をセットするだけ

```lang:環境変数をセット
$ docker-machine env aws01
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://54.238.YYY.XXX:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/aws01"
export DOCKER_MACHINE_NAME="aws01"
$ Run this command to configure your shell:
$ eval $(docker-machine env aws01)
```
最後の行の
`eval $(docker-machine env aws01)`を実行

## Wordpressのコンテナを作成
Wordpressを作成するために`docker-compose.yml`を作成

```lang:docker-compose.yml
wordpress:
  image: wordpress
  links:
    - db:mysql
  ports:
    - 80:80

db:
  image: mariadb
  environment:
    MYSQL_ROOT_PASSWORD: password
```

## コンテナの起動
`docker-compose.yml`があるディレクトリで以下のコマンドを実行

```
$ docker-compose up -d

```

## コンテナーが起動したかを確認
```
# docker-compose ps
     Name                    Command               State          Ports
-------------------------------------------------------------------------------
ec2_db_1          docker-entrypoint.sh mysqld      Up      3306/tcp
ec2_wordpress_1   /entrypoint.sh apache2-for ...   Up      0.0.0.0:80->80/tcp
```

## 試しにWEBで見てみる
まずは接続するIPを確認する

```lang:接続IPの確認
$ docker-machine ls
NAME    ACTIVE   DRIVER      STATE     URL                       SWARM   DOCKER    ERRORS
aws01   *        amazonec2   Running   tcp://54.238.YYY.XXX:2376           v1.11.1
```
WEBでアクセス
http://54.238.YYY.XXX


## ついでにコンテナーの内容を閲覧
何をしているかというとbashを外部コマンドとして実行している

```
$ docker exec  -it ec2_web02_1 bash
```

## おまけでデータの永続性
busyboxを使ってログとコンテンツ、DBのデータを隔離する

```lang:docker-compose.yml
vol_data:
  image: busybox:latest
  volumes:
   - /var/www/html:/var/www/html
   - /var/log/httpd:/var/log/httpd
   - /var/lib/mysql:/var/lib/mysql

web01:
  image: wordpress:latest
  links:
    - db01:mysql
  ports:
    - 80:80
  volumes_from:
    - vol_data
    - 
db01:
  image: mariadb
  environment:
    MYSQL_ROOT_PASSWORD: P@ssw0rd
  volumes_from:
    - vol_data
```
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?