楼上的回答已经讲的很清楚了,db服务没有定义,在compose中link并不是链接容器,而是服务,并且compose启动的时候会单独create network ,也并不是和db-mysql在同一个网络中
$docker ps
$docket network ls
$docker-compose ps
解决这个问题,2点
1.把db也使用compose管理,定义在compose.yml文件中
db-mysql
.....
2.把webserver服务加入到和dbmysql一个网络中
例如:
db-backup.yml
version: '3.0'
services:
# 启动一次备份服务 ./docker-compose restart backup
backup:
image: mysql:5.7
environment:
MYSQL_HOST: mysql
MYSQL_DATABASE: package_v1
MYSQL_USER: root
MYSQL_PASSWORD: zhaojun
external_links:
- dockerprintproject_mysql-db_1:mysql
volumes:
- /etc/localtime:/etc/localtime:ro
- ./data/backup:/backup:rw
- ./dockerfiles/backup/:/app:ro
entrypoint:
- /bin/bash
command: /app/backup-default.sh
启动备份
[root@docker docker-print-project]# docker-compose -f db-backup.yml up
产品docker-compose.yml 部分服务
version: '3.0'
services:
nginx:
image: nginx:1.13.3
volumes:
#网站目录
- ./app:/var/www/html
- ./dockerfiles/nginx/conf.d:/etc/nginx/conf.d:ro
- /etc/localtime:/etc/localtime:ro
- ./logs/nginx:/var/log/nginx
ports:
- "80:80"
deploy:
restart_policy:
condition: any
command: nginx -g 'daemon off;'
mysql-db:
image: mysql:5.7
volumes:
- ./data/mysql:/var/lib/mysql:rw
- /etc/localtime:/etc/localtime:ro
- ./logs/mysql:/var/lib/mysql-logs:rw
- ./dockerfiles/mysql/conf.d:/etc/mysql/conf.d:ro
environment:
MYSQL_ROOT_PASSWORD: zhaojun
MYSQL_DATABASE: package_v1
MYSQL_USER: zhaojun
MYSQL_PASSWORD: zhaojun
deploy:
restart_policy:
condition: any
docker ps