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.
Prerequisites:
- Docker installed in your machine. Please checkout this link and install which fits with your OS.
- 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.
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
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
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.
From Metamask menu click on Import Account
Paste the private key and import
After importing you will see 100 ETH in the account
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 ….