Work needs to use Kubernetes, so you need to learn Docker before.
The learning materials are ruan Yifeng’s tutorials. And official Docker documentation. As well as crazy god said b station video tutorial.
1. Environment configuration problems
To run an application, you need some environment configuration. However, during development, there are often problems that run on one machine but not on another. To solve this problem, several technologies have been developed.
1.1 the virtual machine
Virtual machine is a technology to solve this problem, virtual machine is simple to simulate one operating system on another operating system. Setting up an environment on a VIRTUAL machine ensures a consistent environment. Of course, virtual machines have some disadvantages:
- Resource usage: A virtual machine takes up resources as long as it is running, not just because the virtual machine uses only a small portion of resources.
- Redundant steps: Because a virtual machine is a complete system, redundant operations such as user logins are required.
- Slow startup: The VM starts in the same way as a common OS, which takes a long time.
The 1.2 Linux container
Linux Containers (LXC) have emerged to solve various problems with virtual machines. LXC does not create a new operating system, but merely isolates processes, so that for a container, the resources it touches are virtual, thus isolating it from the underlying system. Advantages of containers:
- Fast startup: Since you don’t need to start an operating system, the container will start much faster than the virtual machine, just a process.
- Small resource usage: No operating system occupies resources, containers occupy required resources, and resources can be shared among multiple containers. By contrast, virtual machine resources are exclusive.
- Small size: As the name suggests.
What is a Docker
Docker is a wrapper around a container that provides some interfaces to make the container easier to use. Docker packages the application and its associated dependencies, generating a file. Running this file generates a virtual container. The program runs on this virtual container.
2.1 Simplified process of Docker
With Docker, the development and deployment process can be simplified as follows:
Develop application -> package application and environment with Docker, generate image -> upload to Docker warehouse -> download image, direct useCopy the code
2.2 Benefits of Docker
Because applications can be isolated, they do not affect each other. Therefore, you can run multiple containers on a server to maximize server efficiency.
3. The use of Docker
- Provide a one-time environment: testing software, unit testing, etc.
- Provide resilient cloud services: Containers can be opened and closed at any time, making them ideal for dynamic expansion and shrinkage.
- Component microservice architecture: A microservice architecture can be simulated because multiple containers can run on a single machine and therefore multiple services can run.
4. Docker installation and stomping pits
Pits encountered:
- Wsl2 networking problems: WSL2 networking has various problems, anyway, wSL1 can be returned to the network. But WSL1 does not support Docker.
- So I used WSL1 to download docker, and then switched back to WSL2.
- However, the network will inevitably be used during the use of Docker, so it is still a pit.
- You can use Docker Desktop for Windows, but it doesn’t make sense.
- So I switched to a virtual machine without any problems.
5. Use Docker
-
Docker requires root permission to use docker. In order to avoid entering sudo every time, you can add the user to docker user group. Docker user groups have the same permissions as root.
-
Docker is a server-client architecture that needs to be started before use.
# service usage $ sudo service docker start # or systemctl $ sudo systemctl start docker Copy the code
6. Image file (also known as image/image)
Docker packages applications and dependencies into an image file that uses a separate file system. This image file is used to generate container instances. An image file can have multiple instances. You can think of the image file as a class and the container as an instance. Like classes, image files can inherit from other image files. Plus your own Settings. Generally speaking, they use image files made by others to modify them. Self-made image files can be uploaded to Docker Hub for others to use.
7. Container files
As mentioned earlier, the container file is an instance of the image file. A container file is also a file, which may be terminated as soon as it runs, or may run forever and need to be closed manually. Closing the container file does not mean deleting it. The container file will occupy space. You can manually delete it as required.
8. Dockerfile file
Dockerfile is a text file used to generate an image file. You can also create a.dockerignore file in the same directory as Dockerfile, similar to.gitinore, but without packing the file declared here. Dockerfile is written as follows:
The image file generates an instance of a container that can run the node command. The image version is 8.4
FROM node:8.4
# COPY: COPY files from the host's. Directory to the image /app directory (ignoring.dockerignore)
COPY . /app
# WORKDIR indicates that the next working directory is /app
WORKDIR /app
# RUN: install all dependencies through NPM install and pack them into the image file.
RUN ["npm"."install"]
# EXPOSE exposes the container's 3000 port to interact with the outside of the container (see an example later in this article)
EXPOSE 3000
The # CMD command, which represents the command to be executed after the container is generated, is different from RUN, one in image and one in the container
CMD node 01.js
Copy the code
After creating the Dockerfile file, you can use the docker image build to generate the image file.
# untagged (default latest)
$ docker image build -t koa-demo .
# tag$docker image build -t koa-demo:0.0.1# Note the last., which indicates where the Dockerfile file is located
Copy the code
8.1 the image list
Docker Image Ls or Docker Images
8.2 the image download
You can download the image file directly from Registry. You can open DockerHub in your browser and search for images, or you can use Docker Search directly on the command line. After searching for the image to download, use docker pull :[tag] to download the latest version.
8.2.1 Union File System
When you download the image file, you will find that you are downloading many small files each time. This is because the resources of the image file can be shared. If you download another image file later, there is no need to download it again if some small files overlap with the one you downloaded earlier.
8.3 image deletion
- Deleting a single container
Docker rmi -f <REPOSITORY or IMAGE ID>
- Delete multiple containers
docker rmi -f <image1> <image2> <image3>
- Delete all containers
docker rmi -f $(docker images -aq)
9. Container generation and running
Docker container run indicates this command.
$ docker container run -p 8000:3000 -it koa-demos:first-try /bin/bash
Copy the code
Explain this command:
- The first one is docker Container Run, that’s the command what’s the point
- -p indicates the corresponding information of the port. 8000:3000 means that port 8000 of the host corresponds to port 3000 of the container. I wrote it in my Dockerfile earlier
EXPOSE 3000
This means that port 3000 of the container is exposed, so it can be connected through port 8000 of host. - -it indicates that the shell of the container corresponds to the shell of the host, indicating that the command entered in the host window will be transmitted to the container.
- Koa-demos :first-try refers to the container name followed by a colon followed by the label name
- /bin/bash refers to the first command executed after the container is started. Bash is executed here to ensure that the shell can be used after the container is run.
After executing the above command, we’ll notice that the terminal prefix has changed and we’re inside the bash terminal of the container. (If you do not add the -it parameter, you will not enter here.)
root@xxxxxx:/app#
Copy the code
At this point we are already in the container. Using the Docker Container LS, we can see that the container is in progress (of course, since the terminal window is occupied by the container, you have to change the terminal window).
9.1 Exiting a Container
- If the container is entered immediately after run:
- If you want to close this container on exit, you can use it on another terminal
docker container kill <id>
Command, or enter exit (or CTRL + D) in the container. - If you just want to exit and do not want to close the container, hold CTRL + P + Q.
- If you want to close this container on exit, you can use it on another terminal
- If the container is running in the background, enter the container through exec:
- Using exit is the same as CTRL + P + Q. To close the container, use either stop or kill.
- If the container is running in the background, enter the container by attaching:
- As with Run, exit closes the container on exit, CTRL + Q + P does not
9.2 Re-entering a Container
The previous section mentioned the exec and attach commands, both of which can be used to access a running container. The difference between the two is:
- Exec is a dummy terminal opened after entering the container. This is why entering exit through exec does not terminate the container, since only the newly created terminal is pushed out. The old terminal is still there. Let’s say you execute a script in a container that continuously prints logs. If you enter the container using exec, the logs are not visible because they are output on a different terminal.
- Note that exec itself runs a command in the container, but can be used to enter the container. So add it and /bin/bash
- Attach directly corresponds to the input and output streams of the container. Typing exit at this point closes the container on exit.
9.3 Deleting a Container
You can manually delete the container using docker container rm
. If necessary, you can also add the –rm parameter when creating the container to indicate that the container will be automatically deleted when closed. Generally used for testing containers.
$ docker container run --rm -p 8000:3000 -it koa-demos:firt-try /bin/bash
Copy the code
10. Some other commands
10.1 docker container start
If you use docker Container run , a new container is generated. If you want to use containers that have been terminated before, use docker container start
.
10.2 docker container stop
Closed container
- If you are using
docker container kill <containerID>
, a SIGKILL signal is issued to force the container to stop running, and all ongoing data is lost. - If you are using
docker container stop <containerID>
, will send a SIGTERM signal to the container, the container will receive some cleaning, and then terminate the operation, or ignore the signal.
10.3 docker container cp
Copy files
You can use this command if you want to copy the container files outside. Usage:
$ docker container cp <containerID>:[path/to/file] [path/in/host]
Copy the code
10.4 Starting containers in the Background
The docker container run -d -d parameter indicates the background startup, where there is a pit. If a container starts without a foreground process, Docker terminates the container. Therefore, if you run the Docker container run -d Ubuntu and use the Docker container ls, you will find that you cannot see the container. Only -a can see the closed container.
10.5 log
Use docker logs -f -t
to see what happens to the container after it runs. -t means to view the timestamp. -f means to track the output in real time. For example, if you use a script to view it all the time, and then use a log to view it, you can see that the result is constantly updated.
10.6 Viewing Processes
docker top <containerID>
10.7 Viewing Metadata
docker inspect <ID>
11. Underlying principles of Docker
- Docker uses a client-server structure. The daemon process resides on the host and the client accesses the process through sockets.
- The server executes the command after receiving the command from the client.
11.1 Docker architecture
- Note the difference between repository and Registry. Repository is where the image is actually stored. Registry is just a collection of Pointers. In the use of
docker images
The first column is REPOSITORY.