最近看了一些Docker相关的知识,对制作dockerfile的流程有一些不明白的地方。
假如要制作一个基于Ubuntu的包含Nginx、MySQL、PHP等服务的dockerfile,我现在的流程是:
docker run一个基本的Ubuntu镜像
更新系统依赖包,如:apt-get update,在文本文档记录执行的命令
安装Nginx,在文本文档记录安装Nginx执行的步骤
....
等于说是先把流程跑一遍,记录下所有执行的命令,然后再把这些步骤复制到dockerfile里。
个人感觉这样做是不对的,请问大家都是怎么做的??
That’s right. If you are very familiar with all the processes, you can naturally write the Dockerfile directly. However, there is basically no absolute guarantee for this kind of thing. In particular, most of Docker’s basic system images are very streamlined, and some software dependency packages need to be downloaded and installed. Therefore, although the process of building a container by yourself is a bit time-consuming, it is relatively short. On the contrary, it will save time than dreaming about writing Dockerfile.
Usually writing a Dockerfile is a process like this:
Preview the installation process through the container and record the build commands
Summarize and optimize installation commands, and write Dockerfile
Try to use Dockerfile to build the image and check if there are any problems
Create a container through the built image to ensure that the program in it can be used correctly
By referring to the ready-made dockerfile on docker hub and modifying it according to your own needs, it will be more efficient. To write the dockerfile from scratch, you can only do it like the brothers above. I call it the "video recorder" method.