我目前正在开发一个 Maven 项目,我希望有 2 个 docker 容器,其中一个启动所有测试,另一个在所有测试成功时编译项目。
问题是两个容器都启动了 prod dockerfile。
所以我的问题是为什么在 docker compose 中指向每个 Dockerfile 之后它们都从 prod 开始
docker-compose.yml:
version: '3.8'
services:
db:
container_name: db
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: cuisine
MYSQL_ROOT_PASSWORD: example
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
ports:
- "3306:3306"
volumes:
- ./docker/mysql-dump/cuisine:/docker-entrypoint-initdb.d
- mysql:/var/lib/mysql
adminer:
image: adminer
restart: always
ports:
- 8080:8080
test:
container_name: java-test
image: spring-boot
build:
context: .
dockerfile: docker/test/Dockerfile
ports:
- "8081:8081"
- "5005:5005"
depends_on:
db:
condition: service_healthy
volumes:
- ${APPLICATION_ROOT_FOLDER}:/usr/src/mymaven
- ${MAVEN_SETTINGS_FOLDER}:/root/.m2
java:
container_name: java
image: spring-boot
build:
context: .
dockerfile: docker/prod/Dockerfile
ports:
- "8082:8082"
- "5006:5006"
depends_on:
db:
condition: service_healthy
volumes:
- ${APPLICATION_ROOT_FOLDER}:/usr/src/mymaven
- ${MAVEN_SETTINGS_FOLDER}:/root/.m2
volumes:
mysql:
当您的服务上同时有
build:
和 image:
时,构建的映像会标有 image:
名称。两个服务具有相同的 image:
名称,因此最后构建的服务“获胜”并为这两个服务运行。删除两者的 image:
名称。