0

I have project running on docker container in staging mode, actually I have two compose files, for staging and production in the same directory. At the moment stagin.yml is running, when I want to run production.yml , staging.yml stoppes running.
staging.yml

version: '3'

volumes:
  staging_postgres_data: {}
  staging_postgres_data_backups: {}
  staging_staticfiles: {}
  staging_mediafiles: {}

services:
  django:
    build:
      context: .
      dockerfile: ./compose/staging/django/Dockerfile
    image: project_staging_django
    depends_on:
      - postgres
      - redis 
    env_file:
      - ./.envs/.staging/.django
      - ./.envs/.staging/.postgres
    volumes:
      - staging_mediafiles:/app/project/media
    command: /start

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    image: project_staging_postgres
    volumes:
      - staging_postgres_data:/var/lib/postgresql/data
      - staging_postgres_data_backups:/backups
    env_file:
      - ./.envs/.staging/.postgres

  bot:
    image: project_local_bot
    container_name: project_local_bot
    build:
      context: .
      dockerfile: ./compose/staging/pytelegrambot/Dockerfile
    volumes:
       - ./bot:/bots:z
    env_file:
      - ./.envs/.staging/.bot
    command: /start

  nginx:
    restart: unless-stopped
    build:
      context: .
      dockerfile: ./compose/staging/nginx/Dockerfile
    container_name: project_staging_nginx
    ports:
      - 3080:80
      - 3443:443
    volumes:
      - staging_mediafiles:/app/project/media
    depends_on:
      - django

  redis:
    image: redis:6

production.yml

version: '3'

volumes:
  production_postgres_data: {}
  production_postgres_data_backups: {}
  production_mediafiles: {}

services:
  django:
    build:
      context: .
      dockerfile: ./compose/production/django/Dockerfile
    image: project_production_django
    container_name: project_production_django
    depends_on:
      - postgres
      - redis
    env_file:
      - ./.envs/.production/.django
      - ./.envs/.production/.postgres
    volumes:
      - production_mediafiles:/app/project/media
    command: /start

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    image: project_production_postgres
    container_name: project_production_postgres
    ports:
      - 5433:5432
    volumes:
      - production_postgres_data:/var/lib/postgresql/data
      - production_postgres_data_backups:/backups
    env_file:
      - ./.envs/.production/.postgres

  nginx:
    restart: unless-stopped
    build:
      context: .
      dockerfile: ./compose/production/nginx/Dockerfile
    image: project_production_nginx
    container_name: project_production_nginx
    ports:
      - 8080:80
      - 8043:443
    volumes:
      - production_mediafiles:/app/project/media
    depends_on:
      - django
    
  bot:
    image: project_production_bot
    container_name: project_production_bot
    build:
      context: .
      dockerfile: ./compose/production/pytelegrambot/Dockerfile
    volumes:
       - ./bot:/bots:z
    env_file:
      - ./.envs/.production/.bot
    command: /start

  redis:
    image: redis:6
    container_name: project_production_redis
    ports:
      - 6378:6379

All ports are different and volumes are different I guess, I don't understand what is happening. Thanks in advance.

mirodil
  • 422
  • 2
  • 8
  • 21
  • Compose associates containers with a "project", by default named after the current directory. You need a `docker-compose -p` option to cause the two runs to be in different Compose projects and not conflict with each other. – David Maze Mar 06 '23 at 15:26
  • (Most things in Compose can have automatic names that include the project name. You should not need to manually set `container_name:`, nor `image:` if you're not planning to push the built images, nor do you need to include "production" as part of the volume names. A typical best practice is to try to run the same image in all environments and not build a different image per environment.) – David Maze Mar 06 '23 at 15:27
  • @DavidMaze How can I run the same image if I use different .envs and different configs ? How to control them separetely ? – mirodil Mar 06 '23 at 17:09
  • 1
    Use different `environment:` variables in each environment, or use `volumes:` to inject a configuration file. Using [Multiple Compose files](https://docs.docker.com/compose/extends/#multiple-compose-files) could be one way to have settings mostly the same, except for an override file per environment. – David Maze Mar 06 '23 at 18:37

0 Answers0