1

I am working on a python project and I need to create a docker image for testing. In my project I have a main.py file that has the statement from <module> import *. here represents a folder in which I put all the other files of my project. The dockerfile is as follows:

FROM continuumio/miniconda3

COPY parcellation_env.yml .
RUN conda env create -f parcellation_env.yml

SHELL ["conda", "run", "-n", "parcellation_env", "/bin/bash", "-c"]

COPY main.py .
ADD module/ .

ENTRYPOINT ["conda", "run", "-n", "parcellation_env", "python", "main.py", "-C", "white_matter_parcellation/config/config_point_autoencoder.yaml", "--use_splits", "-y"]

When creating the image, everything works fine, but when I try to run the image in a container I get a No module named "module" error, signaling that something went wrong.

I'm new to docker and this is my first time using it, does anyone know what went wrong?

Note: the error occurs both if I put ADD and COPY for module/, I already tried changing that


EDIT:

The folder structure of my project is currently the following

|--main.py
|--module
|--parcellation_env.yml
|--__init__.py
|----folder1
|------folder_1_files
|------folder_1_init
|----folder2
|------folder_2_files
|------folder_3...
|------folder_2_init
...

1 Answer 1

1

There are some few issues I could see ideally, if you are having module errors , it usually points to the fact that Python can not find specific module in question.

I think the best is to restructure how you are placing files via your docker file for python to use.

I have made few modifications and I hope it helps if there is anything I suspect might be not having a WORKDIR.

Make sure your structure is like this

├── Dockerfile
├── main.py
├── module or src/
│   ├── __init__.py
│   ├── files.py
├── parcellation_env.yml

here is a snippet of your docker file with some few mods.


FROM continuumio/miniconda3

# Copy the environment file and create the conda environment
COPY parcellation_env.yml .
RUN conda env create -f parcellation_env.yml

# Set the shell to use the conda environment
SHELL ["conda", "run", "-n", "parcellation_env", "/bin/bash", "-c"]

# Your working directory should include src/*/*/ etc. tests and the likes
WORKDIR /app

# copy the main script and the module directory to /app
COPY main.py /app/
ADD module/ /app/module/

ENV PYTHONPATH=/app


ENTRYPOINT ["conda", "run", "-n", "parcellation_env", "python", "main.py", "-C", "white_matter_parcellation/config/config_point_autoencoder.yaml", "--use_splits", "-y"]
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you for the answer, I tried it and it seems to now capture the module. The problem is, most likely, that the structure inside the module has some other submodules in it (e.g. there is a data subfolder in the module folder, with its own files and its own init). Are the ADD or the COPY command recursive? Or is there a way to specify that they should be recursive?
@Neskelogth to answer you question,ADD is more than just a specialist in copying folders although COPY only copies files, I would imagine if you have anything in that folder it will copy it. But ADD does more than just copy folders, the word recursive might not do justice to what ADD does in theory except you want to limit it(add) to the scope of folders then you might say that haha!. if you have time here is the url to add/copy docs it would give you more insights than I can to what they can do and can not do docs.docker.com/build/building/best-practices/#add-or-copy
PS: You are most welcome, let me know if it helped...
So, I tried to look at what you linked there, but unfortunately that does not help. In my case I have the structure of folders as
@Neskelogth do you mean it doesn't help you understand ADD/COPY?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.