Docker basics: how to start and stop containers – Elder Moraes

In this guest tutorial by Jaroslaw Krochmalski, the author of “Docker and Kubernetes for Java Developers,” you will learn how to run and stop Docker containers manually from the shell or command line.

Before you begin, you may want to check out my book “5 Steps to an Extraordinary Career,” which will guide you to build your dream career as a software developer. Click here to view it.

If you are a more “video” person, you may want to check out this video, which covers the same topics as this article.

Starting a

Docker

container

To activate a container from an image, you must use the docker run command. The running container will have its own file system, network stack, and isolated process tree separate from the host. As you may know, each docker run command creates a new container and executes a specified command in 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 name of the image, with the optional TAG or DIGEST. If you omit the TAG and DIGEST command parameters, Docker will run the container based on the most recent tagged image. The docker run command also takes a set of possible options that you may find useful, such as runtime, separate or foreground mode, network settings, or runtime restrictions on CPU and memory.

Of course, you can run the docker run command without any arguments except the image name. It will run and take the default options defined in the image. Specifying options gives you the opportunity to override the options specified by the image author and also the Docker runtime defaults.

The COMMAND parameter is not required; the image author may have already provided a default COMMAND using the CMD statement in the Dockerfile. The CMD occurs only once in a Dockerfile and is usually the last instruction. When launching the container from an image, you can override the CMD statement, simply by providing your own command or parameters as a COMMAND parameter for Docker execution. 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 is appended as an argument to the ENTRYPOINT. But guess what, you can also override the ENTRYPOINT, using the -entrypoint option for the docker run command.

Stop

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 timeout before stopping a container. 10 seconds is the default, which is supposed to be enough for the container to stop successfully. To stop the container in a more brutal way, you can run the following command:

$ docker kill CONTAINER [CONTAINER…]

What is the difference between docker stop and docker kill? Both will stop a working container. However, there is an important difference

: docker stop: The main process inside the container will first receive a SIGTERM and, after a grace period, a docker kill SIGKILL:

  • The main process inside the container
  • will send SIGKILL (by default) or any signal specified with –

signal

In other words, docker stop tries to trigger a successful shutdown by sending the standard POSIX signal SIGTERM, While Docker Kill simply brutally kills the process, thus closing the container.

List of

running containers To list running containers, simply run the docker ps: $ docker ps command

To include all containers present on the Docker host, add

the -a option: $ docker ps -a You can also filter the list using the -f option to specify a

filter. The filter must be provided as a key=value format. Currently available filters include

: id: Filters by container ID tag: Filters by tag name: Filters by container name exited: Filters by

  • container exit code
  • status:

  • Filters by
  • status, which

  • can be created, restarted, executed, removed, paused, exited, or dead volume: When
  • specifies with the name of the volume or mount point, will include containers that mount specified volumes
  • network

  • : when specified with the network ID or name, 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 by execution state:

$ docker ps -a -f status=running

Deleting 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 a time. If you are running short-term foreground processes over and over again many times, these file systems can quickly grow in size. There’s a workaround for that: instead of manually cleaning by hand, tell Docker to automatically clean the container and delete the file system when the container exits. You can do this by adding the -rm flag so that the container data is automatically deleted after the process is complete. Note that the -rm flag will cause Docker to delete 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 above command instructs Docker to remove the container if it is closed

.

You have learned how to start, stop, enumerate, and remove Docker containers. If you found this tutorial helpful and want to learn more about Docker and Kubernetes, you can check out Docker and Kubernetes for Java Developers, an easy-to-follow how-to guide that will help Java developers develop, deploy, and manage Java applications efficiently.