Roger Mukai
Roger Mukai

Reputation: 33

Do you need to update the path in the dockerfile from "/usr/src/app/"?

Defaults in the dockerfile and/or docker-compose.yml

Hi all, this might be an obvious question but a lot of the learning materials I have come across use this path /usr/src/app/.

Here is the example dockerfile I am looking at now:

# pull official base image
FROM python:3.8.2-alpine

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# copy project
COPY . /usr/src/app/

As you can see it's fairly generic. I have made the assumption that I need to be updating those lines with my path. Here is what my updated dockerfile looks like.

My question is do I need to be updating the path or do can I leave it alone?

# pull official base image
FROM python:3.8.2-alpine

# set work directory
WORKDIR /home/$USER/Practice/Project_Enviroment/PROJECT_ROOT_DIRECTORY/

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
    && apk add postgresql-dev gcc python3-dev musl-dev

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /home/$USER/Practice/Project_Enviroment/PROJECT_ROOT_DIRECTORY/requirements.txt
RUN pip install -r requirements.txt

# copy entrypoint.sh
COPY ./entrypoint.sh /home/$USER/Practice/Project_Enviroment/PROJECT_ROOT_DIRECTORY/entrypoint.sh
RUN chmod +x /home/$USER/Practice/Project_Enviroment/PROJECT_ROOT_DIRECTORY/entrypoint.sh

# copy project
COPY . /home/$USER/Practice/Project_Enviroment/PROJECT_ROOT_DIRECTORY/

# run entrypoint.sh
ENTRYPOINT ["/home/$USER/Practice/Project_Enviroment/PROJECT_ROOT_DIRECTORY/entrypoint.sh"]

Upvotes: 3

Views: 1413

Answers (1)

David Maze
David Maze

Reputation: 159393

Each container and each image has an isolated filesystem, so it's just fine to have the same path in every image even for different projects. I tend to use just /app. There's no reason to use a very long-winded path like in your last example, and the container paths don't need to match the host paths.

For simplicity, I also tend to make the right-hand side of COPY instructions relative paths. These are interpreted relative to the current WORKDIR, but that means, whatever path I pick, I only need to write it once.

FROM python:3.8.2-alpine
WORKDIR /app                                # or whatever other path you choose
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
RUN pip install --upgrade pip
COPY ./requirements.txt ./requirements.txt  # note, relative path on RHS
RUN pip install -r requirements.txt
COPY . .                                    # note, relative path on RHS
CMD ["/app/main.py"]                        # interpreted at runtime

You'll also see occasional SO questions that put install-time paths in environment variables. There's no particular benefit to doing this, since the paths are fixed at image build time.

Upvotes: 5

Related Questions