How to rebuild Docker container on file changes?


Docker is a widely used containerization solution that allows programmers to easily package and distribute software in a lightweight and portable manner. The capability of rebuilding a container once modifications are made to its files is one of Docker's key features. This can be very helpful for a number of things, like making sure that code changes are appropriately reflected in a development environment or that code updates are always reflected in a containerized application.

In this article, we will go into Docker's crucial feature and examine how it may be used to rebuild a container when files are changed. We will also cover some of the concepts related to Docker and provide examples to demonstrate the process of rebuilding a container on file changes.

Let us understand some concepts related to Docker for rebuilding the container on file changes along with exploring some examples.

Docker Compose

Multi-container Docker applications are developed and run using a tool called Docker Compose. With Docker Compose, we can define the services that make up our application in a docker-compose.yml file and then use the docker-compose command to manage and orchestrate the application.

This allows us to easily define and run complex applications made up of multiple containers, simplifying the process of managing and deploying multi-container applications. Additionally, Docker Compose supports a variety of features such as environment variable substitution, container linking, and volume mounting, making it a powerful and convenient tool for developing and running multi-container Docker applications.

Rebuilding a Container

To rebuild a Docker container, a new image must be created based on the changes made to the files within the container. This is accomplished by using the docker-compose up command with the --build flag, which instructs Docker to rebuild the container and create a new image based on the changes made to the files.

Rebuilding a container can be useful for a number of purposes, such as ensuring that code changes are reflected in a development environment, or making sure that a containerized application always has the latest updates. Additionally, rebuilding a container can be a useful way to ensure that code modifications are properly reflected in a production environment, as it allows us to update the container with the latest code changes and deploy the updated container to our production environment.

The --build Flag

When the docker-compose up command is used, the --build flag instructs Docker to rebuild a container. When this flag is set, Docker will immediately rebuild the container in light of any changes made to the container's files.

This can be very helpful for a number of things, like making sure that updates to the code are always reflected in a containerized application or making sure that a development environment reflects code changes.

Example

Here is an example of How to rebuild Docker container on file changes −

Step 1 − Navigate to the directory for your project −

$ mkdir directoryname 
$ cd directoryname

Step 2 − Create a docker-compose.yml file in your project directory (or make changes if already existing) and specify the services that you want to build −

version: '3' 
services: 
 web: 
   build: . 
   command: python app.py 
   volumes: 
   - .:/code 
   ports: 
   - "5000:5000"

Here, we are using a python file, app.py for our example purpose but your project file might contain different files.

The python file app.py which we are using for this example, has the following code −

from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World!' 
if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')

Your project must also have a Dockerfile in the project directory with its contents like −

FROM python:3 
COPY . /code 
WORKDIR /code 
RUN pip install -r requirements.txt 
EXPOSE 5000 
CMD ["python", "app.py"]

Here we also need requirements.txt file in the project directory (you may or may need need it depending on your services) with its contents −

flask

Step 3 − We need to run the following command to recreate the web service specified in this docker-compose.yml file −

$ docker-compose up --build

The docker-compose up command can be used with the --build flag to tell Docker to rebuild a container each time the command is executed.

Output

[+] Running 2/2 
- Network examp_default Created 0.9s 
- Container examp-web-1 Created 0.1s 
Attaching to examp-web-1 
examp-web-1 | * Serving Flask app 'app' 
examp-web-1 | * Debug mode: on 
examp-web-1 | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. 
examp-web-1 | * Running on all addresses (0.0.0.0) 
examp-web-1 | * Running on http://127.0.0.1:5000 
examp-web-1 | * Running on http://172.19.0.2:5000 
examp-web-1 | Press CTRL+C to quit 
examp-web-1 | * Restarting with stat 
examp-web-1 | * Debugger is active! 
examp-web-1 | * Debugger PIN: 630-981-535

Step 4 − Navigate to the address provided in the output on your browser.

This result demonstrates that after being rebuilt, the web service is now executing the revised code inside the container.

Conclusion

In this article, we learned that to rebuild a Docker container on file changes, we need to use the docker-compose up command with the --build flag. This will rebuild the container with any changes made to the files within it, creating a new image. Docker Compose simplifies the process of defining and running multi-container Docker applications. Rebuilding a container is useful for ensuring code changes are reflected and applications have the latest updates.

Updated on: 06-Sep-2023

50K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements