Introduction to docker

Keywords: Docker Kubernetes

Introduction to docker

docker pull website
Docker - Official Image | Docker Hub

1. Install docker

1. Delete the local docker

yum remove docker*

2. Download the yum runtime library

 sudo yum install -y yum-utils

3. Set download source

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

4. Download docker

sudo yum install -y docker-ce  docker-ce-cli containerd.io

-The Query options of the y parameter are set to yes, and the docker server, docker command line and dynamic link files used by docker are installed respectively

5. Set startup

systemctl enable docker --now

6. Set the image accelerator
Find mirror acceleration source
Alicloud server - > container mirroring Service - > mirroring tool - > mirroring accelerator - > select centos

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://6nm7p6th.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

Find image

hub.docker.com 

Download Image

docker pull Software name

Common commands

docker images #View the downloaded images
docker ps #View the software started and running on the docker server
docker rmi image Id #Remove mirror
docker logs image id  #View the log of the mirror
docker cp Started mirror id: /etc/nginx/nginx.conf   /data/conf/nginx.conf   #Copy the things in the designated position of the container, or copy the things outside to the inside of the container

Start container

docker run Set item image name #Mirror start run command
docker run --name=mynginx -d  nginx 
--name Give a name to the started container to distinguish it  -d Let the container run in the background
docker rm id/Container name   #Kill process - f this parameter kills the running process. First, use docker ps -a to view the suspended containers that have been terminated
docker stop id/Container name  #Stop the container started in the background running mode. It just stops and still exists in the process
docker start id/Container name  #Start the stopped container

After the running machine restarts, the running container stops. You can add -- restart=always when starting. After the machine restarts, the container starts automatically
-p port mapping 88:80 forwards the access request received by port 88 to port 80

Enter the container to modify the content

(example: change the html file of nginx)
1. Use the docker ps command to query the id of the currently running container

2. Execute docker exec - it < container ID > / bin/bash to switch to the virtual machine operating on the container (/ bin/bash refers to the current command window. If / bin/bash cannot be used, try / bin/sh)

3. Operate the container

4.exit exit docker container

Submit changes

(saving the changes to the container is equivalent to saving a snapshot, similar to git commit)
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

1. Execute the command

docker commit [option] <container id> <Alias the modified container>

Options:
-a, --author string Author (e.g., "John Hannibal ")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)

2. Example: docker commit -a "wood" - m "home page change" < container ID > wood nginx: v1.0

3.docker rm container id # close the started container

4. Use docker images to see the modified container

5. Start the modified container with docker run - D wood nginx: v1.0

Run the modified container on the new machine

Method 1: compress the image and generate a compressed package to save the image

1. Image name submitted by docker save - O xxx.tar: version tag

docker save -o wood-nginx.tar wood-nginx:v1.0

After the command is executed, the image compressed file is generated in the file directory where the command is executed

2. Transfer to other machines and start the image after decompression

docker load -i Compressed file name of the image
docker load -i wood-nginx.tar 

3. After the target machine executes docker images, you can see the decompressed image name

Method 2: push the image to the remote warehouse

1. Register an account on hub.docker.com and establish a public warehouse with your own authority

2. The image name modified by docker tag: the absolute path of version tag in the warehouse: version tag (changed to the name format required by the warehouse)

docker tag wood-nginx:v1.0 wood/wood-nginx:v1.0

3. Using docker images, you can see an additional image name with the path as the name

4. Log in to the remote warehouse at the terminal

docker login 

Enter the user and password registered on the website

5. Push the image with path prefix to the remote warehouse

docker push wood/wood-nginx:v1.0

6. Log out

docker logout

7. View the image pushed on the website

8. Run docker pull + the image name pushed on another machine to run the modified image on other machines
Mount data to external modification
In order not to enter the virtual machine running the image when you want to modify the image, mount the file directory outside the virtual machine to the directory of the virtual machine to realize the external modification of data
Add the - v parameter to the regular docker startup command to mount

docker run --name=mynginx -d \ 
--restart=always -p 80:88 -v /data/html:/usr/share/nginx/html:ro nginx

*The mounting of this file is equivalent to directly replacing the file path inside the virtual machine. When using it, pay attention to add the files required for the image in the mounted path and modify it
*-v you can add multiple - v parameters to mount multiple files
After mounting, you can modify the image on the host

Example: deploying redis

Start redis with the redis.conf parameter configuration file edited by yourself, persist the data to the specified directory, and mount the redis parameter configuration file and the persistence directory respectively
In the redis.conf configuration file of redis, simply write appendonly yes to start persistence for testing

 docker run -v /root/test/redis/redis.conf:/etc/redis/redis.conf \ 
 -v /root/test/redis/data:/data  -d --name=myredis \ 
 -p 6379:6379 redis redis-server /etc/redis/redis.conf

Posted by sader on Sun, 28 Nov 2021 06:23:48 -0800