After reading the previous blog post in this series, "Containers, Kubernetes, microservices: Start here", you're now ready to build your first "Hello World" application and run it in a container. For this, we'll be using Go.

    Buildah, Podman, or docker

    Which method you use to build and run your container is based on your operating system and tool selection. Because it is safer (it does not require root access), I'm going to use Podman to build and run my container, knowing that the commands used are 100 percent compatible with the docker command. In fact, you could run alias docker=podman and you'd not know the difference. So, if you are not using podman, simply use the command docker in place of every podman in the following.

    Parts For Building

    You need the code you're going to run, a file to configure/manage the build process, and the tool (i.e. Podman).

    Build Configuration/Management

    We'll create a file called "Dockerfile" that contains the steps and information needed to build an image. The build process is done in layers, with the starting point typically being an operating system or, more likely, an OS and framework combination. In the case where we are using Go, we don't need any framework to be installed. We can compile our code into a binary that is targeted for the OS we choose. In this example, I'm starting with CentOS. We'll then copy our code in the image and we'll give the image a command to be executed when someone runs the image in a container. The following file, "Dockerfile" does those things (except for the compile):

    FROM centos
    COPY rest-api .
    EXPOSE 3333
    CMD [ "./rest-api"]
     
    A line-by-line explanation is later in this article, but let's just build thing and run it; we can come back to the details.

    Let's Get Some Code

    Fork or clone the github repository at https://github.com/donschenck/path-to-kubernetes.
    Move into the directory src/go/hello-world

    Let's Build And Run

    We need to make sure gorilla/mux (URL router and dispatcher for golang) is installed:
    go get github.com/gorilla/mux
     
    To compile the code into a CentOS-compatible binary, run
    env GOOS=linux GOARCH=amd64 go build -o rest-api
     
    To build the image, run
    podman build -t hello-world-go .
     
    To run the image (again, we'll dive into this later), run
    podman run -p 3333:3333 hello-world-go
     
    Finally, open a second terminal window and run
    curl http://localhost:3333
     
    You should see "Hello World!" as the result.
     
    If you get an error message that "slirp4netns" was not found, you need to install it.

    The Cycle

    So that's the basic cycle:
    1. Create the source code
    2. Create a Dockerfile file
    3. Build the image
    4. Run the image in a container

    About that Dockerfile

    The file “Dockerfile” is used to guide the construction of your image. Here’s a short, step-by-step breakdown: FROM centos This is your base image, the starting point. In this case, it’s the official image from CentOS. In case you’re wondering, it’s 209MB on my Mac.

    I could have chosen almost any Linux distribution. In fact, Alpine Linux weighed in at just over five megabytes. That is one small base image.
     
    COPY rest-api .
     
    Copies the compiled program into the image.
     
    EXPOSE 3333
     
    Exposes the application port, 3333, to the outside world.
     
    CMD [ "./rest-api"]
     
    This is what runs when the image is started (i.e. podman run or docker run).

    Running In A Container

    Running the podman run -p 3333:3333 hello-world-go starts the image in a container. It code uses port 3333, and it is mapped to the local port 3333. Feel free to experiment with this. It will be attached to your command line; that is, it ties up your terminal while it’s running. You can eliminate this by using the --detach option in your command. In that case, the container runs in the background.

    You can see the results of the code by running the curl command or opening your browser to http://localhost:3333.

    Containerize All The Things

    So  you know have all the knowledge and tool necessary to run your Node.js code in a Linux container. Expanding this knowledge to include multiple instances of an application, and/or multiple applications in a cluster, is for the next blog post.

    Last updated: June 10, 2019