Docker Containers Won't run if I give them a port

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.

How are you assigning those ports?

Hi, I have tried lots of different ways. Creating new ones “docker run -d -p 60:60 --name devbox python”, changing the config files, and using the commit method.

And no errors, right?

Dus you also try with “non-privileged ports”, that is: port numbers greater than 1024?

Hi

Yes I have I will try again but the first one I tried was 8080 but I will try another non privileged.

Hi It’s giving me a 0 for my port and still exiting straight away.

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:

I strongly recommend to learn how to build images based on a Dockerfile.

1 Like

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?

Oh, I see if you run it using -t (allocate pseudo tty) and -d (run in detached so it doesn’t take over the command line) then it keeps it running. How couch I miss it. It’s so simple. I feel so silly.

But still Metin’s remark applies:

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.

Hi so does -it create a webserver? What exactly does the what exactly does the -it parameter do? I thought I was making it do work that’s the thing. Obviously I was wrong but as my brother said the other day, Docker is a whole new world.

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.

Hi, I thought that my dockerfiles were not exposing the right ports, but even if I do they have the same problem. Do I have to set up a simple script as a webserver using fast api or flask? They start and execute successfully for 5 minutes and then crash.

It seems you already created a new topic about this, Docker Image On Google Cloud Crashing After about 10 minutes?