Learning Materials:
- Docker Getting Started Tutorial by Ruanyifeng
- Learn Docker Knowledge
- Shanggu Valley 2022 Edition Docker Tutorial
Docker Installation#
Centos Installation of Docker#
Install the community version
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 on the command line, you need to have the Docker service on your local machine.
# 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 in an image file. Only through this file can Docker containers be generated. The image file can be regarded 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
# Delete image files
docker image rm imageName
Simple 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, called a container file. In other words, once the container is generated, there will be two files simultaneously: the image file and the container file. Closing the container does not delete the container file, it just 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
- Delete terminated container files:
docker container rm [containerID]
Issues Encountered in Docker Packaging#
Docker Packaging is Slow#
Docker uses layers to create images, and each command in the Dockerfile creates a new layer, each layer contains changes to the image's file system between the states before and after executing the command. 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 when 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
with Registry Mirrors: https://4piaafr1.mirror.aliyuncs.com/
#
I have already made the modifications, why is it still showing
=> [1/7] FROM docker.io/library/
Solution:
This situation may be caused by Docker build cache. Docker tries to use the previous cache when building images to speed up the build process. So even if you have modified the image source, Docker will still attempt to use the previous cache during the build.
Clear Docker cache: Before building 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 of 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"]
}