Post

Command line (CLI) handbook for using Docker

This is a handbook of commands to control Docker using CLI (Command-Line Interface) in a shell environment without using the Docker GUI. We introduce how to create and control Docker images, create and control Docker containers, and save and load images or containers.

permission denied while trying to connect to the Docker daemon socket

When you install Docker and try to execute Docker-related commands, a permission error related to access to /var/run/docker.sock occurs. From a system perspective, Docker’s actions seem to require root privileges.

1
2
3
$ docker images -a
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/json?all=1": dial unix /var/run/docker.sock: connect: permission denied

Use the command below to grant your Ubuntu account access to /var/run/docker.sock.

1
sudo chmod 666 /var/run/docker.sock;sudo chown -R ${USER}:users /var/run/docker.sock

Image

Build Docker Image

The first thing you need to do to use Docker may be to create a Docker image. You can build a Docker image with the command below. This command builds a Docker image based on the Dockerfile you wrote.

1
2
3
4
5
6
7
8
9
# build general 
docker build -f <Dockerfile> -t <tag> .

# build without cache
docker build -f <Dockerfile> --no-cache -t <tag> .

# -----------------------------------------------
# example
docker build -f Dockerfile -t img-my .

List Docker Images and remove it

Below is the command to view and delete the list of Docker images registered in the system.

1
2
3
4
5
# list
docker images -a

# remove
docker rmi <repository name>

Remove Docker images cache

If you repeat the build of the Docker image while modifying the Dockerfile, the build time will be less than when building the image for the first time. This is possible because Docker has a layered structure and unchanged parts are cached in the system. However, cached content takes up storage space. The command to delete the Docker cache is as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# remove docker cache
docker system prune -a

# -----------------------------------------------
# example
$ docker system prune -a
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

Are you sure you want to continue? [y/N] y
Deleted build cache objects:
kxgva6ktfwea5hiatrfmgyeq3
...
c5tvqn2p53ledfjz4nqyrbl1g
gtdusijog41t1972eh4oggk37

Total reclaimed space: 15.34GB


Container

Create Container from Image

If you have created a Docker image, this is the command to create a container based on the image.

1
2
3
4
5
docker run -d -it --name <container name> -p <ubuntu machine port>:<docker port> <image name>

# -----------------------------------------------
# example
docker run -d -it --name cond-my -p 43389:3389 -p 48080:8080 -p 44000:4000 -p 422:22 img-my

List Docker Containers and remove it

If you first run a Docker container with the run command, a container is created. You can also check the status of Docker containers and list the containers through the ps command. You can also delete Docker containers with the rm command.

1
2
3
4
5
6
# list 
docker ps -a

# remove
docker rm <container id>

Start, Stop, Attach

If you find the ID of the Docker container by searching the Docker list, you can use the start command to start and stop the Docker container (stop) and enter inside Docker (attach).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# start stopped container 
docker start <container id>

# start stopped container & enter container
docker start -i <container id>          

# stop running container
docker stop <container id>

# enter running(started) container 
docker attach <container id>

# pass to commands to running container
docker exec -it <container names> <commands>  

Save/Load docker image

Export Docker Image to file

In my case, I use the Docker function of Synology NAS. Synology basically supports the ability to create containers based on images registered on Docker Hub. However, because I wanted to use the Docker image created based on the Dockerfile as a container, I saved the image created on the Ubuntu machine as a file and loaded it in Synology Docker.

The command below saves the Docker image registered on the system as a tar file.

1
2
3
4
5
docker save -o <file name> <image name>

# -----------------------------------------------
# example
docker save -o img-my.tar img-my

Load Docker Image from Image file

The command below loads the Docker image in tar file format into the system.

1
2
3
4
5
6
docker load -i <file name>

# -----------------------------------------------
# example
docker load -i img-my.tar

Export Docker Container to Image

Depending on the situation, it may not be possible to configure the environment in the image with only the contents described in the Dockerfile. Therefore, sometimes you may need to create an image of a manually configured Docker container. The command below converts a Docker container to an image.

1
2
3
4
5
docker commit <container name> <image name = repository name>:<tag>

# -----------------------------------------------
# example

References

  • Extract and load Docker image: https://dongle94.github.io/docker/docker-image-extract/

This post is licensed under CC BY 4.0 by the author.