DEV Community

Cover image for 100DaysOfCode — Day 06
Moataz Burhan
Moataz Burhan

Posted on

100DaysOfCode — Day 06

This is a simple tutorial demonstrates creating Golang Docker image.

Prerequisites:

Go App

Create a simple Go program hello-http.go:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "hello world\n")
}

func main() {
    http.HandleFunc("/", hello)
    log.Println("Starting server on 0.0.0.0:8080")
    http.ListenAndServe(":8080", nil)
}
Enter fullscreen mode Exit fullscreen mode

Docker file

In the same directory, create Docker manifest file (Dockerfile):

# initializes a new build stage and sets the base image
FROM golang:1.15.1-alpine

# copy hello-http.go and add it to the filesystem of the image
ADD hello-http.go .

# compile the program
RUN go build hello-http.go

# provide the default command for the container
CMD ["/go/hello-http"]
Enter fullscreen mode Exit fullscreen mode

docker build

Build an image from your Dockerfile

$ docker build . -t hello:v1.0.0
Enter fullscreen mode Exit fullscreen mode
  • -t Name and tag the image in the 'name:tag' format

List docker images:

$ docker images
Enter fullscreen mode Exit fullscreen mode
REPOSITORY                         TAG       IMAGE ID       CREATED          SIZE
hello                              v1.0.0    47f0babcdfe2   44 minutes ago   306MB
Enter fullscreen mode Exit fullscreen mode

run the command:

$ docker run -dit --rm -p 8080:8080 --name hello hello:v1.0.0
Enter fullscreen mode Exit fullscreen mode
  • docker run Run a command in a new container
  • -d Run container in background and print container ID
  • -i interactive
  • -t Allocate a pseudo-TTY
  • --rm Automatically remove the container when it exits
  • -p 8080:8080 Publish a container's port(s) to the host
  • hello:v1.0.0 Docker image to run
  • --name hello Assign a name to the container

Test your container

$ docker ps
Enter fullscreen mode Exit fullscreen mode
  • docker ps List containers

You see a list like this shows a container running hello image.

CONTAINER ID   IMAGE          COMMAND            CREATED          STATUS          PORTS                    NAMES
7c3c8774134d   hello:v1.0.0   "/go/hello-http"   54 minutes ago   Up 54 minutes   0.0.0.0:8080->8080/tcp   hello
Enter fullscreen mode Exit fullscreen mode

Now open your browser and go to http://localhost:8080/:
Alt Text

End the container

Now run the following command to end the container:

$ docker stop hello
Enter fullscreen mode Exit fullscreen mode

Now list the containers

$ docker ps
Enter fullscreen mode Exit fullscreen mode
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
Enter fullscreen mode Exit fullscreen mode

Note that docker container has stopped and automatically removed:

$ docker ps -a
Enter fullscreen mode Exit fullscreen mode

You will see this:

CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS                     PORTS     NAMES
Enter fullscreen mode Exit fullscreen mode
End of day 6 of 100DaysOfCode.

Top comments (0)