Docker-compose: Creating same container for different projects

Question:

I’m facing an issue with my Django projects that have a similar structure. Each project has a backend folder containing a docker-compose.yml and Dockerfile.
The structure is as follows:
project1/backend/docker-compose.yml project2.backend/docker-compose.yml

The problem arises when I switch between projects and run docker-compose up. It seems that the container named backend is reusing services from the previous project instead of creating a new container.

Here’s an example of the docker-compose.yml for each project:

Project 1:

version: '3.9'

services:
  redis:
    image: redis:latest
    ports:
      - "6379:6379"

  postgres:
    image: postgres:12
    container_name: postgres
    environment:
      POSTGRES_PASSWORD: project_name
      POSTGRES_DB: project_name
    env_file:
      - .env
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data/

  celery:
    build:
      context: .
      dockerfile: Dockerfile
    command: celery -A project_name worker -l info
    volumes:
      - .:/opt/webapp
    depends_on:
      - redis
      - postgres

  celery-beat:
    build:
      context: .
      dockerfile: Dockerfile
    command: celery -A project_name beat -l info
    volumes:
      - .:/opt/webapp
    depends_on:
      - redis
      - postgres

volumes:
  postgres_data:

Project 2:

version: "3.9"

services:
  web:
    build:
      context: .
      args:
        SECRET_KEY: ${SECRET_KEY}
    env_file: .env
    volumes:
      - ./:/opt/webapp
    ports:
      - "8000:8000"
    # Added Command for hot reloading in dev server
    command: >
      sh -c "python3 manage.py runserver 0.0.0.0:8000"

  postgres:
    env_file: .env
    environment:
      POSTGRES_PASSWORD: project_name
    volumes:
      - postgres-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"

  redis:
    env_file: .env
    ports:
      - "6379:6379"

volumes: 
  postgres-data:

Here’s an example of the Dockerfile for each project

Project1:

# Use Python 3.11 slim image
FROM python:3.11-slim as base

# Install system dependencies
RUN apt-get update 
  && apt-get install -y --no-install-recommends python3-dev libpq-dev gcc 
  && rm -rf /var/lib/apt/lists/*

# Create and set working directory
WORKDIR /opt/webapp

# Install Pipenv
RUN pip3 install --no-cache-dir -q 'pipenv==2023.12.0'

# Copy Pipfile and Pipfile.lock
COPY Pipfile Pipfile.lock /opt/webapp/

# Install project dependencies
RUN pipenv install --deploy --system

# Copy the rest of the application
COPY . /opt/webapp

# Separate stage for release
FROM base as release

# Copy built files from the base stage
COPY --from=base /opt/webapp /opt/webapp

# Set environment variables and collect static files
ENV PATH=/root/.local/bin:$PATH
ARG SECRET_KEY
RUN python3 manage.py collectstatic --settings=project_name.settings.development

# Default command to run the server
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Project2:

FROM python:3.8-slim as base

# libpq-dev and python3-dev help with psycopg2
RUN apt-get update 
  && apt-get install -y --no-install-recommends python3-dev libpq-dev gcc curl 
  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false 
  && rm -rf /var/lib/apt/lists/*
 
WORKDIR /opt/webapp
COPY Pipfile* /opt/webapp/

RUN pip3 install --no-cache-dir -q 'pipenv==2018.11.26'
RUN pipenv install --deploy --system
COPY . /opt/webapp

FROM base as release

COPY --from=base /root/.local /root/.local
COPY --from=base /opt/webapp/manage.py /opt/webapp/manage.py


WORKDIR /opt/webapp
ENV PATH=/root/.local/bin:$PATH
ARG SECRET_KEY 
RUN python3 manage.py collectstatic --no-input

# Run the image as a non-root user
RUN adduser --disabled-password --gecos "" django
USER django
CMD waitress-serve --port=$PORT project_name.wsgi:application

I want to ensure that each project creates its own separate container instead of reusing the existing backend container. How can I achieve this while maintaining the current project structure? Any insights or suggestions would be greatly appreciated. Thanks!

I have given container names for each service but it has not helped me, I want an idea of how to differentiate these two projects and create different containers for each project.

Asked By: Tarang Patel

||

Answers:

Try renaming services/container names in docker-compose.yml file, like "project1_postgres:…", "project1_redis:…".

I believe you can also "docker-compose down -v" to remove anonymous volumes, but I think the startup time will be longer.

If this doesn’t work, you can also try to add a network to your specific project, so that services will speak to eachother only if they are in the same network

networks:
  custom_network:
    driver: bridge

services:
  redis:
    image: redis:latest
    ports:
      - "6379:6379"
    networks:
      - custom_network
Answered By: Adrian Kurzeja