I am working on a python project that uses a conda environment. I got access to new resources for testing my project on a server but I can on ly run docker containers there. My supervisor told me to put in the docker container only the environment, but I'm new to docker so I have no idea on how to do it. Until now, I just put the environment and the code into the docker image and run everything from there, with this dockerfile
FROM continuumio/miniconda3
# needed to run docker container as non-root
ARG USER=<user>
ARG USER_ID=<userid>
ARG USER_GROUP=<userGroup>
ARG USER_GROUP_ID=<userGroupId>
ARG USER_HOME=/home/${USER}
RUN groupadd --gid $USER_GROUP_ID $USER && useradd --uid $USER_ID --gid $USER_GROUP_ID -m $USER
RUN apt-get update && apt-get install -y curl
COPY env.yml .
RUN conda env create -f env.yml
SHELL ["conda", "run", "-n", "env", "/bin/bash", "-c"]
WORKDIR /app
COPY main.py /app/
COPY src/ /app/src/
ENV PYTHONPATH=/app
USER $USER
ENTRYPOINT ["conda", "run", "-n", "env", "python", "/app/main.py", "-C", "src/configs/config.yaml", "--use-splits", "-y"]
In this way, I include the code in the file, but I do not understand how it is possible to use a docker container only as environment. How should I modify the dockerfile to make it work? And how should I run the script telling it to use the docker container as environment?
If I understand well, you've already built your docker image. If it's not the case, and assuming your are in the folder containing your Dockerfile, simply do:
docker build -t <image name> .
To run your script inside a docker container, the idea is NOT to tell your script that is should run inside it. Conversely, you need to create a docker container and tell it to run you script.
docker run --rm <image name>
)conda run -n env python" /app/main.py ...
)However, you specified an entrypoint inside your Dockerfile. This command will automatically be executed whenever your do:
docker run --rm <image name>
However, I think you should use this entrypoint instead:
ENTRYPOINT ["conda", "run", "-n", "env", "python", "/app/main.py"]
It will allow you to change the options you pass to the script, without having to re-build the image again. You could then run your script in your container with:
docker run --rm <image name> -C src/configs/config.yaml --use-splits -y
In addition, I would suggest to run your container with a volume mapping. This way, you won't have to re-build your project everytime you make modifications in your source code. Assuming your project is like:
my_project
|__ main.py
|__ src/
Simply go to your project root folder and do:
docker run --rm --volume $(pwd):/app <image name> <your script options>
or whatever options you need to pass.
To sum it up
I would change the entrypoint as stated above, and run your container like:
docker run --rm --volume $(pwd):/app <image name> -C src/configs/config.yaml --use-splits -y