In this post we're going to learn how to configure watchman to reload a django development server faster.
The enviroment we're going to create is quite simplistic, a hello world django app using watchman as reloader. If you want to add a postgres database, here you have more information if you want to add a database.
The image we are going to use is alpine, to take the advantage of a small image that use less resources.
In alpine, the watchman package is on the edge testing repo, so we need to add that repository to the list of repositories.
Here we install bash, in case you want to access to a shell in your container.
FROM alpine
ENV PYTHONUNBUFFERED 1
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN apk update --no-cache && \
apk upgrade && \
rm -fr /var/cache/apk/*
RUN apk add \
bash \
py3-pip \
watchman
RUN mkdir /var/run/watchman/
RUN mkdir /code
COPY requirements.txt /code/
WORKDIR /code
RUN pip3 install setuptools==41.6.0 wheel==0.33.6
RUN pip3 install --upgrade pip && pip install -r requirements.txt
COPY . /code/
Example requirements.txt file for the project:
Django
pywatchman
We need to install pywatchman. With pywatchman and the Watchman service, kernel signals will be used to autoreload the server, for large projects, that configuration improves the performance reloading the code.
We can add a docker-compose.yml file, that will be handy if you want to add more services into the environment.
version: '3.1'
services:
website:
container_name: website
build:
context: .
ports:
- 8000:8000
command: ["python3.8", "manage.py", "runserver", "0.0.0.0:8000"]
restart: always
environment:
DJANGO_WATCHMAN_TIMEOUT: 20
volumes:
- .:/code
Here the more relevant configuration is DJANGO_WATCHMAN_TIMEOUT
.
In this example is configured to 20 (seconds). That's the time django project will wait to have a response from watchman on the startup. If django didn't receive an answer 20 seconds, will use StatReloader, printing something like that in the console:
Watching for file changes with StatReloader
That's the default django reloader. Watchman takes a bit to start, that amount of time can differ on your machine, try with a higher value.
Let's start our application, following the container logs:
docker-compose build
docker-compose up -d
docker-compose logs -f
Now, if we visit our test app in a browser (http://0.0.0.0:8000), we'll see the django default website after a successful installation:
The example project github repo: https://github.com/jesusenlanet/django-watchman-example
If you want to exclude some folders from the watchman scope, add a .watchmanconfig
file like that:
{
"ignore_dirs": [".git", ".idea", "venv"]
}
And add that folder to the ignore_dirs
list.
May 28 2020 *Last updated May 31 2020
#docker #development #docker-compose #python #django #watchman