An Enthusiastic Programmer

Docker Hub Automated Tests


In the preceding article, we discussed the automated builds mechanism on the Docker Hub. And another cool feature of Docker Hub is the Automated Tests functionality.

Docker Hub enables you to build images from the source code, which needs to pass the tests. When you configured automated tests, the Docker Hub only builds the images when the tests passed. If an image failed on the tests, then it won’t be pushed into your docker hub registry.

It is easy to set up an automatic tests pipeline on Docker Hub.

Enable Automatic Tests on the docker hub dashboard

Altopen automated test pipeline

Set up automated test files

Docker Hub will identity files ending with .test.yml suffix as test files. If you specify more than one test file, the Docker Hub will run them sequentially.

The test file should look like this:

     context: .

The file returns 0 determines that the tests passed, and fail otherwise.

In the preceding article, I created a Github repository Docker Hub CI/CD Explanation for demonstration. Here, I am going to add automatic test files for that repository.

my project tests file architecture

│  ├─
│  └─

The docker-compose.test.yml is a test yaml file, which will be executed by the Docker Hub Tests pipeline. You can add more test yaml files, and they will be executed sequentially.

Let us take a look at my docker-compose.test.yaml file contents.

     context: .
     - "8000:8000"

The above snippet builds an image from the current directory and traffic the port to 8000 from 8000 inside the container. command: means that running the script after the image was build and the container was running up.

The image will be pushed into your docker hub registry, only when the tests passed.(aka the returns code 0).


In order to use script. You should make it executable and place it into the environment variables. Or specify the full executable file path in the services.sut.command.

Grant script executable and add it into environment variables with ENV in Dockerfile.
RUN chmod +x /usr/src/app/
ENV PATH="/usr/src/app"

The is a simple Linux script, which was made to customize this demo project’s test for this article. You can change this script to fit your project’s tests.

In this example, the file will execute all unit test files under the tests directory. contents:

#! /bin/bash
# run all unit test files that file name start with "unittest" under "test" dir.
for dir in $(ls tests -1FA | grep "^unittest"); 
    do python3 tests/$dir; 

I hope I made this article clear, and if there was something not clear, please be free to leave it in the comments.