I am deploying, on 3 different environments (test, stage & production) an API.
I am used to deploy with docker-compose so I wrote 2 services (1 for my API and 1 for a database) like following:
# file docker-compose.yml
version: '3.3'
services:
api:
build:
context: ..
dockerfile: Dockerfile
image: my_api:${TAG}
ports:
- "${API_PORT_FROM_ENV}:8000"
env_file: .env
depends_on:
- db
db:
image: whatever:v0.0.0
ports:
- "${DB_PORT_FROM_ENV}:5000"
env_file:
- .env
In the file above, you can find the parent services.
Thne, I wrote 2 files that explains my strategy to deploy on the same Virtual Machine my containers:
-> staging environment below
# docker-compose.stage.yml
version: "3.3
services:
api:
container_name: api_stage
environment:
- environment="staging"
db:
container_name: db_stage
environment:
- environment="staging"
volumes:
- /I/Mount/a/local/volume/stage:/container/volume
-> production environment below
# docker-compose.prod.yml
version: "3.3
services:
api:
container_name: api_prod
environment:
- environment="production"
db:
container_name: db_prod
environment:
- environment="production"
volumes:
- /I/Mount/a/local/volume/prod:/container/volume
My problem:
The production is actually running.
I deploy my containers with the following command:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up --build
I want to deploy a staging environment on the same virtual machine. I want my api_prod + db_prod running in parallel with api_stage + db_stage.
Unfortunatly, when I run the command:
docker-compose -f docker-compose.yml -f docker-compose.stage.yml up --build
My containers called api_prod and db_prod stops. Why?
2
Answers
I found the solution:
I need to specify --project-name option that allows me to run both containers from stage and production environment without concurrency.
Below the 2 commands:
I am also open to other solutions
You need to specify different port bindings as well: