PaRaD1SE

Docker Personal Experience

Published: 2021/9/21 Views: 5575

Categories: 

Development

My docker personal experience

Docker summary

When I wrote my website and needed multiple service frameworks, Docker appeared in my Google search results. So what is Docker? His icon, a ship full of containers, explains this very vividly. bg2018020901.png

If a factory can build one ship, the factory can build countless ships. ——I don’t know who said it

It is a tool that combines multiple services used in a project, At the same time, it is also equivalent to an application program structure design diagram. How complicated an application structure is, it can be automatically installed and started normally with a single command. When a project uses more and more different services, Docker becomes indispensable. Regardless of the application or the various hardware and software configurations, we only need to figure out how to correctly install Docker on the server at the beginning, and then we will be comfortable.

Some basic nouns

Image

Image, an image is a design drawing of one of the services in an application, and multiple images are combined into a complete design drawing. We will define a relatively independent part of the project as a service, and configure the Image of this service with Dockerfile.

Dockerfile

This is the script for building the Docker image. When building the image, it will execute the commands in it, such as installing the dependent libraries required by some services. Or copy our code file to the mirror.

Container

After we build the image, we can start the service. At this time, the image can be instantiated with a single command, which will create a container container, and the service will run in this container at this time. We can use the docker exec <container name> command to execute shell commands directly in the container.

Docker Compose

However, our application uses multiple services, and we need to run many commands to start all services. Sometimes we need to add some complex parameters to the startup command, such as adding data volume volume, binding port ports, Or add the environment variable environment to make the container run correctly. At this time, we can use the docker-compose tool to configure all the configured images, And the code that needs to be executed at startup is written in a .yml configuration file, and then the entire application can be deployed directly with one command.

Talk of experience

About the use of Docker, all can be found in official documents, and some basic experience can also be found in major forums and search engines. I will just add a record of the practical methods that I have summarized in development and operation and maintenance for your reference.

Regarding the writing of Dockerfile, each RUN command will create a new mirroring layer, and more mirroring layers will lead to more space occupation. Therefore, we should adhere to the idea of ​​using as little as possible, and use && to combine multiple RUN commands into one. However, when you update your application later, add new features or even fix bugs, you need to rebuild the mirror. These RUN commands will run again, which not only wastes bandwidth, but also wastes time. Moreover, if a layer of mirroring changes, the cache of subsequent mirrors on this layer will be invalidated, leading to reconstruction.

If you update new features more frequently in the production environment, A more elegant approach is to not excessively seek to reduce the mirror layer, but to change the least likely to occur, And put the most time-consuming commands to the front as much as possible, such as installing some low-level dependency packages. Then put the things that change most frequently and do not require network resources to the end, such as copying code files. In this way, the time-consuming parts will only be re-executed when necessary, and we don’t need to pretend to make a cup of coffee if we are eager to go online to see the effect.

Finally, I will send you a word,

Unless you know what you are doing, don't use Alpine Linux for the basic image. It's nice to be small, but you'll know the pain when debugging.

Tags:

Docker
Development

Previous

Next