DockerでWebアプリを複数起動させる

vagrant centos/7 に dockerおよびdocker-composeをインストール

[Git Bash]

$ mkdir ~/docker-test
$ cd ~/docker-test
$ vagrant init centos/7
$ vim Vagrantfile
----
-#config.vm.network "private_network", ip: "192.168.33.10"
+config.vm.network "private_network", ip: "192.168.33.10"
----
$ vagrant up
$ vagrant ssh

[VM]

dockerのインストール

[参考] https://docs.docker.com/engine/install/centos/

必要なライブラリのインストール
$ sudo yum install -y yum-utils
リポジトリのインストール
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
dockerのインストール
$ sudo yum install -y docker-ce docker-ce-cli containerd.io
$ docker -v

$ sudo systemctl enable docker
$ sudo systemctl start docker

一般ユーザーが docker pullを利用可能にする
$ id $USER
$ sudo usermod -aG docker $USER
$ id $USER

docker-composeのインストール

[参考] https://github.com/docker/compose/releases

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
$ docker-compose --version

[構成]

----
~/xproject
├── .env
├── docker-compose.yml
├── wordpress
│ ├── index.php
│ ├── …
└── wordpress2
  ├── index.php
  ├── …
----

$ vim .env
----
MYSQL_ROOT_PASSWORD=mysqlxxx
WORDPRESS_DB_PASSWORD=mysqlxxx
----

$ vim docker-compose.yml
----
version: '3'

services:
db:
image: mysql:8.0
container_name: mysql
volumes:
- "db-data:/var/lib/mysql"
restart: always
env_file: .env
networks:
- back_bridge
wordpress:
depends_on:
- db
image: wordpress
container_name: wordpress
expose:
- "80"
volumes:
- "$PWD/wordpress:/var/www/html"
env_file: .env
environment:
VIRTUAL_HOST: app1.xxx.com
WORDPRESS_DB_HOST: db:3306
LETSENCRYPT_HOST: app1.xxx.com
LETSENCRYPT_EMAIL: xxx@xxx.com
networks:
- front_bridge
- back_bridge
db2:
image: mysql:8.0
container_name: mysql2
volumes:
- "db-data2:/var/lib/mysql"
restart: always
env_file: .env
networks:
- back_bridge
wordpress2:
depends_on:
- db2
image: wordpress
container_name: wordpress2
expose:
- "80"
volumes:
- "$PWD/wordpress2:/var/www/html"
env_file: .env
environment:
VIRTUAL_HOST: app2.xxx.com
WORDPRESS_DB_HOST: db2:3306
LETSENCRYPT_HOST: app2.xxx.com
LETSENCRYPT_EMAIL: xxx@xxx.com
networks:
- front_bridge
- back_bridge
nginx-proxy:
container_name: nginx_proxy
image: jwilder/nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- html:/usr/share/nginx/html
- dhparam:/etc/nginx/dhparam
- vhost:/etc/nginx/vhost.d
- certs:/etc/nginx/certs:ro
- /var/run/docker.sock:/tmp/docker.sock:ro
labels:
- "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy"
restart: always
environment:
TZ: Asia/Tokyo
networks:
- front_bridge
letsencrypt-nginx-proxy-companion:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: nginx-proxy-lets-encrypt
restart: always
depends_on:
- "nginx-proxy"
volumes:
- certs:/etc/nginx/certs:rw
- vhost:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- /var/run/docker.sock:/var/run/docker.sock:ro
environment:
NGINX_PROXY_CONTAINER: nginx-proxy
networks:
- front_bridge

volumes:
db-data:
db-data2:
certs:
html:
vhost:
dhparam:

networks:
front_bridge:
external: true
back_bridge:
driver: bridge
----

$ docker network create front_bridge
$ docker-compose up

[ホストOSのhostsファイル]

$ vim Vagrantfile
----
192.168.33.10 app1.xxx.com
192.168.33.10 app2.xxx.com
----

[参考]
https://qiita.com/lzs0627/items/139e32d69b1bb9bd4e2c


投稿日

カテゴリー:

投稿者:

タグ: