Reloading your development environment as you make changes to your code is an obvious step for developers working with projects locally. However, if there’s no setup for efficient/fast auto-reloading, this can be quite a productivity killer, especially with a modern heavy-weight dev stack where stopping/starting manually could often be quite slow. Most popular, modern frameworks and stacks often ship with built-in capability for auto-reloading with file changes for this very reason. But if you are moving to a docker-based development environment, those solutions won’t be applicable out of the box. This is due to project files being copied to the virtual container during the build process. In this article, let’s look at a couple of approaches around how you can auto reload docker environment when:
Your stack doesn’t come with built-in auto-reload capability
Your environment has built-in auto-reload functionality, but after docker integration, you lost that due to project files being moved to the docker image.
Note: The examples provided here are for Python/Django with Docker, but they should be applicable to any language/development stack in general.
#1 Mount project directory as external volume:
The very first basic intuition we can have to solve this reloading problem within the Docker environment is to make Docker use the current working directory somehow. However, it doesn’t work like that out of the box due to the portability feature, one of the main reasons to use Docker in the first place. However, we can achieve exactly that with some minor additional setup. It uses Docker’s volume mounting feature to reflect code changes in the container without any external restart mechanism.
This works well if your development stack comes with auto-reload capability itself. This is also the fastest method to auto reload Docker, as most development stacks already optimize for reloading on file changes.
The steps for this approach include:
Defining an explicit external volume(which is defined as the current working directory of the project) (compose.yml/docker-compose.yml file)
Mount that volume to the application directory within docker. (compose.yml/docker-compose.yml file)
Use the application directory as the current working directory (Dockerfile)
The fastest way to get your environment auto-reloaded with very low latency between and services refreshed.
Cons:
Support from the dev stack is required for the auto reload feature.
#2 Auto Reload docker using built-in “watch” functionality:
In this second approach, we would leverage the built-in auto-reload functionality(aka “watch”) provided by the Docker package itself. This approach requires:
Defining “develop” specific watch behaviour
Running “docker compose”/”docker-compose” command with the “–watch” flag
Also, the command for running the Docker project would look something like:
docker-compose up --build --watch
If things are set correctly, it should show a “watch enabled” status log while starting the container(s):
To learn about these configurations in more detail, please refer to Docker’s watch and develop documentation.
Pros:
Works regardless of whether or not the development stack comes with an auto-reload feature.
Cons:
This is typically slower than the previously described “volume mount” approach.
Conclusion:
Which approach to use would depend entirely on the development environment and your preference. As a rule of thumb, if the development stack ships with an auto-reload feature, the volume mount approach would be more efficient. If not, with a “watch” configuration, you can simply supercharge your productivity that wouldn’t have existed without docker. In a multi-container approach, a mix of both could also be a viable choice as well.
var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("%7B%22overlayOptions%22%3A%7B%22colorTheme%22%3A%22light%22%2C%22enableInfScroll%22%3Atrue%2C%22enableFilteringOpensOverlay%22%3Atrue%2C%22enablePostDate%22%3Atrue%2C%22enableSort%22%3Atrue%2C%22highlightColor%22%3A%22%23FFC%22%2C%22overlayTrigger%22%3A%22submit%22%2C%22resultFormat%22%3A%22expanded%22%2C%22showPoweredBy%22%3Atrue%2C%22defaultSort%22%3A%22relevance%22%2C%22excludedPostTypes%22%3A%5B%5D%7D%2C%22homeUrl%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%22%2C%22locale%22%3A%22en-US%22%2C%22postsPerPage%22%3A5%2C%22siteId%22%3A18994550%2C%22postTypes%22%3A%7B%22post%22%3A%7B%22singular_name%22%3A%22Post%22%2C%22name%22%3A%22Posts%22%7D%2C%22page%22%3A%7B%22singular_name%22%3A%22Page%22%2C%22name%22%3A%22Pages%22%7D%2C%22attachment%22%3A%7B%22singular_name%22%3A%22Media%22%2C%22name%22%3A%22Media%22%7D%7D%2C%22webpackPublicPath%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-content%5C%2Fplugins%5C%2Fjetpack%5C%2Fjetpack_vendor%5C%2Fautomattic%5C%2Fjetpack-search%5C%2Fbuild%5C%2Finstant-search%5C%2F%22%2C%22isPhotonEnabled%22%3Afalse%2C%22isFreePlan%22%3Atrue%2C%22apiRoot%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-json%5C%2F%22%2C%22apiNonce%22%3A%22155bc22a78%22%2C%22isPrivateSite%22%3Afalse%2C%22isWpcom%22%3Afalse%2C%22hasOverlayWidgets%22%3Afalse%2C%22widgets%22%3A%5B%5D%2C%22widgetsOutsideOverlay%22%3A%5B%5D%2C%22hasNonSearchWidgets%22%3Afalse%2C%22preventTrackingCookiesReset%22%3Afalse%7D"));
Leave a Reply