上一篇利用 不同的命令来构建 nginx,mysql,php镜像 和 容器。 这样做比较麻烦,也很容易出错,当然可以写入 sh脚本来执行。但是可以通过 docker-compose 来达到效果,管理起来更加简单方便。
docker-compose安装
github地址:https://github.com/docker/compose
curl -L https://github.com/docker/compose/releases/download/1.13.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
目录结构
lemon@lemon:~/docker$ tree
.
├── docker-compose.yml
├── mysql
│ └── dockerfile
├── nginx
│ ├── conf
│ │ ├── nginx.conf
│ │ └── vhost
│ ├── dockerfile
└── php7
├── conf
│ └── php.ini
└── dockerfile
编写yml文件
version : ''
services :
mysql :
build :
context : ~/docker/mysql
dockerfile : dockerfile
ports :
- "3306:3306"
volumes :
- ~/docker/mysql/data:/var/lib/mysql
#初始密码
environment :
- MYSQL_ROOT_PASSWORD=
container_name : mysql
restart : always
php7 :
build :
context : ~/docker/php7
dockerfile : dockerfile
ports :
- "9000:9000"
volumes :
- ~/web:/usr/local/nginx/html
- ~/docker/php7/conf/php.ini:/usr/local/php7/lib/php.ini:ro
container_name : php7
links :
- mysql
restart : always
nginx :
build :
context : ~/docker/nginx
dockerfile : dockerfile
ports :
- "80:80"
- "433:433"
volumes :
- ~/docker/nginx/logs:/usr/local/nginx/logs
- ~/web:/usr/local/nginx/html
- ~/docker/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf:ro
- ~/docker/nginx/conf/vhost:/usr/local/nginx/conf/vhost:ro
container_name : nginx
links :
- php7
restart : always
运行命令自动构建容器
sudo docker-compose up -d
查看状态
lemon@lemon:~/docker$ sudo docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------
mysql docker-entrypoint.sh mysqld Up 0.0.0.0:->/tcp
nginx /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:->/tcp, /tcp, 0.0.0.0:->/tcp
php7 /usr/local/php7/sbin/php-f ... Up 0.0.0.0:->/tcp
具体文件查看地址:https://github.com/zhaocong222/docker-lnmp