我有两个需要能够互相通信的应用程序。一个是项目应用程序,另一个是API。它们每个都有自己的docker-compose.yml文件,因此位于单独的Docker主机上。我对其进行了配置,以使它们共享相同的桥接网络,并且能够彼此“交谈”。在api应用程序中,我需要在/ etc / hosts文件中指定项目应用程序的ip地址,以便映射到正确的url。
目前,在检查网桥网络thank-view_net之后,我正在将IP地址硬编码为extra_hosts。有没有一种方法可以事先知道项目的ip地址,以便新创建项目的人不必在自己的计算机上查找它并自行更新docker-compose?
这是API docker-compose:
version: '2'
# define all services
services:
# our service is called laravel
api:
# we want to use the image which is build from our Dockerfile
build: .
# apache is running on port 80 but we want to expose this to port 8080 on our local machine
ports:
- 801:80
- 4431:443
# we depending on the mysql backend
volumes:
- ./:/var/www/html
networks:
- thank-views_net
container_name: api
extra_hosts:
- "dev-thankview.com:172.24.0.2"
depends_on:
- mysql_api
# we use this service to easily view our database on localhost:8080
phpmyadmin:
restart: always
image: phpmyadmin/phpmyadmin
networks:
- thank-views_net
ports:
- 8080:80
environment:
- PMA_HOST=mysql_api
- PMA_USER=root
- PMA_PASSWORD=root
depends_on:
- mysql_api
mysql_api:
# we use the mysql base image, version 5.7.22
image: mysql:8.0.12
# we mount a datavolume to make sure we don't loose data
ports:
- 33061:3306
volumes:
- db_api:/var/lib/mysql
networks:
- thank-views_net
# setting some envvars to create the DB
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=thankviews_api
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
volumes:
db_api:
networks:
thank-views_net:
external: true
这是docker-compose应用程序:
version: '2'
# define all services
services:
# our service is called laravel
laravel:
# we want to use the image which is build from our Dockerfile
build: .
# apache is running on port 80 and we expose it to port 80 on our host machine
ports:
- 80:80
- 443:443
# attach current directory to application directory in docker
volumes:
- ./:/var/www/html
networks:
- net
depends_on:
- mysql
# we use this service to easily view our database on localhost:8080
phpmyadmin:
restart: always
image: phpmyadmin/phpmyadmin
networks:
- net
ports:
- 8080:80
environment:
- PMA_HOST=myql
- PMA_USER=root
- PMA_PASSWORD=root
depends_on:
- mysql
mysql:
# we use the mysql base image, version 5.7.22
image: mysql:5.7.22
# we mount a datavolume to make sure we don't loose data
ports:
- 3306:3306
volumes:
- db_data:/var/lib/mysql
- ./docker/init_db:/docker-entrypoint-initdb.d
networks:
- net
# setting some envvars to create the DB
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=thankviews
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
volumes:
db_data:
networks:
net:
driver: bridge
这是检查docker网络的值。我正在将来自thankview容器的ipv4address硬编码到extra_hosts部分:
[
{
"Name": "thank-views_net",
"Id": "2cbeb86d05a042dc37e96394626ff1080e883616897af07ad0551d25ed9b57d3",
"Created": "2018-08-24T15:13:43.0327743Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.24.0.0/16",
"Gateway": "172.24.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"8238f9608e06a1ded6b6162039d5c366b466a42970084dbd3df8807bb9fc353c": {
"Name": "api",
"EndpointID": "793254eef92a8dc6ea481b3e5c04eb202c653b691c7473e39882b74c7412703b",
"MacAddress": "02:42:ac:18:00:04",
"IPv4Address": "172.24.0.4/16",
"IPv6Address": ""
},
"86c463136c0ccfec5b1bcebbc8f81fd52931f7e0ab6e4ed9aa8c40b7cd13dfb5": {
"Name": "api_mysql_api_1",
"EndpointID": "c74233b8d875c2ebf50d326742373f4406359fc95c61a1bdcb39b037fe24af19",
"MacAddress": "02:42:ac:18:00:03",
"IPv4Address": "172.24.0.3/16",
"IPv6Address": ""
},
"8aabf5a963cba2cce260f3e357a0c3fca1263e9aa2f536c8162bab3c392b2a54": {
"Name": "api_phpmyadmin_1",
"EndpointID": "fdadae8bf1210c83e45e56fe873730a10bfbb9460e9690f524f4240a71ea7823",
"MacAddress": "02:42:ac:18:00:05",
"IPv4Address": "172.24.0.5/16",
"IPv6Address": ""
},
"f85629b1ab72c99d15588381f05b2b40b9d31d5511c6ab5639b2b06d75303d6b": {
"Name": "thankview",
"EndpointID": "278408b8ccf17e1b5723e3c34ae4fe07c0928333161e7c7ef3d5dab8b3b2306e",
"MacAddress": "02:42:ac:18:00:02",
"IPv4Address": **"172.24.0.2/16"**,
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]