Let’s Play with Docker (giggity)

I don’t know why Docker sounds dirty to me, but I’m apparently a 12 year old boy and it does.

Anyhoo, Docker is going to play a central role in building and maintaining smart contracts that run on Dragonchain. In this post, I’ll share my first experiments creating a simple Docker container, pushing it to Docker hub, and making sure it all works as expected.

Remember from previous posts that all Dragonchain smart contracts are merely Docker containers that get information passed in, do something with that information, and spit information back out. So let’s figure out how they work.

Goals for This Post

At the end of this post, I want to know how to:

  1. Create a simple Dockerfile and build it to run a simple shell script
  2. Run my new Docker container
  3. Push my Docker image (the snapshot of the container) to Docker Hub

Step 1: Create and build a Dockerfile that will run a simple BASH script

So this stuff isn’t really that scary. It CAN be intimidating, but for the most part I think our use cases will be very straightforward.

I want my Docker container to simply run a shell script that will spit out a message, so let’s start with the script called testscript.sh

#!/bin/sh

echo "Hello from a shell script running inside a Docker container!"

Like I said, nothing fancy.

The next step is to create a simple Dockerfile in the same directory as my script that will do a few things:

  1. Name the Docker base image (like Ubuntu, debian, Alpine, etc.)
  2. Copy my script file into the root directory of the new mini-OS
  3. Tell Docker to run that script file when invoked

The Dockerfile (named just that way) will look like this:

FROM alpine:latest
COPY ./testscript.sh /
ENTRYPOINT sh /testscript.sh

Again, nothing fancy. We tell Docker to pull the latest version of the alpine linux base image (alpine is a SUPER lightweight version of linux), copy our test script from the current directory into /, then tell Docker that the “entrypoint” is the simple linux command to run the test script.

Note that we could also add some other commands in there to just make the testscript.sh file executable and run it directly, but no need in this case.

Okay, so next we’ll actually build our Docker container with the following command (where johnwantsmore is my Docker Hub username):

docker build -t johnwantsmore/docker-test .

This will create an image named “docker-test” using the Dockerfile and its instructions. The output should be something like this:

Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM alpine:latest
 ---> 961769676411
Step 2/3 : COPY ./testscript.sh /
 ---> 0b0a1ef72b0e
Step 3/3 : ENTRYPOINT sh testscript.sh
 ---> Running in 45d81dbd37fb
Removing intermediate container 45d81dbd37fb
 ---> 1b4a9cfe89f0
Successfully built 1b4a9cfe89f0
Successfully tagged docker-test:latest

Sweet!

Step 2: Run our new Docker container to make sure it works

Running our new Docker container is super straightforward.

docker run johnwantsmore/docker-test

Should spit out our “Hello from a shell script…” message in the script. Easy peasy!

Step 3: Push our Docker image to Docker Hub

Docker Hub is one of the most important pieces of our Dragonchain smart contract puzzle. Once we have a working Docker image, we need to be able to tell our Dragonchain node where to find it. Enter Docker Hub.

Think of Hub like Github. It’s a place where you can setup your own repositories for Docker container images, publicly or privately. You get one free repository (public or private) when you signup, so you can give this stuff a try for free (though I’ve already upgraded since it’s a) cheap and b) I KNOW I’ll need a bunch of repositories anyway).

The first step for our purposes, then, is to create a repository for our image on Docker Hub. I’ll call the repository “docker-test” (like the name I gave when building above).

That’ll look like this:

Creating a new repository on Docker Hub

Back on the command line, run “docker login” and follow the prompts to log into Docker Hub. Then let’s try pushing our image up to Docker Hub with the following command (replacing “johnwantsmore” with your Docker Hub account name if you’re following along):

docker push johnwantsmore/docker-test

And boom! If I check out my repository page on Docker Hub, I’ll see that I have a new push under the “latest” tag.

And that’s really all there is to the most basic process for building a Docker image and pushing it to Docker Hub in a way that’s suitable for our purposes building Dragonchain smart contracts.

Note that I haven’t gotten into tagging our images properly or the best practices thereof here. I’ll leave that as an exercise for you/your preferences.

So, without any further delay (except the time it’ll take me to figure it out and write it up), let’s create and deploy our first Dragonchain smart contract in the next post!

Until next time,
John

Leave a comment

Your email address will not be published. Required fields are marked *