Learn how to use Minikube to manage your Docker containers in order to test integration scenarios and avoid problems when deploying to a remote environment.

Whether or not you are planning to deploy the software you're developing to containers, using Docker -- along with the services it depends on -- to run them in your local developer machine can be extremely helpful to test integration scenarios and avoid problems when it's deployed to a remote environment.

This is something easy to do using Docker Desktop, but what if you just need the Docker Engine and the Docker CLI, and maybe a plug in to your favorite IDE? Can we go on without Docker Desktop?

In a previous article, you learned how to install and configure Docker Engine in a Vagrant VM. In this article, you'll learn how to use Docker through a Minikube cluster.

I will assume you are using a Mac operating system with ZSH as the default shell, but these steps can be easily modified to work on other operating systems.

Setting Up Your Machine

The first thing you have to do is to make sure Docker Desktop and Docker CLI are not installed on your Mac, since later you will have to install the Docker CLI alone. If you need to uninstall it, follow the steps here.

You will also need Homebrew to proceed with the steps below. You can learn how to have it installed here, or simply run the command below:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Now you need Hyperkit, an open-source hypervisor for macOS hypervisor, optimized for lightweight virtual machines and container deployment, being a viable choice for local Kubernetes clusters on Mac.

brew install hyperkit

Install Docker, Kubectl, and Minikube with the commands below.

brew install kubectl

brew install minikube

brew install docker

brew install docker-compose

This last command will install only Docker CLI, not the Docker Engine or Docker Desktop, so if you try to use Docker now, you will receive the following error message:

docker run hello-world

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.

See 'docker run --help'.
Kubernetes Cluster

Now that you have everything that you need on your Mac, it's time to start your Kubernetes cluster. Here we specify that we want to use a Hyperkit VM and Docker as our container runtime.

minikube start --driver=hyperkit --container-runtime=docker

At this point, we have a Kubernetes cluster and a Docker daemon running. Set the environment variables with the command below so that you can use the daemon with docker-cli.

eval $(minikube docker-env)

These environment variables are lost when you restart your terminal session. To avoid this, add the command above to your .zshrc file; in so doing, the variables will be set every time you open a terminal.

Now you can run docker info to confirm that the Docker daemon is available. From now on you can manage your containers through docker-cli. 

To access a container port, you can't use localhost since Docker is running on the cluster VM. Run the command below to get the Minikube IP address and use it to access your containers.

minikube ip

Now you should be ready to use Docker CLI in the host:

docker run hello-world

The last step is to test Docker Compose. Go to some project that has a properly configured docker-compose.yml file and spin that up:

docker-compose up

Docker CLI is ready, so now it's time to configure the IDEs.

Configuring IntelliJ to Connect to Docker

In the Docker settings, add a Docker configuration using Minikube. Select Minikube and press "OK," as shown in the image below:

 

If you use VS Code, it is even simpler. With the Docker CLI properly configured, Visual Studio should connect to Docker without problems and the Docker Explorer should show images and containers. You can run Docker commands as usual.

That's it! I hope that these configuration steps work well for you and that you can now use Minikube to manage your Docker containers.

 

References

Author

Matheus Correia

Matheus Correia is a Software Engineer at Avenue Code. He loves to learn new technologies and to work on challenging projects that impact people. In his free time, Matheus likes to read, watch movies and spend time with the people he loves.


How Heuristics Can Improve Your Tests

READ MORE

How to Use WireMock for Integration Testing

READ MORE

The Best Way to Use Request Validation in Laravel REST API

READ MORE

How to Write Unit Tests Using PHPUnit and WP_Mock

READ MORE