
Automating the test stage
Now that you have been introduced to make, let's modify our Makefile to do something that is actually useful, and execute the various actions performed during the test stage. Recall that the test stage involves building the first stage of the Dockerfile as a service, called test, and then running the test service, which by default will run the python3 manage.py test command, executing application unit tests:
.PHONY: test
test:
docker-compose build --pull release
docker-compose build
docker-compose run test
Note that rather than building the test service in the Docker Compose file, we actually build the release service and specify the --pull flag, which ensures that Docker will always check whether there are any newer releases of the Docker image referenced in the FROM directive. We build the release service this way because we only want to build the entire Dockerfile once, rather than rebuild the Dockerfile on each stage execution.
This guards against the unlikely, yet still possible, scenario where you could pull a newer base image if rebuilding during the release stage, which may result in a different runtime environment to what you tested in the test stage. We also run the docker-compose build command immediately afterwards, which ensures all services are built before we run tests. Because we built the entire Dockerfile in the previous command, this will ensure any cached images for other services are updated to the newest image build.