2

I have a working Docker-Compose on my local server Now that I have to go to prod, I just copied the whole directory on my prod server, same directory structure, config file etc... But when I run the docker-compose command (docker-compose -f docker-compose.dev.yml up) it throws me this error :

Step 7/28 : COPY ../../data_acquisition_survey_api/ . ERROR: Service 'web' failed to build: COPY failed: forbidden path outside the build context: ../../data_acquisition_survey_api/ ()

I went through all google available threads but with no luck, tried Docker build, docker context etc.... but nothing works, any suggestion?

Thanks !!

Folder structure :

server/docker/api
server/docker/db
server/docker/nginx

server/docker-compose.dev.yml file details:

version: '3.7'

services:
  web:
    build:
      context: .
    dockerfile: docker/api/Dockerfile.dev
    command: gunicorn data_acquisition_survey_api.wsgi:application --bind 0.0.0.0:8000 --reload
    expose:
      - 8000
    volumes:
      - .:/home/server/web
    env_file:
      - docker/api/.env
    depends_on:
      - db
  db:
    build:
      context: ./docker/db
      dockerfile: Dockerfile
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - docker/db/.env
    environment:
      - "POSTGRES_INITDB_ARGS=-E UTF8"
  nginx:
    build:
      context: ./docker/nginx
      dockerfile: Dockerfile.dev
    ports:
      - 80:80
    depends_on:
      - web
    environment:
      TZ: Europe/Paris

volumes:
  postgres_data:

server/docker/api/Dockerfile.dev as follow:

###########
# BUILDER #
###########

# pull official base image
FROM python:3.7-slim-buster as builder

# set work directory
WORKDIR /usr/src/server

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apt-get update \
    && apt-get -y install postgresql-server-dev-all gcc python3-dev musl-dev

# lint
RUN pip install --upgrade pip
COPY ../../data_acquisition_survey_api/ .
COPY ../../manage.py .

# install dependencies
COPY ../../docker/api/requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/server/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.7-slim-buster

# create directory for the app user
RUN mkdir -p /home/server

# create the app user
RUN adduser --system --group server

# create the appropriate directories
ENV HOME=/home/server
ENV APP_HOME=/home/server/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

# install dependencies
RUN apt-get update && apt-get -y install libpq-dev
RUN apt install -y netcat

COPY --from=builder /usr/src/server/wheels /wheels
COPY --from=builder /usr/src/server/requirements.txt .
RUN pip install --no-cache /wheels/*

COPY ../../docker/api/entrypoint.sh $APP_HOME
RUN chmod +x $APP_HOME/entrypoint.sh

# copy project
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R server:server $APP_HOME

# change to the app user
USER server
Thierry B
  • 23
  • 1
  • 3

1 Answers1

5

You're almost there. Whenever you define a context for your docker build, the context's directory becomes the base directory for your Dockerfile. So, wherever you have a path of the type:

COPY ../../path/of/interest

in your Dockerfile, you should remove the relative parent dir paths:

COPY path/of/interest

And you should be good to go.

amiasato
  • 914
  • 6
  • 14
  • 1
    ...and furthermore, if the `COPY` path starts with `..`, Docker removes that; the source for a `COPY` instruction is _always_ inside the build-context directory. – David Maze Nov 15 '21 at 14:13
  • 2
    The weird thing is that it works with the relative paths on my local server. I did remove them and replaced by ./path and it worked! Now I don't know if this happens at first launch only, when folders are created. Anyway, thanks a lot for your support! It solved my issue! – Thierry B Nov 15 '21 at 16:11
  • anyone know how to change the context when using VSC + devcontainer.json? – mike01010 Jun 02 '23 at 22:17