Hi every time I assign a port to a container the container will exit as soon as it is started.
But containes without ports still run fine and any container I download that already has a port assigned runs fine. What could I be doing wrong?
I don’t get any error messages and I’m following tutorials to the letter.
I just used this command before I set up this comment and “docker context ls” as I thought it might help but I think it’s why I’m getting a 0, is there a way to undo it? Ok the 0 was just a snaffoo but I think the “docker context ls” was still a mistake. I did manage to get the ports showing they still are not starting though.
It is highly unlikely that
docker context ls
is the cause.
It realy just lists the contexts (~=execution environment for local or remote docker engins) you registered in your docker command line client.
Please share the exact commands of what you do, otherwise it will remain a guessing game. Also it might be relevant, if you try to run a windows or linux container.
It is perfectly normal that
docker run -d -p 60:60 --name devbox python
would create a container that immediatly finishes, as there is no process started that keeps the container running.
For the sake of testing, try
docker run --rm -p 60:60 --name test python
and then try
docker run --rm -it -p 60:60 --name test python
.
Do you see a difference in both’s behavior?
Also:
bnbutler:
changing the config files, and using the commit method.
I strongly recommend to learn how to build images based on a Dockerfile.
Hi your method worked, I thought it was ok to build containers from images such as the official python image? I haven’t used the -it command before. By using the command in this way "docker run -d -p 60:60 --name devbox python” how is there no process that keeps the container running? What is the process that keeps the container running?
meyay:
It is perfectly normal that
docker run -d -p 60:60 --name devbox python
would create a container that immediatly finishes, as there is no process started that keeps the container running.
So, if you want a Python container to keep running without using
-it
, simply make it do some actual work. Like, given that you were trying to publish a port, make it start a web server that listens to that port.
A container is not a vm. Please consult google on the differences, as there are likely hundred or thousands of blog posts that address the differences.
It container realy just starts the process that is declared as ENTRYPOINT and uses whatever is declared as CMD as argument(s) to it. If ENTRYPOINT is absent, it will start the process that is declared in CMD directly.
In your case, the python image has
CMD ["python3"]
declared in its Dockerfile (you can see the history of instruction applied in the Dockerfile for the latest tag
here
), which makes the container execute
python3
on container start. As python3 has litteraly nothing to do it finishes and the container stops. A container needs a foreground process to be kept running,
Like I already wrote: please make yourself aquinted with how to build images with a Dockerfile and use a COPY instruction to copy your application code inside and adjust the ENTRYPOINT/CMD so that it actualy executes your stuff.
Most of the questions you have, can actualy only be answered by yourself, as you are the one creating the image.
Note: I highly recommend to invest some time in this
excellent free self-paced docker training
. It will provide you a solid understanding how docker works and how things are done.
Docker Desktop for Mac(Intel Chip)
Docker Desktop for Mac(Apple Chip)
Docker Desktop for Windows
Docker Desktop for Linux