Question

Creating docker containers for dev and prod with multiple postgres databases

I’m trying to create 2 docker containers one for dev and one for prod using docker-compose. The two containers should be linked to separate postgres databases. I tried the following, but it seems to create just one container and one database everytime.

docker-compose.yml

version: "3"
services:
  db:
    image: postgres:latest
    restart: always
    container_name: myinstance-postgres-database
    environment:
      - POSTGRES_USER= dbuser
      - POSTGRES_PASSWORD= dbpass
      - POSTGRES_DB= ProductionDB
   ports:
   - 127.17.0.1:5432:5432
   volumes:
   - myinstance-postgres-db:/var/lib/postgresql/data
  app:
    image: service/platform:latest
    restart: always
    container_name: prod-app
    environment:
      DB_SETUP: "true"
      DB_VENDOR: "postgresql"
      DB_HOST: db
      DB_USER: "dbuser"
      DB_PASSWORD: "dbpass"
      DB_NAME: "ProductionDB"
      DB_WAIT: 10
    ports:
    - 8443:8443
    volumes:
    - myinstance-postgres-git:/usr/local/tomcat/webapps/ROOT/WEB-INF/git
    depends_on:
    - db

volumes:
  myinstance-postgres-db:
  myinstance-postgres-git:

docker-compose.dev.yml

version: "3"
services:
  db:
    image: postgres:latest
    restart: always
    container_name: myinstancedev-postgres-database
    environment:
      - POSTGRES_USER= dbuser
      - POSTGRES_PASSWORD= dbpass
      - POSTGRES_DB= DevDB
   ports:
   - 127.17.0.1:5432:5432
   volumes:
   - myinstancedev-postgres-db:/var/lib/postgresql/data
  app:
    image: service/platform:latest
    restart: always
    container_name: dev-app
    environment:
      DB_SETUP: "true"
      DB_VENDOR: "postgresql"
      DB_HOST: db
      DB_USER: "dbuser"
      DB_PASSWORD: "dbpass"
      DB_NAME: "DevDB"
      DB_WAIT: 10
    ports:
    - 8444:8443
    volumes:
    - myinstancedev-postgres-git:/usr/local/tomcat/webapps/ROOT/WEB-INF/git
    depends_on:
    - db

volumes:
  myinstancedev-postgres-db:
  myinstancedev-postgres-git:

Then I run :

sudo docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d

as a result I have one container which is dev-app and only one database is created. Any solution ?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there @hsenoussi,

If you are running the two containers on the same server, I believe that the problem is due to the 2 containers running on the same ports.

I could suggest changing the port mapping for your dev container to something different so that you don’t get that conflict.

Regards, Bobby