client container to test service in container

Viewed 69

I am new to Docker and Kubernetes. I am going through some documentation and running the setup locally. I have some questions regarding that.

I created an image from a go file that prints "hello world". Here is the sample go file

package main

import (
    "fmt"
    "net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
    response := "Hello World!"
    fmt.Fprintln(w, response)
    fmt.Println("Processing hello request.")
}

func listenAndServe(port string) {
    fmt.Printf("Listening on port %s\n", port)
    err := http.ListenAndServe(":"+port, nil)
    if err != nil {
        panic("ListenAndServe: " + err.Error())
    }
}

func main() {
    http.HandleFunc("/", helloHandler)
    port := "8080"
    go listenAndServe(port)
    select {}
}

Here is the Dockerfile

FROM golang:1.13
WORKDIR /go/src/hello
COPY ./hello.go /go/src/hello
CMD ["go","run","hello.go"]

This container is up and running. The container's IP address is stored in IP variable.

Now, in the documentation it says that we need a client container to test this service.

docker container run --rm busybox wget -qO- $IP:8080 && echo

Status: Downloaded newer image for busybox:latest
Hello World!

I am wondering why it needs this client container and when I the wget directly from my local machine, this does not work.

Any inputs. Thanks in advance for the help.

1 Answers

By default, the containers are deployed inside the docker-bridge network. There is no implicit route in-between your host machine and that VLAN in which the containers are connected: this is the reason the documentation you've read suggested to run a client container to check if the web-app is indeed working - another container can reach the go container, because it is connected in the same subnet.

However, if you have for example a container that is listening on port 8080, you can use the -p <src>:<dst> option when you launch the container, in order to create a forwarding rule between your host-machine:<src> to the container:<dst>.

If you'd use this command to launch the go container, you'd be able to access the web site from your host machine at localhost:9999

docker run -d -p 9999:8080 my-go-image
Related