In this guest tutorial by Jaroslaw Krochmalski, the author of “Docker and Kubernetes for Java Developers”, you’ll learn how to run and stop Docker containers manually from the shell or the command line.
If you are a more “video person”, you may want to check this video, which covers the same topics of this article.
Starting a docker container
To spin-up a container from an image, you need to use the docker run command. The running container will have its own file system, networking stack, and isolated process tree separate from the host. As you may be aware, every single docker run command creates a new container and executes a command specified in the Dockerfile, CMD, or ENTRYPOINT.
The syntax of the docker run command is as follows:
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
The command takes the image name, with the optional TAG or DIGEST. If you skip the TAG and DIGEST command parameters, Docker will run the container based on the image tagged latest. The docker run command also takes a set of possible options you may find useful, such as the runtime mode, detached or foreground, network settings, or runtime restrictions on CPU and memory.
Of course, you can execute the docker run command without any argument except the image name. It will run and take the default options defined in the image. Specifying options gives you the chance to override the options specified by the author of the image and, also, the runtime defaults of the Docker engine.
The COMMAND parameter is not mandatory; the author of the image may already have provided a default COMMAND using the CMD instruction in the Dockerfile. The CMD occurs only once in a Dockerfile and it’s usually the last instruction. When starting the container from an image, you can override the CMD instruction, simply by providing your own command or parameters as the COMMAND parameter for the docker run. Anything that appears after the image name in the docker run command will be passed to the container and treated as a CMD argument. If the image also specifies an ENTRYPOINT, then the CMD or COMMAND gets appended as an argument to the ENTRYPOINT. But guess what, you can override the ENTRYPOINT as well, using the –entrypoint option for the docker run command.
To stop one or more running Docker containers, you can use the docker stop command. The syntax is simple:
$ docker stop [OPTIONS] CONTAINER [CONTAINER...]
You can specify one or more containers to stop. The only option for docker stop is -t (–time) which allows you to specify a wait time before stopping a container. 10 seconds is the default value, which is supposed to be enough for the container to gracefully stop. To stop the container in a more brutal way, you can execute the following command:
$ docker kill CONTAINER [CONTAINER...]
What’s the difference between docker stop and docker kill ? They will both stop a running container. There’s an important difference though:
- docker stop: The main process inside the container will first receive a SIGTERM and, after a grace period, a SIGKILL
- docker kill: The main process inside the container will be sent SIGKILL(by default) or any signal specified with –signal
In other words, docker stop attempts to trigger a graceful shutdown by sending the standard POSIX signal SIGTERM, whereas docker kill just brutally kills the process, thereby shutting down the container.
Listing the running containers
To list the running containers, simply execute the docker ps command:
$ docker ps
To include all the containers present on your Docker host, append the -a option:
$ docker ps -a
You can also filter the list using the -f option to specify a filter. The filter needs to be provided as a key=value format. Currently available filters include:
- id: Filters by the container’s ID
- label: Filters by label
- name: Filters by the container’s name
- exited: Filters by the container’s exit code
- status: Filters by status, which can be created, restarted, run, removed, paused, exited or dead
- volume: When specified with the volume name or mount point, it will include containers that mount specified volumes
- network: When specified with the network ID or name, it will include containers connected to the specified network
Consider the following example, which will take all the containers present on the Docker host and filter them out by running status:
$ docker ps -a -f status=running
Removing the containers
To remove the container from the host, you can use the docker rm command. The syntax is as follows:
$ docker rm [OPTIONS] CONTAINER [CONTAINER...]
You can specify a single container or more containers at once. If you are running short-term foreground processes over and over many times, these file systems can grow rapidly in size. There’s a solution for that: instead of cleaning manually by hand, tell Docker to automatically clean up the container and remove the file system when the container exits. You can do this by adding the –rm flag so that the container data is removed automatically after the process has finished. Note that the –rm flag will make Docker remove the container after it has been shut down.
For example, use the run command as shown in the following example:
$ docker run --rm -it Ubuntu /bin/bash
The preceding command tells Docker to remove the container if it’s shut down.
You’ve learned how to start, stop, list, and remove Docker containers. If you found this tutorial helpful and wish to learn more about Docker and Kubernetes, you can refer to Docker and Kubernetes for Java Developers, an easy-to-follow practical guide that will help Java developers develop, deploy, and manage Java applications efficiently.