Run the ganache-cli inside the Docker Container
Google images

Run the ganache-cli inside the Docker Container

Connect the Metamask to ganache-cli running inside the docker container

In this article we’ll run the ganache-cli inside the docker container and then we will connect it to the Metamask and transfer some Ether from one account to another.

Medium Link

Prerequisites:

  1. Docker installed in your machine. Please checkout this link and install which fits with your OS.
  2. Metamask plugin installed to your browser. Please checkout this link and follow the instructions to install it.

Lets first create the project directory by name “docker-ganache”

Inside the docker-ganache, create a docker file by name Dockerfile.

Dockerfile don’t require any extension and “D” must be uppercase.

Our file structure will look like this

- docker-ganache
  - Dockerfile

Dockerfile is the file where we’ll define all the commands required to create a ganache-cli docker image.

Docker can build images automatically by reading the instructions from a  Dockerfile.

Open the Dockerfile in any editor and paste the below code

# node:alpine will be our base image to create this image
FROM node:alpine
# Set the /app directory as working directory
WORKDIR /app
# Install ganache-cli globally
RUN npm install -g ganache-cli
# Set the default command for the image
CMD ["ganache-cli", "-h", "0.0.0.0"]

Lets take a dip into the code and explore what it is actually doing

The docker container require nodejs installed in it to install the ganache-cli.

The node:alpine is the base image in which latest nodejs is pre-installed.

What does alpine means?

It is minimum requirement needed for nodejs. It just include libraries which are required. For more information please visit this link.

The base image already has a filesystem, for node:alpine

app    dev    home   media  opt    root   sbin   sys    usr
bin    etc    lib    mnt    proc   run    srv    tmp    var

The second step WORKDIR /app we are instructing the Dockerfile to use /app folder as the working directory for this image

The third step RUN npm install -g ganache-cli install the ganache-cli globally.

The last step CMD ["ganache-cli","-h","0.0.0.0"]

When this image run there must be a default command for this else you have to manually tell what it should do.

ganache-cli -h 0.0.0.0 is the default command for this image.

When this image run it will run this default command.

Ganache-cli’s default host is 127.0.0.1 but for docker instance it is 0.0.0.0

Please refer to this link for more information on ganache-cli flags like -h



Lets now build the image

Open the terminal or cli and make sure you’re in docker-ganache directory

Run the below command to build the Dockerfile

docker build .

For the first time it might take sometime. After it complete you might see the output something like below.

Output of docker build

Your output might be little different from this, as in my case, I have tested it couple of times before so everything was saved in cache memory.

Check the output carefully.

Successfully built 7726b19a7bff

This is the image Id of Dockerfile what we just build.

Great all the setup is done, now lets test it.


Run the docker image and start a container

docker run -p 8545:8545 7726b19a7bff

In my case 7726b19a7bff is the image id you should run yours.

What is -p 8545:8545 doing here?

When ganache-cli run on 8545 port, this port is not exposed out of the container. To access this -p 8545:8545 flag expose the 8545 port on 8545 port.

Just for understanding take an example;

-p 3000:8545

Whenever any request will point to 3000 port it will redirect the request to container’s 8545 port. It is not compulsory that other port number has to be same like container.

--publish , -p Publish a container’s port(s) to the host

For more information please refer to this link.

After running the command you’ll see the output like this

No alt text provided for this image

If you see output like this then you’re on right path.


Lets connect our ganache-cli with the metamask

Open the browser and metamask

Select the Localhost 8545 network

No alt text provided for this image

Metamask it now connected to the docker-ganache.

Let import one account to the metamask.

Copy private key of an account from the running ganache instance.

No alt text provided for this image

From Metamask menu click on Import Account

No alt text provided for this image

Paste the private key and import

After importing you will see 100 ETH in the account

No alt text provided for this image

Lets send some ether to other account

  • Click on Send
  • Select Account 1 in To field
  • Enter 10 in Amount field
  • Select Next and Confirm

Once the transaction confirmed check the Account 1 and Account 2.

Account 2 is 89.9998 something. The rest is fee paid for the transaction.


We just connected to the ganache-cli running inside the docker container and made a transaction on Metamask.

Hope you like the tutorial. Please do share it and don’t forget to give it a clap if you learn something from it.

Thanks and stay tuned more to come ….

To view or add a comment, sign in

More articles by Shubham Kumar Chadokar

  • Read of the Week 23rd Aug 2021

    This week's read is about Continous Integration. GitHub Actions: Basics Github Actions — The What, Why, and How How To…

  • Read of the Week Newsletter (RoW) Aug 16th 2021

    This week read list: Monday, Aug 16th 2021 Cloud Object Storage: Store & Retrieve Data Anywhere: Amazon Simple Storage…

  • Read of the Week Newsletter (RoW)

    Hello Everyone, I am starting this read of the week newsletter. Every Monday I will share a list of 5 articles on…

  • Build a Todo App in Golang, MongoDB, and React

    When I was learning Golang, I realized, there are very few tutorials and articles out there which give you a complete…

Others also viewed

Explore content categories