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
Install Go:
sudo apt install golang-go
Change the working directory:
cd exercises/go-helloworld
Run Go app using the terminal command:
go run main.go
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 replaceYOUR_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:
Remove all containers and images from your local computer.
Pull the image from the Docker Hub repository.
Run the pulled image on your local computer.
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:
Create Dockerfile.
Build a Docker image.
Run newly built image as a container.
Check the Flask application at http://127.0.0.1:5000/
Push the Docker container to Docker Hub.
Remove container from the local machine.
Pull the container from Docker Hub.
Run pulled container.