But we have a problem here. Our static website is a sample application. But in reality, we generally work with bigger apps. If we make any changes to the app, it is time taking and heavy to build the image again. At such times, docker-compose comes to our rescue for this problem.
Let us create the docker-compose.yml file inside the project directory, and write the code as displayed in the image.
Let us now understand the terms used here.
version: it is the docker-compose version we want to use. This generally depends on the docker engine version installed on the machine. You can check the docker engine version b running docker version command in your console. Further, you may refer to the versions of docker-compose based on docker engine version here.
services: tell docker-compose about the services in the project. For now, we only have one service named welcome. You can out the service name as you wish.
tty: terminal for container. If true, we are enabling the terminal.
build: build specifications
context: path to folder whose files will be used by the build context. These files will be accessed by docker daemon(docker server) during the build so it can use them in the filesystem of the image. Since the docker-compose file is inside the project folder, we set context to current directory.
dockerfile: path to Dockerfile of the project. Since it is present in the current directory, we just mention the name Dockerfile.
ports: just like we set -p 8080:80 while executing docker run -p 8080:80 , here we mention the same mapping of ports.
volumes: The volumes key mounts the html directory (in the local machine) on the host to /usr/share/nginx/html/ inside the container, allowing you to modify the code on the fly, without having to rebuild the image.
We have already discussed that /usr/share/nginx/html/ is the default place referred by nginx server for HTML files to be served. So if make any changes to HTML files in local system, those changes will be reflected in the container too, because of this mapping.
Thus, this method removes the heavy task of rebuilding the images if any changes are made.
Remember to put a space after every “:” of the key words.