The remove container command looks something like this:
# the new syntax
# Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
docker container rm cd828234194a
# the old syntax
docker rm cd828234194a
The command requires a value that uniquely identifies a container; in this case, I used the full container ID for the hello-world container that we just ran. You can use the first few characters of a container's ID, as long as it provides a unique identifier between all of the containers on your system. Another way to uniquely identify the container is by the name assigned to it. Docker will provide a unique randomly-generated name for your container when you run it. In the preceding example, the random name assigned was competent_payne. So we could have used the remove command like this:
# using the randomly generated name
docker container rm competent_payne
While the randomly-generated names provided by docker are more human-readable than the container IDs it assigns, they still may not be as relevant as you would like. This is why docker has provided an optional parameter to the run command for naming your containers. Here is an example using the --name parameter:
# using our own name
docker container run --name hi-earl hello-world
Now when we list all of the containers, we can see our container has the name hi-earl. Of course, you would probably want to use a better container name, perhaps one that describes the function performed by the container, such as db-for-earls-app.
You can delete multiple containers at the same time by providing the unique identifier for each on the command line:
# removing more than one
docker container rm hi-earl hi-earl2
Usually, you will remove containers only after they have exited, such as the hello-world containers that we have been using. However, sometimes you will want to remove a container even if it is currently running. You can use the --force parameter to handle that situation. Here is an example of using the force parameter to remove a running container:
# removing even if it is running
docker container rm --force web-server
Here is what that would look like:
Notice that in the first container ls command, we didn't use the --all parameter. This reminds us that the web server container is running. When we tried to remove it, we were informed that the container is still running and would not be removed. This is a good safeguard to help prevent the removal of running containers. Next, we used the force command, and the running container was removed without any warning. Finally, we did another container ls command, including the --all parameter to show that the read/write data for our container was actually removed this time.
Sometimes, you may want to remove all of the containers on your system, running or not. There is a useful way to handle that situation. You can combine the container ls command and the container remove command to get the job done. You will be using a new parameter on the container ls command to accomplish this—the --quiet parameter. This command instructs Docker to only return the container IDs instead of the full list with a header. Here is the command:
# list just the container IDs
docker container ls --all --quiet
Now we can feed the values returned by the container ls command as input parameters to the container remove command. It will look like this:
# using full parameter names
docker container rm --force $(docker container ls --all --quiet)
# using short parameter names
docker container rm -f $(docker container ls -aq)
# using the old syntax
docker rm -f $(docker ps -aq)
This will remove all of the containers both running and exited from your system, so be careful!
You can add something like the following to your ~/.bash_profile or ~/zshrc file: alias RMAC='docker container rm --force $(docker container ls --all --quiet)'.
Many containers are designed to run and exit immediately, such as the hello-world example we've used several times already. Other container's images are created so that, when you run a container using it, the container will continue running, providing some ongoing useful function, such as serving web pages. When you run a container that persists, it will hold onto the foreground process until it exits, attaching to the processes: standard input, standard output, and standard error. This is okay for some testing and development use cases, but normally, this would not be desired for a production container. Instead, it would be better to have the container run as a background process, giving you back control of your terminal session once it launches. Of course, there is a parameter for that. It is the --detach parameter. Here is what using that parameter looks like:
# using the full form of the parameter
docker container run --detach --name web-server --rm nginx
# using the short form of the parameter
docker container run -d --name web-server --rm nginx
Using this parameter detaches the process from the foreground session and returns control to you as soon as the container has started. Your next question is probably, how do I stop a detached container? Well, I am glad you asked. You use the container stop command.