Testing Docker Images - Tools and Best Practices

5 minute read

Testing is a crucial aspect of software development, and Docker images are no exception. Properly testing Docker images ensures their reliability, security, and compatibility across different environments. In this blog post, we will explore the importance of testing Docker images and discuss various tools, including Bats, that can aid in this process.

The Importance of Testing Docker Images

Testing Docker images provides several benefits, such as:


By thoroughly testing Docker images, you can identify and fix issues before deploying them, ensuring a higher level of reliability and stability in production environments.


Testing helps ensure that Docker images function correctly across different platforms, operating systems, and versions of dependencies, avoiding potential compatibility issues.


Docker images often contain sensitive data or critical components. Testing helps identify security vulnerabilities and ensures that proper security measures are in place.

Testing Strategies for Docker Images

Unit Testing

Unit tests focus on testing individual components or functionalities within the Docker image. It involves writing tests for each isolated part of the image, such as specific scripts, configuration files, or libraries.

Integration Testing

Integration tests check the interactions between different components within the Docker image. This ensures that the image functions as a cohesive unit and that all integrated parts work together correctly.

Acceptance Testing

Acceptance tests verify whether the Docker image meets the requirements and expectations defined by stakeholders. These tests typically simulate real-world scenarios and use cases to ensure the image behaves as intended.

Useful Tools & Frameworks for Testing Docker Images


Bats (Bash Automated Testing System) is a popular testing framework specifically designed for Bash scripts. It allows you to write simple and concise test cases using Bash syntax, making it ideal for testing Docker images. Bats provide an easy way to validate scripts, execute commands within the container, and assert expected outcomes.

Here’s an example of a Bats test case for a Docker image:

#!/usr/bin/env bats

@test "Ensure web server is running" {
  run docker run -d --name myapp myapp-image
  run docker exec myapp curl localhost

  [ "$status" -eq 0 ]
  [ "$output" = "Hello, World!" ]

@test "Verify database connection" {
  run docker run -d --name db mydb-image
  run docker exec db psql --version

  [ "$status" -eq 0 ]
  [[ "$output" =~ "psql (PostgreSQL)" ]]


Testinfra is a testing framework that allows you to write tests in Python to validate infrastructure and system state. It provides a high-level, Pythonic API for executing commands inside Docker containers and making assertions about their state. Testinfra complements Bats by providing a Python-based testing solution for Docker images.

Here’s an example of using Testinfra to test a Docker image:

import pytest
from testinfra import DockerContainer

# Define a fixture to create a Docker container
def docker_container():
    # Run the Docker container
    with DockerContainer("myapp-image") as container:
        yield container

# Test to ensure the web server is running
def test_web_server_running(docker_container):
    # Check if the web server process is running
    assert docker_container.process.get(comm="nginx").args == "/usr/sbin/nginx -g 'daemon off;'"

    # Check if the web server is listening on port 80
    assert docker_container.socket("tcp://").is_listening

    # Make an HTTP request to the web server
    response = docker_container.run("curl http://localhost")
    assert response.stdout.strip() == "Hello, World!"

# Test to verify the database connection
def test_database_connection(docker_container):
    # Check if the database process is running
    assert docker_container.process.get(comm="postgres").args == "/usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main -c config_file=/etc/postgresql/12/main/postgresql.conf"

    # Check if the database port is open and accepting connections
    assert docker_container.socket("tcp://").is_listening

    # Connect to the database and execute a query
    response = docker_container.run("psql -U postgres -c 'SELECT version();'")
    assert "PostgreSQL" in response.stdout

# Run the tests


Serverspec is a Ruby-based testing framework for infrastructure testing. It enables you to write tests in Ruby to verify the state of your Docker image. Serverspec provides a DSL for defining assertions related to packages, files, services, ports, and other infrastructure aspects.

Here’s an example of using Serverspec to test a Docker image:

require 'serverspec'
require 'docker'

# Set the backend to Docker
set :backend, :docker
set :docker_url, ENV['DOCKER_HOST']
set :docker_image, 'myapp-image'

# Verify the web server
describe package('nginx') do
  it { should be_installed }

describe service('nginx') do
  it { should be_enabled }
  it { should be_running }

describe port(80) do
  it { should be_listening }

describe command('curl http://localhost') do
  its(:stdout) { should match /Hello, World!/ }

# Verify the database
describe package('postgresql') do
  it { should be_installed }

describe service('postgresql') do
  it { should be_enabled }
  it { should be_running }

describe port(5432) do
  it { should be_listening }

describe command('psql -U postgres -c "SELECT version();"') do
  its(:stdout) { should match /PostgreSQL/ }

It’s worth mentioning that these are just a few examples, and there are many other testing frameworks and tools available for testing Docker images. The choice of framework or tool depends on your specific requirements, the programming languages and technologies used in your Docker image, and the types of tests you want to perform.

Best Practices for Testing Docker Images

Automate Tests

Automating your tests ensures consistency and reduces human error. Use tools like Bats, Docker Compose, or Testinfra to automate your test suites, enabling easy execution and integration with your CI/CD pipeline.

Test in Isolation

When testing Docker images, isolate the testing environment from external dependencies to maintain consistency and avoid interference. Use Docker Compose to define any necessary dependencies within the test environment.

Reproducible Tests

Ensure that your tests consistently yield the same results, regardless of the testing environment or the execution context of your Docker images. This reliability enables you to have confidence in the accuracy and stability of your tests, allowing you to detect and address issues promptly, maintain the desired functionality of your applications, and facilitate seamless collaboration among team members. By implementing reproducible tests, you establish a solid foundation for maintaining the integrity and reliability of your Docker images throughout the software development lifecycle.


In conclusion, testing Docker images is a crucial aspect of ensuring the reliability, functionality, and adherence to best practices in containerized applications. By incorporating appropriate testing frameworks and tools like Bats, Testinfra, and Serverspec, developers can validate the expected behavior of Docker images and detect any issues or deviations early in the development cycle. These testing frameworks offer different approaches and capabilities, allowing developers to write expressive tests, perform linting, and conduct integration testing.

By investing in comprehensive testing for Docker images, developers can enhance the quality, stability, and security of their containerized applications, leading to more reliable and efficient deployments. With the help of these testing frameworks and a solid testing strategy, developers can confidently build and deploy Docker images with greater confidence and peace of mind.