skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    # Stage
    docker-compose --project-name stage -f docker-compose.yml -f docker-compose.prod.yml up --build
    
    # Production
    docker-compose --project-name prod -f docker-compose.yml -f docker-compose.prod.yml up --build
    

    I am also open to other solutions


  2. You need to specify different port bindings as well:

    # docker-compose.stage.yml
    version: "3.3
    
    services: 
      api:
        container_name: api_stage
        ports:
            - "8001:8000"
      environment:
        - environment="staging"
    
      db:
        container_name: db_stage
        ports:
            - "xxxY:xxxx"
      environment:
        - environment="staging"
      volumes:
        - /I/Mount/a/local/volume/stage:/container/volume
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search