Hot Reloading with Local Docker Development

tl;dr You can find the source code for a bare-bones dockerized python HTTP server with hot reloading using fastapi on this Github page.

I recently came by this tweet:

While there are many reasons to have separate Docker images for your development and production environments, it may be overkill for a small project you’re just starting. Over the past several years, I’ve been using the volume mounting feature in order to:

  1. Use the same Dockerfile for local development and production.
  2. Test against a locally running docker container.
  3. Have the docker container reflect changes I make to the source code on my host machine.

Following the instructions at fastapi, I’ve created a very simple python server:

The server can be started locally with uvicorn src/main:app --reload and tested with curl -X GET http://localhost:8000. Since we started the server with the --reload flag, modifying the return value of read_root will dynamically modify the return value from the http request.

In order to Dockerize our application, we add a Dockerfile and a docker-compose.yaml file:

The server can be started locally with docker-compose up -d and tested with curl -X GET http://localhost:8008. Note how on line 5 of the Dockerfile, the source code is copied over from our local directory into the working directory of the docker image. However, if you modify the code locally, these changes will not be reflected inside the container and the image will need to be rebuilt.

The solution is quite simple. Simply add the following volume mount to your service definition in the docker compose file:

volumes:
- ./src:/usr/src

The above command will override the COPY operation we did while building the image and have the code inside the container reflect any changes that were made on your local machine. The final docker-compose.yaml file will look like so:

People call me Olshansky