banner
AndrewTsui

AndrewTsui

Docker Usage Notes

Learning Materials:

Installation of Docker#

Installing Docker on Centos#

Installing Docker on Centos 7

Install the Community Edition
Run the command to verify if the installation is successful

docker version
# or
docker info

Docker is a server-client architecture, and when running Docker commands in the command line, the local machine needs to have the Docker service.

# Usage of the service command
sudo service docker start
# Usage of the systemctl command
sudo systemctl start docker

Image Files#

Docker packages the application and its dependencies into an image file. Only through this file can Docker containers be generated. An image file can be seen as a template for containers. Docker generates instances of containers based on image files. The same image file can generate multiple running container instances.

# List all image files on the local machine
docker image ls
# Remove image files
docker image rm imageName

Basic Operations#

  • Fetch the image file from the repository to the local machine: docker pull hello-world
  • Run this image file: docker container run hello-world
  • Terminate manually: docker container kill [containID]

Container Files#

The container instance generated by the image file is also a file, known as a container file. In other words, once a container is generated, two files will exist simultaneously: the image file and the container file. Closing the container does not delete the container file, it only stops the container from running.

  • List all running containers on the local machine: docker container ls
  • List all containers on the local machine, including terminated containers: docker container ls -all
  • Remove terminated container files: docker container rm [containerID]

Issues Encountered in Docker Packaging#

Slow Docker Packaging#

Docker uses layers to create images, and each command in the Dockerfile creates a new layer, each layer containing changes to the image's file system between the states before and after the command is executed. If the Dockerfile and related files have not changed, some existing layers in the local image cache can be reused when rebuilding.

Packaging without using cache#

docker build --no-cache -t xxxx

Issues Encountered in Docker Usage#

1. Error running docker run#

docker run hello-world
/usr/bin/docker-current: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See '/usr/bin/docker-current run --help'.

Solution: systemctl restart docker

2. Error in docker info showing Registry Mirrors: https://4piaafr1.mirror.aliyuncs.com/#

Even though it has been modified, why does it still show

=> [1/7] FROM docker.io/library/

Solution:
This situation may be caused by Docker build cache. Docker tries to use the cache as much as possible to speed up the build process. So even if the image source has been modified, Docker will still attempt to use the previous cache during the build.
Clear Docker cache: Before building with Docker, you can use the --no-cache option to disable the cache and force the download of the required images.
docker build --no-cache -t your_image_name .

For Mac version Docker, the daemon.json file is located in the ~/.docker folder.

3. Changing the Source#

Switching to Tsinghua source for Docker:

{
  "registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.