Difference between dockerfile and docker-compose Let's briefly understand the usage process of docker, which is divided into image building and container startup.
Image construction: that is, create an image that contains the environment, program code, etc. required for installation and operation. This creation process is done using a dockerfile.
Container Startup: The container is finally run by pulling the built image and starting the service through a series of running instructions (such as port mapping, external data mounting, environment variables, etc.). For a single container, this can be run via docker run.
If it involves running multiple containers (such as service orchestration), it can be implemented through docker-compose, which can easily run multiple containers as a service (of course, only one of them) and provides the function of scale (service scaling).
Brief summary:
1.dockerfile: build the image;
2. docker run: start the container;
3. docker-compose: Start the service;
Start from the beginning. If you don't use docker, how do you build wordpress? Find a server first, assume its OS is Ubuntu, and then follow the documentation step by step to type the command and write the configuration, right? What about using docker? Just find a server, no matter what operating system, as long as it supports docker, docker run ubuntu, docker will pull the latest Ubuntu image from the official source, you can think of it as you opened an Ubuntu virtual machine, and then install it step by step, just like above.
However, this installation has a significant drawback: once the container is deleted, you don't have any work done. Of course, you can use docker commit to save it as an image, so that it can be reused.
But the image is generally large, and if you only share the image, others will not know what your image contains, which is not conducive to sharing and reuse. An intuitive solution is to write a script to record the entire installation process, so that when you install it again, you can execute the script. A Dockerfile is one such script that documents the process of making an image. With a Dockerfile, just run docker build . You can create an image, and the Dockerfile is a text file, which is very easy to modify.
Now that you have a WordPress image, you just need to docker run to get WordPress up and running.
If it's just WordPress, that's enough. But many times, multiple images are needed to start a service, such as nginx, mysql, mail service, etc. More commonly, nginx, mysql, and smtp are all images, and then these images work together to serve a project. docker-compose is the solution to this problem. Information about which images your project needs, how to configure each image, which volumes to mount, and so on is included in docker-compose.yml. To start a service, you only need to docker-compose up, and you only need to docker-compse stop/down to stop it
In short, a Dockerfile records the construction process of a single image, docker-compse.yml it records the construction process of a project (usually multiple images).
You said that some tutorials use dockerfile+docker-compose because docker-compose.yml itself does not have information about the image build, and if the image is pulled from the docker registry, then the dockerfile is not needed; If the image needs to be built, then a Dockerfile needs to be provided.
docker-composeIt is an orchestration container. For example, you have a php image, a mysql image, and an nginx image. If there is no docker-compose, then every time you start up, you need to type the startup parameters of each container, environment variables, container name, specify the link parameters of different containers, and a series of operations, which is quite cumbersome. After using docker-composer, you can write these commands in docker-composer.yml file at once, and every time you start the entire environment (including 3 containers), you only need to type a docker-composer up command.
dockerfileThe role is to build a mirror image from scratch. It contains the environment, program code, etc. required for installation and operation. This creation process is done using a dockerfile. Dockerfile - Prepared for the docker build command to create a standalone image, which can also be used in docker-compose for live build docker-compose.yml - A script for docker-compose that can manage multiple containers at the same time, including relationships between them, whether to build with an official image or yourself, various network port definitions, storage space definitions, etc |