minikube as drop in replacement for Docker Desktop (Mac and Windows)
For a very long time, development time dependencies like postgres (database), kafka (message broker) was run using docker desktop (Docker for Mac / Docker for Windows). However due to recent changes to licensing, docker desktop is not free for all organisations. As a developer, if you are in an organisation who can't provide Docker Desktop, then minikube can be used as a drop in replacement for Docker Engine, so that your dependency scripts using docker cli can continue to work.
The following contents explains the bare minimal commands needed to run minikube which will work if you are running dependencies using docker cli via:
- Shell scripts
- Maven docker plugin
- Gradle docker plugin
- TestContainers
- IDE (Intellij / Eclipse / VS Code) Plugin
Install minikube:
https://minikube.sigs.k8s.io/docs/start/
Start minikube:
minikube start
Stop minikube:
minikube stop
Delete minikube cluster:
minikube delete
Delete all minikube clusters:
minikube delete --all
See minikube dashboard:
minikube dashboard
Export minikube's docker config:
eval $(minikube -p minikube docker-env)
View minikube's IP:
minikube ip
Mounting host directories and accessing it within docker:
Mount host directories using the following commands in separate terminals:
Note: minikube should be fully started and running before executing these commands. The following command attaches the host directory to an active running minikube cluster.
minikube mount $HOME/professional/learning/github-actions-aws-terraform:/github-actions-aws-terraform
minikube mount $HOME/professional/learning/aws_cli_docker:/aws_cli_docker
Run a docker container with the mounted directories:
docker run -it --rm -v /github-actions-aws-terraform:/github-actions-aws-terraform -v /aws_cli_docker:/aws_cli_docker --entrypoint=/bin/bash ubuntu:latest
Running postgres database in minikube using docker cli:
Setup the terminal with necessary environment variables:
eval $(minikube -p minikube docker-env)
export MINIKUBE_IP=$(minikube ip)
Run postgres database:
docker pull postgres:latest
docker run -it --rm --name postgres_latest_local -e POSTGRES_DB=mypostgresdb -e POSTGRES_USER=mypostgresuser -e POSTGRES_PASSWORD=mypostgrespassword -p 5432:5432 postgres:latest
Troubleshooting:
If you get the following error
error getting credentials - err: docker-credential-desktop resolves to executable in current directory (./docker-credential-desktop), out: ``
Then remove following line from ~/.docker/config.json
"credsStore": "desktop"
Connecting to Postgres database from plain Java application:
NOTE: The connection is using an environment variable MINIKUBE_IP instead of localhost for connecting to docker
Jdbc3PoolingDataSource source = new Jdbc3PoolingDataSource();
source.setDataSourceName("A Data Source");
source.setServerName(System.getenv("MINIKUBE_IP"));
source.setPortNumber(5432);
source.setDatabaseName("mypostgresdb");
source.setUser("mypostgresuser");
source.setPassword("mypostgrespassword");
source.setMaxConnections(10);
Spring Boot application property configuration:
spring.datasource.url=jdbc:postgresql://${MINIKUBE_IP}:5432/mypostgresdb
spring.datasource.username=mypostgresuser
spring.datasource.password=mypostgrespassword
spring.datasource.driver.class.name=org.postgresql.Driver
Running build / tests in command line:
NOTE: It will be easier to create a build.sh script with the commands in series so as to avoid repeating the minikube command on every terminal
eval $(minikube -p minikube docker-env)
export MINIKUBE_IP=$(minikube ip)
mvn clean install
gradle clean build
Running build / tests in IntelliJ IDE:
To run the build / tests in IntelliJ IDE, the environment variables for docker and minikube IPs should be available in the IDE. In order to do this, the easiest way is to set these environment variables in a terminal and launch the IDE through command line as shown below:
eval $(minikube -p minikube docker-env)
export MINIKUBE_IP=$(minikube ip)
In Mac terminal use open command
open "/Applications/IntelliJ IDEA CE.app"
In Linux terminal use nohup command
nohup /opt/idea/bin/idea.sh > /dev/null 2>&1 &
More on running IntelliJ IDEA from command line is detailed in official documentation:
https://www.jetbrains.com/help/idea/working-with-the-ide-features-from-command-line.html
Comments
Post a Comment