Running your own Docker containers in Minikube for Windows

Mauricio M. Ribeiro
5 min readJan 12, 2017

--

Linux is great, especially when it comes down to work with Docker and Kubernetes. Now, with Minikube, we can run k8s locally and play with it. Awesome … with Linux :)

Nevertheless, some of us may have (or prefer) to work with Windows for whatever reasons…

And yes, working all this amazing stuff in a Windows environment can be really painful. Happily, we have some very good guys who help us with it. Romin Rami has an awesome tutorial to run Minikube in Windows here.

In his tutorial, he explains how you can easily run minikube from a Windows environment, using Virtual Box. He shows how to download the basic nginx image and manage it with Kubernetes locally.

In this page, I am going to “extend it”, showing how we can use minikube to manage locally an app that we create ourselves. The stuff we are going to do here is based on this awesome — and simple — tutorial.

So, let’s go for it. We are going to:

  • create a dummy program in Go, and create a Dockerfile for it
  • build an image from this Dockerfile
  • run a container using this image, and expose it as a service
  • manage and scale the service

Setup environment

For this tutorial, we are going to need to install:

(Note: I suggest putting the executables of minikube and kubectl in a same directory, e.g. C:\minikube)

Docker for Windows uses Hyper-V, and we need it disabled in order to make Virtual Box to work. Take a look here or here in order to do this.

If you have everything, we can go on and create our dummy program.

Our dummy program

Open your favorite code editor — in my case I use VSCode — browse to you %GOPATH% and create a directory to your project in the src folder.

C:\>cd %GOPATH%
C:\<yourGoPath>>cd src
C:\<yourGoPath>\src> mkdir <your_project>

Then, create a file main.go in this new directory and paste the content below into it:

package mainimport (
"log"
"net/http"
"github.com/gorilla/mux"
)
func runProgram(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
response := []byte("Hello Universe! (because World is too small...)")
w.Write(response)
return
}
//NewRouter api router
func newRouter() *mux.Router {
apiRouter := mux.NewRouter().StrictSlash(true) //mainRouter.PathPrefix("/api").Subrouter().StrictSlash(true)
//Create Routes
apiRouter.HandleFunc("/", runProgram).Methods("GET")
return apiRouter
}
func main() { // launch server in port 8500
log.Fatal(http.ListenAndServe(":8500", newRouter()))
}

If you want to test it, browse to the project directory and type go run main.go:

C:\<yourGoPath>\src\<your_project>> go run main.go

Then, open a web browser and go to http://localhost:8500/

Now, create a Dockerfile and paste the contents below:

#Note: golang:onbuild is a very straightforward way for you to build your own GO app image
FROM golang:onbuild
EXPOSE 8500

Now we are ready to dockerize and expose the application!

Building the image and running the container

Time to put our minikube to work. It is possible that you run into issues while running the commands below. I suggest again the Romin Rani’s tuto and check the troubleshooting in the end of it.

We are going to use minikube and kubectl here. First, we start minikube. This will create the VM to host the images and containers, and start it. (I use Powershell but you do as you wish as far as you do it as an Administrator).

PS C:\> minikube start

Here is the screenshot when I use the param — v=3, which displays log info.

As you can see, we can start using Kubernetes and Docker inside it. To do it, run the following command:

PS C:\minikube> minikube docker-env

This will give you the following output:

As you can see, in order to configure our shell, we have to run:

PS C:\minikube> minikube docker-env | Invoke-Expression

After this, run docker images. You will see that your Docker environment is inside Kubernetes:

Let’s build our image then! Go to your Go project directory, and run the following command:

C:\<yourGoPath>\src\<your_project>> docker build -t dummy:v0 .

If you run docker images, you will see the dummy image there.

Now that we have our image, let’s use kubectl to run the container and expose it as a service.

Running the app from a container

To start the container, run the following command:

PS C:\> kubectl run hello-universe --image=dummy:v0 --port=8500
deployment "hello-universe" created

You can see that the Pod was well created:

PS C:\> kubectl get pods

Now, let’s expose it to external traffic:

PS C:\> kubectl expose deployment hello-universe --type="LoadBalancer"
service "hello-universe" exposed

We can see that the service is well created:

PS C:\> kubectl get services
Don’t worry with the “pending” :)

Now we get the generated URL

PS C:\> minikube service hello-universe --url
http://192.168.99.100:32736

Finally, we can open it on the browser:

Now, we can play with management and scaling.

Final chapter: Kubernetes dashboard and scaling

To open the dashboard

PS C:\> minikube dashboard
Opening kubernetes dashboard in default browser...

This command will open the dashboard on the browser

Just for fun, let’s scale our dummy app in 3 replica sets:

PS C:\> kubectl scale deployment hello-universe --replicas=3
deployment "hello-universe" scaled

This gives as a result:

Now, we can see three pods with our code running.

Conclusion

Before reaching Heaven, you have to go through Hell.

Setting Kubernetes locally can be painful, really harmful, but it is definitely worth it!

And minikube came to make it even funnier allowing us to manage and scale locally our containers! Ops stuff will be far less painful than before (even in Windows)!

--

--