Search code examples
dockerdocker-composedockerfile

Dockerfile is not copying from the context directory (as defined in docker-compose)


I have a Dockerfile and a docker-compose that work when in the same directory. However I'm trying to make an orchestration docker-compose which will build multiple images from different repositories.

My folder structure looks like this:

my-apps/
my-apps/service-orchestration
my-apps/service-orchestration/docker-compose.yaml
my-apps/service-one # this is the root directory of the service-one app
my-apps/service-one/Dockerfile
my-apps/service-two
...

The goal is to set the context to the root directory of service-one. This seems to be working, because I can see requirements.txt being installed and I see the env vars in the image. However, by the time everything is built, I no longer see the contents of service-one in the docker image. Instead I see the contents of service-orchestration

services:
  service-one:
      image: service-one
      build:
        context: ../service-one
      command: uvicorn service-one.asgi:application --host=0.0.0.0 --port=8001 --reload
      ports:
        - "8001:8001"
      # depends_on:
      #   - db
      env_file:
        - ../service-one/.env
      volumes:
        - .:/app
      stdin_open: true
      tty: true

Here's the Dockerfile inside of service-one/

FROM python:3.11

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY ./requirements.txt /app/
COPY ./entrypoint.sh /app/

RUN pip install --upgrade pip && pip install -r requirements.txt # this works!

COPY ./ /app/

CMD ["gunicorn", "service-one.asgi:application", "--bind", "0.0.0.0:8001", "-k", "uvicorn.workers.UvicornWorker"]

ENTRYPOINT ["/app/entrypoint.sh"]

Can anyone see a problem with this approach? I have no idea why the image doesn't have the contents of service-one/


Solution

  • Your issue is due to the volume, which maps the current directory to the container.

    The build context is only used during build. Not when running the container.

    Change your volume mapping to

    volumes:
      - ../service-one:/app
    

    and it should work.