简体   繁体   中英

How to run different python scripts in parallel containers from same image

I am trying to do something very simple (I think), I want do build a docker image and launch two different scripts from the same image in parallel running containers.

Something as simple as

Container 1 -> print("Hello")

Container 2 -> print('World")

I did some research but some techniques seem a little over engineered and others do something like

CMD ["python", "script.py"] && ["python", "script2.py"]

which, isn't what I'm looking for.

I would love to see something like

$ docker ps
CONTAINER ID        IMAGE            CREATED          STATUS               NAMES
a7e789711e62        67759a80360c   12 hours ago     Up 2 minutes         MyContainer1
87ae9c5c3f84        67759a80360c   12 hours ago     Up About a minute    MyContainer2

But running two different scripts.

I'm still fairly new to all of this, so if this is a foolish question, I apologize in advance and thank you all for working with me.

If you are set on using the same image then I would suggest you set the ENTRYPOINT to python and then use docker run command to start the containers by providing the scrips as the CMD like so:

Dockerfile:

FROM python

...

ENTRYPOINT ["python"]

And the use the docker run command like so

docker run -d my_image script.py && docker run -d my_image script2.py

Which would start two containers each running separate scripts

BUT - I like to keep the images clean in terms of not having any additional scripts or packages that are not necessary for my service to work, so in this case I would simply create two separate images each having one of the scripts and then run them similarly.

Example:

FROM python

COPY script.py script.py

ENTRYPOINT ["python"]

CMD ["script.py"]

And the second image:

FROM python

COPY script2.py script2.py

ENTRYPOINT ["python"]

CMD ["script2.py"]

And then just build them as separate images and run the the same way as before

Try these steps, it should work.

  • Create a Dockerfile with contents:
FROM python:3.7-alpine
COPY script1.py /script1.py
COPY script2.py /script2.py
CMD ["/bin/sh"]
  • In script1.py
print("Hello")
  • In script2.py
print("World")
  • Build docker image docker build -t myimage:v1 .
  • Run the containers
$ docker run -it --rm --entrypoint python myimage:v1 /script1.py
Hello
$
$ docker run -it --rm --entrypoint python myimage:v1 /script2.py
World
$

NOTE: Here we're using same docker image myimage:v1 and just changing the entrypoint in every docker run command.

More info here .

Hope this helps.

You can do this easily using Docker Compose . Here is a simple docker-compose.yml file just to show the idea:

version: '3'

services:
  app1:
    image: alpine
    command: >
      /bin/sh -c 'while true; do echo "pre-processing"; sleep 1; done'

  app2:
    image: alpine
    command: >
      /bin/sh -c 'while true; do echo "post-processing"; sleep 1; done'

As you see both services use the same image, alpine in this example, but differ in commands they run. Execute docker-compose up and see the output:

app1_1  | pre-processing
app2_1  | post-processing
app2_1  | post-processing
app2_1  | post-processing
app1_1  | pre-processing
app2_1  | post-processing
app1_1  | pre-processing
app2_1  | post-processing
...

In your case, you just change the image to your image, say myimage:v1 , and change the service commands so that the first service definition will have command: python script1.py line and the second one command: python script2.py .

Any command you put at the end of the docker run command (or the Docker Compose command: field) replaces the CMD in the Dockerfile. I would suggest still putting in some useful default CMD, but you can always just

docker run --name hello myimage python script.py
docker run --name world myimage python script2.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM