Containers and Sockets

I need some help has to how to get a container not to stop based on the socket. I read this can be done by this command; docker run -d -p 8080:8080 IMAGE_NAME nc -l -p 8080. Although what I do understand is that every container has a default socket, How can you take the default socket or another socket and keep a container running until that socket is exited ?

Not sure where you got the idea every container has a default socket. Docker itself uses a socket for control commands from the CLI.

A container usually runs a single process. If it’s listening on a port, it should stay active all the time, like a web server.

If I understand correctly, you want to make the container lifetime depend on it, there I see a challenge. It would mean you need to give it a initial delay during start after opening the port, so a controlling connection can be established, before the container shuts down again.

You probably need your own custom logic in a process, that starts listening, and only exits after a first connection has been established and is lost. Even more reliable would be to wait for a command to exit.

should keep your container running, but it depends on the image and the entrypoint script used.

The command nc -l -p 808 will keep the container running, and how will it exit ?

Please help us to make sense of this topic and your posts. We do know the mechanics of docker, but we are no mind readers. Please share the context knowledge you kept to yourself, so we know the full picture and can have a chance to give good recommendations.

Usually it’s better to not ask about “the problem of the minute”, but instead share the big picture. Otherwise, you end up jailing us into whatever you think must be the solution, without giving us the chance to share how a recommended solution could look like.

It will not exit. Like a web server will not exit. You need to terminate the container. Or connect to it and kill the process or shut it down.

When running netstat, Any unconnected ports those can be assigned to the container ?

This is still not the big picture. Inside a container, you can bind whatever port you like (at least with recent kernel and docker versions). Since you didn’t share the big picture: why would netstat be relevant here?

Yes. Any free port on the host can be assigned to a container. It can be forwarded to an internal port your application is listening on.