Testing Smart Contract with Docker

Good code is usually followed with good test. The same rule also apply with smart contract creation. Truffle framework comes along with automated testing framework in Javascript and Solidity language. Another tools to help is Ganache. Ganache is a personal ethereum network that can be use to deploy contracts, develop application, and run tests.

Setting up solidity contract testing can be as below:

  1. Run Ganache as your ethereum network for testing. You don’t need to think ether when using this. You can simply configure on your ganache command.
  2. Deploy the contract.
  3. Run your test!

You need to run the step one by one every time you do your test. But with helps of docker compose you only need one command to run all step.

In this post, we will set up docker and docker compose to run our test. We will run a docker compose file that runs ganache docker service and our truffle test docker service. These two services will communicate each other to run our tests.

All code on this post can be found on Github.


For this post, we use a simple contract that have function to add number on total storage.

With contract above we will create tests with these scenario:

  1. The contract deployed and total storage is 0.
  2. Transact contract with addTotal function and total storage added to 5.

Before testing the scenario, with “beforeEach” keyword, we deploy contract “AddContract”.

Docker Preparation

Creating docker images needs a Dockerfile. For the Dockerfile, I’m using juwitaw/truffle-base-image as the base image. All the truffle dependencies already included on the base image. So the additional layer on Dockerfile is only to add our code to docker and install additional dependencies.

As for the docker compose file, we created 2 services named ganache and truffle_simpletests.

Ganache service use ganache docker image from trufflesuite docker hub. Our testing will connect to this service instead of ethereum main network, rinkeby, ropsten or other test network.

truffle_simpletests service is our contract services. This service contains our contract code to deploy on ganache and run our test codes. As seen on above snippets, this service runs “truffle test — network dev” for run our tests. It depends to ganache service so this service will run after ganache service up.

Running Test

To run the test, we will you docker compose command:

docker-compose -f docker_compose_test.yml

Ganache service will up and truffle_simpletests runs truffle test. It will produce test result like below.


We use ganache to easily test without to wait mining time. This is good for doing test to check your contract functions. But before you want to deploy on main network, I recommend to test on test network (rinkeby, ropsten, kovan, etc).

What’s next?

We can say that the current contract we made in this post still the basic contract. So, next we will use other real world use case contract and get into test coverage!

Join our community on Telegram!