Docker container is another important concept. You have learned docker image in depth.   A container is a runnable instance of an image. Containers are a group of applications that run independently in isolation. Example a single container can be built with centos7 + jdk + tomcat + web-app. The isolated environment where it runs is known as docker runtime. In this page, will talk about how to do docker run, docker exec, import container, export container, clean up container with example.

Docker container command – cheatsheet

Start a docker container

I am assuming you have gone through the above cheatsheet for container commands. There are basically 2 ways to start a container, (i) Create a container from docker image using docker run or (ii) start a terminated/stopped container using docker start

Docker containers are lightweight, it is a usual practice to delete and recreate containers at any time.

Create a Container and run

Basically, docker run is used to create a container and run from an image. This image either has to be available in the registry (docker hub or custom registry), or you build the image from a Dockerfile using docker build command.

Let us look at the example below


The above example runs centos:7 docker container and names the container as centos-container.  Type exit to get out of the container, this will also terminate the container. Now run docker container ls --all, you will be able to see centos:7 in the list of containers.

Now, start the terminated container again using the below command


Stop a container and start the container again

Run docker stop containerID/containerNAME to stop a running container. You can use the docker start again to up the container again.


Pause a running container and Unpause it

Use docker pause to pause and docker unpause to resume the container.


Connecting to a running container

There are occasions when you need to enter a running container to debug, inspect or run commands. Docker provides 2 commands for the same, docker attach and docker exec .

1. Using docker attach

Docker attach is used to connect the terminal to a running container. Look at the different examples to get a clear idea of when to use this command.

docker attach [OPTIONS] CONTAINER

Example (press ctrl + c to get out of the container)


Linux top command can display system summary information as well as a list of tasks currently being managed by the Linux kernel. Above example, explains how to connect the local terminal to a running container.

In the below example, you will see that the exit 13 returned by bash process, is also returned by the docker attach command.

2. Using docker exec

As the official documentation says, use docker exec to run a command in a running container. Command runs in the default directory of the running container. The default directory is specified as the WORKDIR in Dockerfile.


In the example below, -d is used to print the digest so you can connect to it. -i to start in interactive mode and -t to start in a new terminal session.


Copy the digest from the above and use it with the available command.


It is always recommended to go with docker exec over docker attach

Export and import docker container

Use docker export or docker container export command to export a container’s filesystem as a tar archive.

docker export [OPTIONS] CONTAINER

Look at the example below


Similarly, use docker import command to import the contents from a tarball to create a filesystem image.

docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

To reimport, the above-exported centos:7.tar, follow the code below


Alternatively, you can import it by specifying a URL or a directory, for example


Note: Use docker load to import image files stored in the local image library. Use docker import to import a snapshot of the container to the local image library. The difference between the two is that the container snapshot file will discard all history and metadata information (that is, only the snapshot state of the container at the time), and the image storage file will save the full record and is large. In addition, metadata information such as tags can be reassigned when importing from a container snapshot file.

Clean up containers

As you have read in this tutorial before, it is a usual practice to clean up/ delete old containers and rebuild the new ones from the image repository. In your case, you may have a test build deployed every morning, so basically delete the old containers and recreate new ones.

Use docker rm containerId or docker container rm containerId to remove container(s). You can use containerId, container-name or digest to delete.


Example to remove a particular container


Using the above command, you can only delete a terminated container. Use -f to forcefully delete a running container, but be careful.

To clean up all containers that are terminated, use docker container prune, look at the below code

Further reading:

I would love to hear your thoughts on this topic, please share in the comments below.

By |Last Updated: July 16th, 2019|