---
# System prepended metadata

title: Wordpress on Container using docker-compose

---

# Wordpress on Container using docker-compose
## ホスト情報
```
ip_address: 192.168.11.20
```
## VM情報
`Vagrantfile`
```
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.define "dc01" do |wp|
    wp.vm.hostname = "wp01"
    wp.vm.network "forwarded_port", guest: 80, host: 11080
    wp.vm.network "forwarded_port", guest: 22, host: 11022, id: "ssh"
  end
  config.vm.define "dc02" do |wp|
    wp.vm.hostname = "dc02"
    wp.vm.network "forwarded_port", guest: 80, host: 22080
    wp.vm.network "forwarded_port", guest: 22, host: 22022, id: "ssh"
  end
end
```

## Docker-composeインストール
```
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
```

## Dockerコンテナを削除しておく
ポートバッティングを防ぐため
```
$ sudo docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                 PORTS                                 NAMES
69a3c28e7e7b        wordpress               "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes           0.0.0.0:80->80/tcp                    jovial_elbakyan
$ sudo docker stop ${CONTAINER_ID}
$ sudo docker rm ${CONTAINER_ID}
```

## Docker-composeで作る
```
$ vi docker-compose.yml
```
`docker-compose.yml`
```
version: "3.5"

services:
  wordpress:
    image: wordpress:latest
    ports:
      - 80:80
    links:
      - wordpress-db
    environment:
      WORDPRESS_DB_HOST: wordpress-db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: database_password

  wordpress-db:
    image: mysql:5.7
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: database_password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: database_password
```

```
$ sudo docker-compose up -d
```
クライアント側のブラウザから http://192.168.11.20:${forwarded_port}/wordpress にアクセス
