2. Docker for Application Packaging

In this exercise, you will use a minimal Go application that looks like this:

package main

import (
        "fmt"
        "net/http"
)

func helloWorld(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Sherlock Holmes sat silently...")
}

func main() {
        http.HandleFunc("/", helloWorld)
        http.ListenAndServe(":8080", nil)
}

Your tasks are to containerize this Go application and push it to Docker Hub. But before you get started, we highly recommend watching the Docker introductory video at the top of the page and following through with the How to Build a Containerized Go Application with Docker tutorial.

Preparation

Note

This application listens on port 8080

  1. Download and install Docker Engine

  2. Install Go: sudo apt install golang-go

  3. Change the working directory: cd exercises/go-helloworld

  4. Run Go app using the terminal command: go run main.go

  5. Check your app in a web browser at: http://127.0.0.1:8080/

Exercise 2.1 - Create Dockerfile

Dockerfile requirements:

  • Use an image that is based on the latest stable Go environment.

  • Set the proper working directory.

  • Copy source code into the image.

  • Implement logic to build the application.

  • Make sure that app access on a default port 8080.

  • Add the command to start the container.

Exercise 2.2 - Build a Docker Image

Docker image requirements:

  • Use the go-helloworld name as the image name.

  • Tag the image to YOUR_DOCKERHUB_USERNAME/go-helloworld:v1.0.0, (don’t forget to replace YOUR_DOCKERHUB_USERNAME with your Docker Hub username).

Exercise 2.3 - Push a Docker Container to Docker Hub

Note

To push a Docker container image to Docker Hub, you need to have a Docker Hub account

The application image shoud be available on Docker Hub under the https://hub.docker.com/r/YOUR_DOCKERHUB_USERNAME/go-helloworld, where YOUR_DOCKERHUB_USERNAME is you current user name e.g. https://hub.docker.com/r/osdoc/go-helloworld.

2.4 Pull Image From the Docker Hub

To complete the exercise, do the tasks in the following order:

  1. Remove all containers and images from your local computer.

  2. Pull the image from the Docker Hub repository.

  3. Run the pulled image on your local computer.

  4. Verify go-helloworld application at: http://127.0.0.1:8080/

Exercise 2.5 (Optional) - Containerize Python Application

This is an exercise to consolidate the material covered in the section. Your job is to containerize the Flask application that you created in the first lesson.

Follow all these steps to reach your goal:

  1. Create Dockerfile.

  2. Build a Docker image.

  3. Run newly built image as a container.

  4. Check the Flask application at http://127.0.0.1:5000/

  5. Push the Docker container to Docker Hub.

  6. Remove container from the local machine.

  7. Pull the container from Docker Hub.

  8. Run pulled container.

Additional Resources

  1. Docker Tutorial for Beginners - 3 Hour Video Course

  2. Docker Hub Quickstart

  3. Build a Docker Image

  4. Pushing a Docker Container Image to Doker Hub

  5. How to Build a Containerized Go Application with Docker

  6. Docker Image Pipeline for Go

  7. Build Python Docker Image

  8. How to Serve a Flask App with Amazon Lightsail Containers

  9. Docker Cheet Sheet

  10. How To Install and Use Docker on Ubuntu 20.04

  11. A Beginner-Friendly Introduction to Containers, VMs and Docker