Can't we use the existing container (instead of the new container which will be created by Docker)?

Can’t we use the existing container (instead of the new container which will be created by Docker)?

Hi @saikrishna248

Can you try and be a bit more specific?
The point of containers is that you are able to remake them, fx. if a newer version is to be deployed.

But if you just want to use the same container all the time.

Can you do it and will it work? sure.
Is it recommended? no :slight_smile:

Hi @terpz
How to use the Same container again & again? Do we have any documentation for that? If yes could you share with me, it will be helpful for me as I am new to Docker & just started learning.

Cheers,
Sai Krishna M.

@saikrishna248 You can use

docker container ls --all

to see the container that already are created. You can then restart a stopped container by issuing the

docker container start CONTAINER_ID

command. You can use

docker container --help

to see the other commands useful for controlling containers, but it sounds like you would benefit from and enjoy the docker tutorial available in their online documentation available here: Orientation and setup | Docker Documentation

It is possible, but considered bad practice:

It depends on how and what you’re trying to achieve, if you only want to start/stop, then as @toyfrog (love the name btw) is pointing you, you can use docker container start/stop (or just docker start/stop container_id)

If you want to access your running container, you can use something like

  docker exec -ti container_id sh

But this is very bad practice, you should make none changes to a running container, if you are, then you need to create a better image

1 Like

A new Docker feature, Docker Development Environments is based on the premise of stopping an existing container, passing it to someone, and having them start it up again. Therefore you are not able to say that it is bad practice, but it is not the way to deploy containers in certain use cases.

My understanding is that the objective is to create an image of a container, in order to create new containers of that container based image.

Every image not created in an automated way is bad practice for me. As you lack reproducability, reliablity and automated builds…

Though, if it’s just about a single container that is stopped, in fact restarting it is an option. If this was the objective, then I got it all wrong in my previous response.

That is how I heard it described in a live webcast, where the initial container container the docker-compose.yml file, or the Dockerfile, and is then used to create the environment, but I have also seen descriptions that it is intended to be used to create environments where “all of the dependencies are already installed in a container, and you send that container to someone else so that can collaborate without having to go through the entire build process, etc” (paraphrase).

@saikrishna248 If you do end up looking over the docker documentation link I provided, can you confirm with us whether you mean using an existing container versus an existing image? These are very related, although different, terms for components in the docker ecosystem.

Hey @terpz , thanks!

This doesn’t make it less of a bad practice :slight_smile:

for someone with limited knowledge about oci containers, this might appear to be a good idea.
Someone experienced would neiter use docker commit, nor docker export.

My approach is “if you have to do something, at least do it properly”. Though, then again I am automation nerd…

Hey, @toyfrog Thanks for the information.
On the docker run command, we are providing only Image Name while executing the command so when I run, it creates a new container for each run. My doubt is anyway the first time if we run, one container(let’s say the name of this container is Container 1) will create under the image. Can’t we use the same existed container (i.e, Container 1) when we run the second time?

@saikrishna248
Can you provide some background on the image or container you are using?

Containers stop for various reasons; maybe it ran into an error and stopped, maybe the container was not able to startup properly because your image was not properly defined. In these cases you would not really want to restart the container, you would want to start a new container (hopefully after fixing the problem in the image build definition aka “Dockerfile”). But even containers that stop without any errors are often not reused. For this reason, and definitely a few other reasons not mentioned here, others in this thread have suggested that the “Best Practice” is to start up a new container each time you need one.

But there can also be reasons to manually “pause” a container, and then start it back up again. If your container has stopped without errors, and if you are really really sure you want to follow the Docker anti-pattern, then you can use the commands I provided in my previous post:

# List all containers, including stopped containers
docker container ls --all
# Use the container ID from the above command to restart the container
docker container start CONTAINER_ID

If your reason to want to reuse containers is that you are worried about disk space, or something like that, then you can use the “–rm” flag in the “docker run” command that creates “CONTAINER1” and when a container stops for any reason it will be removed automatically.

If your reason to want to reuse a container is because you have persisted some data inside of the container, then you should read about docker volumes in the documentation.