Reputation: 321
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" ]
Upvotes: 0
Views: 2472
Reputation: 81
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.
Upvotes: 1
Reputation: 2115
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
Upvotes: 0