Using Docker and Testcontainers for database integration testing


After a bit of research I was led to think that Docker containers could be a good fit for database integration testing using test containers, as this would only require a Docker container running a database image reproducing the database schema against which the tests would be executed. This instance can either be run locally on each developer’s machine or, even better, one single instance can be shared by multiple developers: an automated test can be configured to start a database instance from the same image in Docker for every test method if needed using the @Rule annotation.

While trying to install Docker for Windows 7 to play with it, I kept getting the following error which seems to be related to VirtualBox:

docker Error creating machine: Error in driver during machine creation: Unable to start the VM: VBoxManage.exe startvm default --type headless failed:

Result code: E_FAIL (0x80004005)
Component: MachineWrap
Interface: IMachine

I will share how I worked around this problem to help other people who may have run into the same problem.

asked on Stack Overflow Apr 16, 2019 by Taoufik Mohdit

1 Answer


To workaround the issue described in the question, I followed the steps below:

#1. Install VMware workstation player#

#2. Run a Linux Ubuntu virtual machine#

Using iso file from Ubuntu website, run an Ubuntu appliance on VMware player

#3. Install Docker on Ubuntu VM#

$ sudo apt-get install

#4. Post-install steps#

###4.1. Create the docker group and add your user###

$ sudo groupadd docker

$ sudo usermod -aG docker $USER

###4.2. Log out and log back in## so that your group membership is re-evaluated

###4.3. Verify that you can run docker commands without sudo###

$ docker run hello-world

#5. Download database image#

In this example it's Oracle 11g:

$ docker pull wnameless/oracle-xe-11g-r2

#6. Run Database image#

(if you plan to connect to the Oracle database instance from testcontainers, this step is not necessary)

$ docker run -d -p 49161:1521 wnameless/oracle-xe-11g-r2

For further details/options refer to docker hub:

#7. Connect to the database#

Using with following settings:

hostname: localhost

port: 49161

sid: xe

service name: xe

username: system

password: oracle

To connect from the host machine use the IP address of the VM instead of localhost . Run ifconfig on the guest Ubuntu VM to get the IP address

#8. Configure Docker to be accessed remotely#

Configure where the Docker daemon listens for connections (Required only if Docker needs to be accessed remotely, i.e. not from the guest Ubuntu system), as by default Docker daemon listens on unix sockets (local connections)

###8.1. Create config file###

Create /etc/systemd/system/docker.service.d/override.conf file with content to override default one (ExecStart=/usr/bin/dockerd -H fd://):

# /etc/systemd/system/docker.service.d/override.conf



ExecStart=/usr/bin/dockerd -H fd:// -H tcp://

To create this file you can run the following unix command:

printf "# /etc/systemd/system/docker.service.d/override.conf\n[Service]\nExecStart=\nExecStart=/usr/bin/dockerd -H fd:// -H tcp://" > /etc/systemd/system/docker.service.d/override.conf

###8.2. Restart Docker###

After saving this file, reload the configuration by running:

systemctl daemon-reload

Then restart Docker by running:

systemctl restart docker.service

###8.3. Check your Docker daemon###

After restarting docker service, you can see the port number in the output of either:

systemctl status docker.service

(You should see something like: /usr/bin/dockerd -H fd:// -H tcp://


sudo netstat -tunlp | grep dock

(You should see something like: tcp6 0 0 :::2376 :::* LISTEN 121686/dockerd)

###8.4. Useful resources###

How do I enable the remote API for dockerd

How to detect a docker daemon port

Configure where the docker daemon listens for connections

#9. Set Docker Host environment variable#

This step is necessary only if you plan to connect remotely to the database container using testcontainers API (e.g. from a Junit test) from the OS hosting the Ubuntu VM (docker dameon is running on the ubuntu VM)

Define environment variable: DOCKER_HOST = tcp://<Ubuntu machine's IP address>:2376 . Note the hostname is the ubuntu VM's. If this environment variable is not defined, testcontainers API (OracleContainer oracleContainer = new OracleContainer("wnameless/oracle-xe-11g");) will be expecting Docker daemon to be running on localhost (See code snippet further below)

#10. Use the database container from a test class#

Using testcontainer API, a Junit test can start a database instance from a the Docker image on Ubuntu's VM, execute queries against it and, eventually shut it down

###Junit test class###

import static org.junit.Assert.assertEquals;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.util.concurrent.TimeoutException;
import org.junit.ClassRule;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.testcontainers.containers.OracleContainer;
public class MyRepositoryIT {
    public OracleContainer oracleContainer;
    public void setup() throws TimeoutException {
        String dockerHost = System.getenv("DOCKER_HOST");
        System.out.println("dockerHost: @" + dockerHost + "@");
        System.out.println("Starting Oracle Container... (" + + ")");
        oracleContainer = new OracleContainer("wnameless/oracle-xe-11g");
        System.out.println("Oracle Container started. (" + + ")");
    public void tearDown() {
        System.out.println("Stopping Oracle Container... (" + + ")");
        System.out.println("Oracle Container stopped. (" + + ")");
    public void whenSelectQueryExecuted_thenResulstsReturned() throws Exception {
        String jdbcUrl = oracleContainer.getJdbcUrl();
        String username = oracleContainer.getUsername();
        String password = oracleContainer.getPassword();
        Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
        ResultSet resultSet = conn.createStatement().executeQuery("SELECT 1 FROM DUAL");;
        int result = resultSet.getInt(1);
        assertEquals(1, result);

###Maven dependencies###

<project xmlns="" xmlns:xsi=""

#Miscellaneous notes#

###Useful docker commands###

  • List images: docker images
  • List all containers: docker ps -a
  • Start a container: docker start [container id]
  • List started containers: docker ps
  • View logs of started container: docker logs [container id]


Installing Docker on Ubuntu

Further details about Post-install steps

Using an Oracle image within Docker

Database Testing With TestContainers

###About Oracle 12c image###

I've tried an Oracle 12c image (sath89/oracle-12c from:

$ docker run -d -p 8080:8080 -p 1521:1521 --name oracle-db-12c sath89/oracle-12c

but it seems to be so slow starting up from testcontainers that the following exception is eventually (after approximately 4 minutes) thrown:

java.sql.SQLRecoverableException: ORA-01033: ORACLE initialization or shutdown in progress.

If the 12c image is started from docker host itself (i.e. Ubuntu), it does start successfully.

answered on Stack Overflow Apr 16, 2019 by Taoufik Mohdit • edited Sep 28, 2020 by Taoufik Mohdit

User contributions licensed under CC BY-SA 3.0