In Docker, you either build your own images to run your application as a container, or you can pull and use thousands of public images from the Docker repository and use them in your project. Once your image is ready, you can launch your containers using those images. A container is a running instance of a Docker image. Managing Docker containers is one of the most important aspects to look after as a system administrator who manages Docker hosts/containers. In this article, we’ll be focusing on managing containers using docker command.

run Command

docker run command is used to run a container from an image by specifying the Image ID or the Repository and/or Tag name. Example: The above command runs an instance of nginx application on a docker host, if it already exists. If it doesn’t exist on the Docker host, it goes out to the docker hub (by default) and pulls the image down. But this is done the only first time. For subsequent times the same image is reused. If you want to run a particular version of an image, specify its version separated by a colon. This is known as Tag. In case you don’t specify any tag, docker will consider it by default as the latest. Further, if you want to run the container in the background in a detached mode so that you get back to the prompt after Docker launches the container, use -d flag. Example:

ps Command

docker ps command lists all running containers and some basic information about them. Like container ID, name of image, time container is created, current status, and name of the container. Each container gets a random name (if not specified explicitly) and ID. Example: To list all the running and not running/exited containers at once, you can use: Example:

ls Command

Like ps command, ls can also be used for listing containers. -a flag can be used to list all containers (not just the running ones). Example:

stop Command

docker stop command is used to stop a running container. Here we need to put container name or ID along with this. On success, it would return the docker name or ID. Example: This will return the CONTAINER ID which you can use to stop the container. For this example and the coming ones, do note that you don’t need to specify a complete value of CONTAINER ID. It’ll accept up to the part, which makes it unique among other running containers as Docker knows which container to stop.

rm Command

docker rm command removes a stopped or exited container. Example:

exec Command

We can use exec command to go inside a running container. This is useful to debug running containers or do some stuff within a container. Example: Suppose you want to launch bash shell (assuming the image has Bash available, you can use other available shells as well) within a container named unruffled_meninsky in interactive mode, use: This should land you inside the container on a bash shell. Here the flag -i stands for interactive mode and -t for the terminal. If you just wish to execute one or more commands and exit out from the container, you can use:

logs Command

In case a container is launched in detached mode, and we want to see its logs, we can use logs command to review its logs: Example:

cp Command

To copy files between a container and localhost filesystem, you can use cp command. Example:

export Command

Docker container command offers an option to export the filesystem of a container as a TAR file.

inspect Command

We can check detailed information about a container using inspect command as: OR $ docker container inspect {CONTAINER NAME or ID}

kill Command

A running container can be killed using kill command with an optional –signal or -s flag. Multiple containers can be specified to kill them in one go. Example:

stats Command

To display a live stream of a container’s resource usage, you can use stats command: Example:

top Command

Like top command in Linux, we can use it with Docker to get a list of running processes. Example:

rename Command

To rename an existing container, use rename command. Example:

diff Command

We can inspect changes to files or directories on a container’s filesystem with diff command. Example:

Summary

To conclude, Doker offers an extensive set of commands to manage containers from their creation to destruction. We’ve covered some of the important commands and their usage in this article which should give you a good idea on managing docker containers. Next, find out some of the resources to learn DevOps.

15 Docker Commands to Manage Containers  List  Stop  Start  Remove and More  - 2315 Docker Commands to Manage Containers  List  Stop  Start  Remove and More  - 3915 Docker Commands to Manage Containers  List  Stop  Start  Remove and More  - 8615 Docker Commands to Manage Containers  List  Stop  Start  Remove and More  - 3715 Docker Commands to Manage Containers  List  Stop  Start  Remove and More  - 1715 Docker Commands to Manage Containers  List  Stop  Start  Remove and More  - 1