0

I have a docker image that exposes 9000 port for server. After the server is running, I need to execute the 3 python scripts which depends on server so, they can only get executed after server.py is running however, after CMD command, the other code do not get executed and remains stuck. What are the possible suggestion to run 3 scripts in same container?

FROM python:3.7.3 as build

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . . 

# CMD [ "python", "./server.py" ]   (The following 3 scripts depends on server.py for execution)

RUN python /app/script1.py
RUN python /app/script2.py
RUN python /app/script3.py

EXPOSE 9000

CMD [ "python", "./server.py" ]
1
  • 1
    friendly reminder to mark an answer as "accepted"! Never too late :) Commented Nov 23, 2022 at 18:47

2 Answers 2

1

As written in the Dockerfile referece

There can only be one CMD instruction in a Dockerfile

The CMD instruction tells the container what its entry point is, and when running the container, that is what will be run.

If running python ./server.py is a blocking call (which I'm assuming it is, since it's called a server, and most likely responds to some kind of requests), then this won't be possible.

Instead, try restructuring your scripts so that they are run when the server is run, by doing everything you do in script1.py, script2.py, script3.py after the server has been started inside of server.py.

If instead this is about script1.py... sending requests to the server, I'd recommend not including those in the container. Instead, you can simply run those scripts, manually, from the terminal while the server container is running.

Sign up to request clarification or add additional context in comments.

Comments

0

You can just execute those scripts from the command line using docker exec after the container has started. You'll just need to know what the container name is

docker exec <CONTAINER NAME> python /app/script1.py
docker exec <CONTAINER NAME> python /app/script2.py
docker exec <CONTAINER NAME> python /app/script3.py

Or just make a bash script, say my_script.sh to run them all and just execute that

#!/usr/bin/env bash

docker exec <CONTAINER NAME> python /app/script1.py
docker exec <CONTAINER NAME> python /app/script2.py
docker exec <CONTAINER NAME> python /app/script3.py

And then

docker exec <CONTAINER NAME> ./my_script.sh

Comments

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.