JVM microservices - run Docker containers on Windows
Microservices is one of the loudest IT buzzwords, everybody’s anxious to try it.
But what Linux
and MacOS
users get for free is not so easy in Windows
.
This guide shows how to setup Windows
environment for running Docker
containers, as a basis for microservice architecture projects.
- Glossary
-
-
GIT-SCM - Git For Windows project, containing
git
,bash
and otherLinux
tools -
Environment variables -
Windows
environment variables, assumed to be managed via Control Panel
-
In fact, Docker Toolbox
for Windows
comprises everything for running Docker
containers because it is bundled with GIT-SCM project.
Unfortunately the default installation has few drawbacks
-
No option for changing GIT SCM destination
-
Сommand line tools ain’t added to
PATH
environment variable -
Impossible to configure additional
git
parameters unlike in original GIT-SCM installer
To overcome those and to achieve the better environment flexibility, I’ll explain in this guide a longer way, where all required software will be installed from separate bundles.
Following old habit I tend to avoid installation of tools that are planned to be used from command line to |
Setup MSYS2
MSYS2
is a basis project for GIT-SCM and provides advantages over GIT-SCM,
- Advantages over GIT-SCM
-
-
More command line oriented, not limited to
git
usage -
Symbolic link support
-
Built-in package manager
pacman
, ported fromArch Linux
distributive-
Possibility to install arbitrary tools not included into
MSYS2
distributive -
Possibility to upgrade
MSYS2
core from command line
-
-
- Installation steps
-
-
Run installer from https://msys2.github.io/ and follow instructions
-
Use
d:\opt\msys
as a destination folder -
After installation is completed add
d:\opt\msys\usr\bin
toPATH
environment variable
-
- HOME directory location
-
By default
MSYS2
uses own directory for user home, so insteadC:\Users\
your home will be located ind:\opt\msys\home
folder. This behavior can be overridden by adjusting/etc/nsswitch.conf
file.Find and change line
db_home: cygwin desc
todb_home: windows desc
. - Linux Tools Manuals
-
man
command is not installed by default, but you can enable it usingpacman
tool.$ pacman -S man-db
Setup Docker Toolbox
-
Run
Windows
installer from https://www.docker.com/docker-toolbox and follow instructions -
Use
d:\opt\docker
as a destination folder -
Uncheck
Git for Windows
checkbox, we will use Linux tools fromMSYS2
installed before
Setup ConEmu
MSYS2
provides possibility to run bash
but as soon as you run many consoles you start to get lost in those floating windows. ConEmu
comes to the rescue, providing comfortable tabbed interface for bash
shells along with additional functionality improving command line experience and better integration on`Windows`.
-
Run installer from https://conemu.github.io/ and follow instructions, alpha releases can be used
-
Create
ConEmu
task for runningbash
console and run it on program startupFigure 1. ConEmu task for running MSYS2 -
Create new consoles inside single
ConEmu
windowFigure 2. ConEmu single window settings -
Integrate with
Windows
shell, environment variableCHERE_INVOKING
forcesMSYS2
to use current directory as a working directory for newbash
instanceFigure 3. ConEmu shell integration settingsFigure 4. ConEmu shell integration
Verify that everything works
-
Start
ConEmu
program (should start withbash
console running inside new tab) -
Open new console in
ConEmu
withCtrl+X
hotkey, this is just to checkCtrl+X
works -
Go to
/d/opt/docker/
folder and run./start.sh
there -
Execute
docker run hello-world
command -
Check output, it should looks like below, refer to Docker Guide for latest actual information about the output
$ docker run hello-world Hello from Docker. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com For more examples and ideas, visit: https://docs.docker.com/userguide/
docker-machine tool
Docker Toolbox
installs VirtualBox
and creates own VM inside it named default
.
Although VM management can be performed via VirtualBox
UI, there’s useful docker-machine
tool.
It allows to interact with VirtualBox
VM from command line.
Some useful commands are shown below.
-
$ docker-machine ls
- list machines and their statuses -
$ docker-machine stop default
- stop defaultVirtualBox
VM -
$ docker-machine start default
- start defaultVirtualBox
VM -
$ docker-machine help
- for more information
Improve Git experience on Windows
If you plan to use git
then pay attention to steps below, otherwise this section could be skipped.
- Line endings
-
GIT-SCM as well as other sources advice to use
core.autocrlf
equals totrue
while working withgit
onWindows
. Execute command below to set this parameter for allgit
repositories.$ git config --global core.autocrlf true
- Password caching
-
Working with remote repositories via HTTP / HTTPS requires entering user name password. It’s good to use credentials helper that caches passwords, so there no need to type them each time. For
GitHub
it’s easy and explained in this article. But this approach doesn’t fit well withBitBucket
repositories.Git Credential Manager for Windows project works fine with both
GitHub
andBitBucket
, but currently it can be used only withgit
installed via GIT-SCM - track сorresponding issue.The solution is to use Git Credential Manager for Windows predecessor that works fine with any
git
installation. - Branch name in shell promt
-
Add following to your
~/.bashrc
to displayGit
branch name in shell promt.
. /usr/share/git/completion/git-prompt.sh
PS1='\[\033]0;$MSYSTEM:${PWD//[^[:ascii:]]/?}\007\]' # set window title
PS1="$PS1"'\n' # new line
PS1="$PS1"'\[\033[32m\]' # change color to green
PS1="$PS1"'\u@\h ' # user@host<space>
PS1="$PS1"'\[\033[33m\]' # change color to yellow
PS1="$PS1"'\w' # current working directory
if test -z "$WINELOADERNOEXEC" ; then
PS1="$PS1"'$(__git_ps1)' # bash function
fi
PS1="$PS1"'\[\033[0m\]' # change color to normal
PS1="$PS1"$'\n' # new line
PS1="$PS1"'$ ' # prompt: always $
What’s next ?
This is the first post about JVM
based projects based on microservice architecture, mostly related to Windows
specific features.
Second post explains how to create and run sample JVM
project using environment described in this guide.