Home > Net >  Running multiple python main files off Docker image
Running multiple python main files off Docker image

Time:09-21

I have created a Docker image with dockerfile where the Entrypoint is as follows:

ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "myproject", "python", "./myprojectmain.py", "--config", "./config.py"]

When I run I use the command:

docker run myproject

all is fine it seems. However I have a secondary .py file in the root of the project called setup.py. The purpose of this file is to update some of the config and json files after getting some input from the user.

Is there a way to run this secondary file (setup.py) or do I need to create a whole new image (which seems ridiculous).

Thanks

CodePudding user response:

Well... if you got an image, you don't have to use entrypoint... just run your scripts like this:

docker run image "python /some/path/myscript.py"

or

docker run image /bin/bash -c "cd /some/path && python myscript.py"

or with entry point

RUN ./myprojectmain.py --config ./config.py
RUN ./myproject2main.py --config ./config.py
ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "myproject", "python"]

CodePudding user response:

You can straightforwardly provide an alternate command after the image name in the docker run command. It's harder to override the entrypoint, though. If you have both a command and an entrypoint then they are combined together into a single command.

This workflow is easiest if your Dockerfile has a CMD, and that's a complete runnable shell command. If you have an ENTRYPOINT at all, it is some kind of wrapper that does some initial setup and then runs the command it's given as additional arguments. In this particular setup, conda run with its arguments seems to meet that need and have the correct form, so you could say

ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "myproject", "--"]
CMD ["python", "./myprojectmain.py", "--config", "./config.py"]

(Note that conda run seems to have some issues; you could probably simulate it using a custom entrypoint wrapper script or use a pip-based non-virtual-environment workflow instead.)

If you split the ENTRYPOINT and CMD like this, then you can run

docker run myproject \
  python setup.py

The alternate python setup.py command will be appended to the conda run entrypoint command.

... update some of the config and json files ...

It's often a good idea to inject these into your container using a bind mount. Depending on how exactly the files get set up, you may be able to initialize them from the host environment, without Docker

./setup.py
docker run -d -v $PWD/config:/app/config myproject

but if they are sensitive to the Docker environment in some way, you could do it in Docker too; make sure to mount the same configuration storage into both containers.

docker network create mynet
docker volume create config
docker run --rm --net mynet -v config:/app/config myproject ./setup.py
docker run -d -p 8000:8000 --net mynet -v config:/app/config myproject
  • Related