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
Docker run is used to create a container from an IMAGE and run it.
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
Example
docker top CONTAINER [ps OPTIONS]
Example
Start one or more stopped containers
docker top CONTAINER [ps OPTIONS]
Example
Docker stop command is used to stop one or more running containers.
docker stop [OPTIONS] CONTAINER [CONTAINER...]
Example
Docker containers can be deleted using docker rm containerId
or docker container rm containerId
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Example
Display a live stream of container(s) resource usage statistics.
docker stats [OPTIONS] [CONTAINER...
docker stats
returns a live data stream for running containers.
Example
Attach local standard input, output, and error streams to a running container.
docker attach [OPTIONS] CONTAINER
docker attach
is used to connect terminal to a running container, using containerID or name. So basically, it displays the output of the ENTRYPOINT/CMD
process. The may look like hung, that just means no process from the container is interacting with the terminal at that time.
Example
Docker pause
command suspends all processes in a specified container.
docker pause CONTAINER [CONTAINER...]
Example
Unpause all processes within one or more containers.
docker unpause CONTAINER [CONTAINER...]
Example
Kill one or more running containers
docker kill [OPTIONS] CONTAINER [CONTAINER...]
Example
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
.
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
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
overdocker 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. Usedocker 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.
docker rm [OPTIONS] CONTAINER [CONTAINER...]
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.