thatayster
thatayster

Reputation: 55

How can I build a docker image with it parent folder as workdir?

This probably is a dumb question, but I'm newbie in Docker and I'm struggle with this. I have a project with many subfolders, like the example below:

project-folder:
       folder_1:
           code1.py
           Dockerfile
           requirements.txt
       folder_2:
           code2.py
           Dockerfile
           requirements.txt
       folder_data:
           file1
           file2
           file3

Then, I would like to do this:

  1. Maintain my workdir in project-folder for all containers;
  2. Inside each container, I should be able to access the folder_data - I know that I have to specify a volume, I just don't know how to do this without keep my project-folder as workdir;
  3. I need to pass my workdir (project-folder) to my code1.py

Note: my image was created successfully only when I create it within each subfolder, like this Dockerfile:

FROM python:3.6-slim
COPY . /folder_1
WORKDIR /folder_1
RUN pip install -r requirements.txt
CMD ["python3", "code1.py", "$(pwd)"]

Image creation comand:

docker build -t image_folder1 .

I am currently creating the image in the context of folder_1, because I was unable to create the image correctly in the context of the project-folder

Upvotes: 1

Views: 9839

Answers (1)

David Maze
David Maze

Reputation: 158996

The . argument at the end of the docker build command is the context directory; anything you COPY into your image must be within this subtree. If you need to include content in your image outside its immediate subtree, then you need to use an ancestor directory as the build context, but you can then use the docker build -f option to name a file in the subdirectory.

cd project-folder
docker build -t image_folder1 -f folder_1/Dockerfile .

Within the Dockerfile, since you're starting at the parent directory, you need to include the relative path to any files you COPY in; but, that's now allowed to include what previously would have been sibling directories.

FROM python:3.6-slim
WORKDIR /app

# Good practice to copy just dependencies first; this helps
# layer caching when you repeat `docker build`.
# COPY source path is relative to the `docker build` directory,
# which we've set up to be the parent of the application source.
COPY folder_1/requirements.txt .
RUN pip install -r requirements.txt

# Now copy in the rest of the application
COPY folder_1 .

# And also copy the shared data
COPY folder_data ./folder_data

# And have ordinary container startup metadata
CMD ["./code1.py"]

Do not use volumes here. Docker has a tempting behavior of populating a named volume from image content, but the old volume content will take precedence over updated image content if you rebuild, and this only works with native Docker volumes (not host-directory bind-mounts, not at all in Kubernetes). It's better practice to have a self-contained image that includes everything the application that needs to run than to have a partial image that requires key content to be injected from outside.

Upvotes: 7

Related Questions