Home About Me

Docker Commands in One Map: A Practical Reference

The mind map below provides a quick visual overview of the Docker commands covered here.

Docker command mind map

Docker rests on three basic concepts.

Image: A Docker image is a special filesystem. In addition to the programs, libraries, resources, configuration files, and other files a container needs at runtime, it also carries runtime configuration parameters such as anonymous volumes, environment variables, and users. An image contains no dynamic data, and its contents do not change after it is built.

Container: The relationship between an image and a container is like the relationship between a class and an instance in object-oriented programming. An image is a static definition; a container is the runtime entity of that image. Containers can be created, started, stopped, deleted, paused, and so on.

Repository: A repository is similar to a remote Git repository. It stores image files in a central location.

The relationship among the three is shown below.

Relationship between image, container, and repository

Now let's look at common Docker commands.

Service

Check Docker version information:

docker version

Check brief Docker information:

docker -v

Start Docker:

systemctl start docker

Stop Docker:

systemctl stop docker

Enable Docker at boot:

systemctl enable docker

Restart the Docker service:

service docker restart

Stop the Docker service:

service docker stop

Images

Image registries such as Docker Hub host many high-quality images, and you can obtain images from them.

Search for an image:

docker search 关键字

Pull an image:

docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]

Image management:

List images:

docker image ls
docker images

Delete an image:

# Delete a specific image
docker rmi <镜像Id>

Export an image:

# Save the image as an archive file
docker save

Import an image:

docker load

Building images with a Dockerfile

A Dockerfile is a text-format configuration file. Users can use a Dockerfile to quickly create custom images. A Dockerfile consists of command statements, one per line, and supports comment lines beginning with #.

Common Dockerfile instructions:

  • FROM: specify the base image
  • RUN: execute commands
  • COPY: copy files
  • ADD: a more advanced file-copy instruction
  • CMD: the container startup command
  • ENV: set environment variables
  • EXPOSE: expose ports

Other instructions include ENTRYPOINT, ARG, VOLUME, WORKDIR, USER, HEALTHCHECK, ONBUILD, LABEL, and more.

Here is a Dockerfile example:

FROM java:8
MAINTAINER "jinshw"mailto:[email protected]
ADD mapcharts-0.0.1-SNAPSHOT.jar mapcharts.jar
EXPOSE 8080
CMD java -jar mapcharts.jar

Build an image:

docker build

Run an image:

Running an image means creating and starting a container from it.

docker run [镜像ID]

Containers

Container lifecycle:

Starting a container can be done in two ways: create and start a new container from an image, or restart a container that is in the stopped state.

# Create and start a new container
docker run [镜像名/镜像ID]
# Start a stopped container
docker start [容器ID]

View containers:

# List running containers on this host
$ docker ps
# List all containers on this host (including stopped and running)
$ docker ps -a

Stop a container:

# Stop a running container
docker stop [容器ID]
# Kill the container process
docker kill [容器ID]

Restart a container:

docker restart [容器ID]

Delete a container:

docker rm [容器ID]

Enter a container. There are two ways:

# Exiting from this stdin will stop the container
docker attach [容器ID]
# Enter the container interactively
docker exec [容器ID]

The second approach is usually preferred. Common parameters for docker exec are:

  • -d, --detach: run the command in the background in the container
  • -i, --interactive=true|false: open standard input to accept user commands

Export and import:

Export a container:

# Export an existing container to a file
docker export [容器ID]

Import a container:

# The exported container snapshot can be imported as an image
docker import [路径]

Other commands

View logs:

# View container logs
docker logs [容器ID]

Common parameters for this command:

  • -f: follow log output
  • --since: show all logs since a given start time
  • -t: show timestamps
  • --tail: list only the latest N container logs

Copy files:

# Copy from host to container
sudo docker cp host_path containerID:container_path
# Copy from container to host
sudo docker cp containerID:container_path host_path