-1

I am new to Docker and just getting started. I pulled a basic ubuntu image and started a few containers with it and stopped them. When I run the command to list all the docker containers (even the stopped ones) I get an output like this:

> docker container ls -a
   
CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS                      PORTS     NAMES
099c42011f24   ubuntu:latest   "/bin/bash"   6 seconds ago    Exited (0) 6 seconds ago              sleepy_mccarthy
dde61c10d522   ubuntu:latest   "/bin/bash"   8 seconds ago    Exited (0) 7 seconds ago              determined_rosalind
cd1a6fa35741   ubuntu:latest   "/bin/bash"   9 seconds ago    Exited (0) 8 seconds ago              unruffled_lichterman
ff926b6eba23   ubuntu:latest   "/bin/bash"   10 seconds ago   Exited (0) 10 seconds ago             cool_rosalind
8bd50c2c4729   ubuntu:latest   "/bin/bash"   12 seconds ago   Exited (0) 11 seconds ago             cranky_darwin

My question is, is there a reason why docker does not delete the stopped containers by default?

Marco Polo
  • 113
  • 1
  • 6
  • 2
    For instance, because someone may want to restart it ... – derpirscher Mar 29 '21 at 14:50
  • 2
    Why would you want docker to delete the stopped containers? That would be like taking time to setup a new computer and then resetting it to factory settings any time you wanted to turn it off for the day. – CWSites Mar 29 '21 at 14:50
  • You need to use `docker container prune -f` to remove all stopped container. You could make a script to stop all the containers and delete them. – J-Dumas Mar 29 '21 at 14:51
  • 2
    If you want your container to be automatically removed, once stopped, you can start it with the `--rm` flag https://docs.docker.com/config/pruning/#prune-containers – derpirscher Mar 29 '21 at 14:53
  • 1
    @CWSites My assumption is that all the 'setup for a new computer' is what the docker image itself is about no? Why would I want to keep around an old container when I can spin up a new one using the docker image. Isn't a better analogy for keeping old containers around that you turn off your computer but you want the data in your RAM/cache to be safe? – Marco Polo Mar 29 '21 at 15:21
  • @GinoMempin It answers what docker artifacts remain undeleted but not why they remain so. – Marco Polo Mar 29 '21 at 15:23
  • 1
    @CWSites Note that "a container always starts from a clean slate" is actually one of the big advantages of using Docker. It's a lot easier to set up a container when you know that it contains exactly what's in the image than to try to follow on a long stream of incremental updates. – David Maze Mar 29 '21 at 15:28
  • @MarcoPolo It's not deleted *because* of all those artifacts that are kept after containers are stopped. – Gino Mempin Mar 30 '21 at 04:49

2 Answers2

1

The examples you've provided show that you're using an Ubuntu container just to run bash. While this is fairly common pattern while learning Docker, it's not what docker is used for in production scenarios, which is what Docker cares about and is optimizing for.

Docker is used to deploy an application within a container with a given configuration.

Say you spool up a database container to hold information about your application. Then your docker host restarts for some reason, and that database disappears by default. That would be a disaster.

It's therefore much safer for Docker to assume that you want to keep your containers, images, volumes, and so on, unless you explicitly ask for them to be removed and decide this is what you want when you start them, with docker run --rm <image> for example.

msanford
  • 11,803
  • 11
  • 66
  • 93
0

In my opinion, it may have some reasons. Consider below condition:

I build my image and start the container (production environment, for some reason I stop the current container, do some changes to image and run another instance, so new container with new name is running.

I see new container does not work properly as expected, so as now I have the old container, I can run the old one and stop the new so the clients will not face any issues.

But what if containers were automatically deleted if they were stopped?

Simple answer, I would have lost my clients (even my job) simply:) And one person would be added to unemployed people :D

As @msanford mentioned, Docker assumes you want to keep your data, volumes, etc. so you'll probably re-use them when needed.

Since Docker is used to deploy and run applications (as simple as WordPress with MySQL but with some differences installing on Shared Hosting), usually it's not used for only running bash.

Surely it's good to learn Docker in the first steps by running things like bash or sh to see the contents of container.

Saeed
  • 3,255
  • 4
  • 17
  • 36