我想在一个桥接网络下将三个服务(db、nginx 和 web 服务)链接在一起,并且能够从我的本地主机或容器内部访问不同的服务,并有一个用于 web 服务的交互式终端。我怎样才能做到这一点?
我的 docker-compose 配置如下:
version: '2'
services:
web:
build:
context: .
dockerfile: Dockerfile-alpine
command: /bin/bash
ports:
- "5000:5000"
volumes:
- $REPO_DIR:/repository
links:
- db
- nginx
nginx:
image: nginx:stable-alpine
ports:
- "8080:8080"
volumes:
- $NGINX_STATIC_DIR:/var/www/static
- $NGINX_CONFIG_FILE:/etc/nginx/nginx.conf
db:
image: mysql/mysql-server
ports:
- "3307:3306"
environment:
MYSQL_USER: $MYSQL_USER
MYSQL_PASSWORD: $MYSQL_PASSWORD
MYSQL_DATABASE: $MYSQL_DATABASE
MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
volumes:
- $DB_DATA_DIR:/var/lib/mysql
- $DB_LOG_DIR:/var/log/mysql
Web 服务使用mhart/alpine-node:6.7.0
作为基础,并进一步添加了一些实用程序以及 Python3.5 和 Flask。
如果我这样做docker-compose run web
,我可以访问交互式终端,但是 db 和 nginx 服务没有正确启动/链接。我得到以下输出:
Starting courseadmin_nginx_1
Starting courseadmin_db_1
bash-4.3#
然后来自容器内部的进一步网络取证发现 nginx 或 db 都不存在。我可以进一步支持这一说法,因为使用docker-compose up
实际上为我的主机系统上安装了 mysql 的 db 服务创建了端口冲突,它从未报告过docker-compose run
.
另一方面,如果我尝试docker-compose up
,我会得到更活跃的跟踪,但没有交互式终端:
Starting courseadmin_nginx_1
Recreating courseadmin_db_1
Recreating courseadmin_web_1
Attaching to courseadmin_nginx_1, courseadmin_db_1, courseadmin_web_1
db_1 | Initializing database
courseadmin_web_1 exited with code 0
db_1 | Database initialized
db_1 | MySQL init process in progress...
db_1 | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db_1 | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db_1 | mysql: [Warning] Using a password on the command line interface can be insecure.
db_1 | mysql: [Warning] Using a password on the command line interface can be insecure.
db_1 | mysql: [Warning] Using a password on the command line interface can be insecure.
db_1 | mysql: [Warning] Using a password on the command line interface can be insecure.
db_1 |
db_1 | /entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1 |
db_1 |
db_1 | MySQL init process done. Ready for start up.
db_1 |
我可以做些什么来实现交互式终端和我的三个容器的正确链接?