Article directory
what can docker do
Docker can help us package applications, dependencies, function libraries, and configurations together to form a portable image
Docker makes it easy for an application to run on any operating system. And the virtual machines we contacted before can also run another operating system in one operating system to protect any application in the system.
Difference between docker and virtual machine
In short, docker only encapsulates the function library that needs to be used, unlike virtual machines, which need to simulate a complete operating system
In this way, it is very vivid to see that docker is much lighter than virtual machines
Concepts you need to know before learning docker
Image : Docker packages the application and its required dependencies, function libraries, environment, configuration and other files together, which is called an image. Mirror image is like a stamp
Container : The process formed after the application in the image runs is the container , but Docker will do the container process.isolate, not visible to the outside world. The container is like the words
printed on the seal. No matter what you do with the printed words, you will not affect the seal.
All applications are ultimately composed of code, and they are all files formed by bytes in the hard disk . Only when running, will it be loaded into memory to form a process.
The image is a file package formed by packaging a file applied on the hard disk, its operating environment, and some system function library files together. This package is read-only.
The container is to load the programs and functions written in these files into the memory to allow it to form a process, but it needs to be isolated. Therefore, an image can be started multiple times to form multiple container processes.
Docker is a CS-architecture program that consists of two parts:
-
Client (client): Send instructions to the Docker server through commands or RestAPI. Commands can be sent to the server locally or remotely.
-
Server: Docker daemon, responsible for processing Docker instructions, managing images, containers, etc.
Why is docker a cs architecture?
Because the docker container is isolated from the outside, you cannot directly operate the docker container,
so you have to send instructions to the server through the client first and then operate the container through the server
DockerHub:
- An image-hosted server, similar to Alibaba Cloud Image Service, collectively referred to as DockerRegistry,
we downloaded images from dockerhub
Summarize
Mirror:
- Package the application together with its dependencies, environment, configuration
container:
- An image runs as a container, and an image can run multiple containers
Docker structure:
-
Server: Receive commands or remote requests, operate images or containers
-
Client: Send commands or requests to the Docker server
DockerHub:
- An image-hosted server, similar to Alibaba Cloud Image Service, collectively referred to as DockerRegistry
Operation of Docker
mirror operation
image name composition
- The mirror name is generally divided into two parts: [repository]:[tag].
- When no tag is specified, the default is latest, which represents the latest version of the image
As shown in the figure:
the two parts are the name: version number If there is no version number, it is the latest version
mirror command
As shown in the figure, we give an example of downloading nginx
- First go to the mirror repository to search for nginx mirrors, such as DockerHub :
- According to the viewed image name, pull the image you need and use the command:
docker pull nginx
- View the pulled image through the command: docker images
docker images
- pull image
docker pull nginx
- save image
docker save -o [保存的目标文件名称] [镜像名称]
Because we downloaded nginx
docker save -o nginx.tar nginx:latest
Delete the local nginx mirror
docker rmi nginx:latest
load local file
docker load -i nginx.tar
Here I would like to talk about the difference between pulling the image and saving the image.
What we download (pull) from the server is in the docker.
We take the image out of the docker and use save.
container operation
The container protects three states:
- Running: The process is running normally
- Pause: The process is suspended, the CPU is no longer running, and memory is not released
- Stop: The process is terminated, and the memory, CPU and other resources occupied by the process are reclaimed
in:
-
docker run: create and run a container, in running state
-
docker pause: Pause a running container
-
docker unpause: resume a container from a paused state
-
docker stop: stop a running container
-
docker start: make a stopped container run again
-
docker rm: remove a container
command to create and run an nginx container:
docker run --name containerName -p 80:80 -d nginx
More instructions https://blog.csdn.net/qq_32447301/article/details/79387649
Command interpretation:
- docker run : create and run a container
- --name : give the container a name, such as mn
- -p : Map the host port to the container port, the left side of the colon is the host port, and the right side is the container port
- -d: run the container in the background
- nginx: image name, such as nginx
The -p
parameter here is to map the container port to the host port.
By default, the container is an isolated environment. We directly access port 80 of the host machine, and we certainly cannot access the nginx in the container.
Now, associate the 80 of the container with the 80 of the host. When we access the port 80 of the host, it will be mapped to the 80 of the container, so that we can access nginx:
how to get into the container
1) Enter the container. The command to enter the nginx container we just created is:
docker exec -it mn bash
Interpretation of the command:
-
docker exec : go inside the container and execute a command
-
-it : Create a standard input and output terminal for the currently entered container, allowing us to interact with the container
-
mn : the name of the container to enter
-
bash: command executed after entering the container, bash is a linux terminal interactive command
2) Enter the directory /usr/share/nginx/html where the HTML of nginx is located
Inside the container, a separate Linux file system is simulated, which looks like a Linux server:
The environment, configuration, and running files of nginx are all in this file system, including the html files we want to modify.
Looking at the nginx page in the DockerHub website, you can know that the html directory location of nginx is in/usr/share/nginx/html
We execute the command to enter this directory:
cd /usr/share/nginx/html
View the files in the directory:
3) Modify the content of index.html
There is no vi command in the container and cannot be modified directly. We use the following command to modify:
sed -i -e 's#Welcome to nginx#helloworld#g' -e 's#<head>#<head><meta charset="utf-8">#g' index.html
Visit your own virtual machine address in the browser to see the result
summary
What are the common parameters of the docker run command?
- --name: specify the container name
- -p: specify port mapping
- -d: let the container run in the background
Command to view container logs:
- docker logs
- Add the -f parameter to continuously view the log
View container status:
- docker ps
- docker ps -a View all containers, including stopped ones
data volume
What is a data volume
A data volume is a virtual directory that points to a directory in the host's file system.
Once the data volume is mounted, all operations on the container will act on the host directory corresponding to the data volume.
In this way, we operate the /var/lib/docker/volumes/html directory of the host, which is equivalent to operating the /usr/share/nginx/html directory in the container
Why have data volumes
Because docker only contains the functions needed to run the program, commands like vim are not in docker, so we need to establish a relationship between the files in the docker and the host file, so that operating the files on the host can also change the contents of the container
Data volume operation commands
The basic syntax of data volume operations is as follows:
docker volume [COMMAND]
The docker volume command is a data volume operation, and the next operation is determined according to the command that follows the command:
- create creates a volume
- inspect displays information about one or more volumes
- ls lists all volumes
- prune removes unused volumes
- rm removes one or more specified volumes
Create and view data volumes
Requirement : Create a data volume and view the directory location of the data volume on the host
① Create a data volume
docker volume create html
② View all data
docker volume ls
③ View data volume details volume
docker volume inspect html
As you can see, the host directory associated with the html data volume we created is a /var/lib/docker/volumes/html/_data
directory.
summary
The role of the data volume:
- Separating and decoupling the container and data to facilitate the operation of the data in the container and ensure data security
Data volume operations:
- docker volume create: create a data volume
- docker volume ls: view all data volumes
- docker volume inspect: View data volume details, including associated host directory locations
- docker volume rm: delete the specified data volume
- docker volume prune: delete all unused data volumes
mount data volume
When we create a container, we can mount a data volume to a directory in a container through the -v parameter. The command format is as follows:
docker run \
--name mn \
-v html:/root/html \
-p 8080:80
nginx \
The -v here is the command to mount the data volume:
-v html:/root/htm
: Mount the html data volume to the /root/html directory in the container
The container can not only mount the data volume, but also directly mount it to the host directory. The relationship is as follows:
- With data volume mode: host directory --> data volume --> directory in the container
- Direct mount mode: host directory -> directory in the container
Syntax :
The syntax for directory mounts and data volume mounts is similar:
- -v [host directory]:[container directory]
- -v [host file]:[container file]
For an example:
docker run \
--name mn \
-v 主机的目录:容器里面的目录 \
-p 8080:80
nginx \