Mixing Windows and Linux containers with Docker Compose
Premier Developer Consultant Randy Patterson explores how to mix Windows and Linux containers with Docker Compose.
Running Linux containers on a Windows host has been available for awhile now. However, getting Windows and Linux containers to communicate without Docker Compose results in using the containers’ IP Addresses. This is not a good solution since container IP Addresses change often. A better solution is to take advantage of the Docker Compose networking and use the service name instead of the IP address. A Windows host has no problem running Linux containers but Docker Compose does not automatically support multi-platform hosts.
The platform configuration option was added to Docker Compose files starting with version 2.4 and then removed beginning with version 3.x. It is a know bug and the expectation is that the platform option will return soon. When this article was written, it has not yet appeared in version 3.x so we will continue to use file format version 2.4 for our Docker Compose yaml files.
Setting up your environment
Running Linux containers on Windows and platform support in Docker Compose requires Windows 1803 or greater. I am running Windows 10 1809.
In addition, you must be running the latest version of Docker for Windows and have the experimental features enabled.
If you are running different versions of Windows or Docker your mileage may vary.
Next, grab the source code located in the GitHub repo here containing two solutions
- Webtier.sln – Asp.Net MVC application running .NET Framework 4.6.x and a Dockerfile for creating a Windows image hosting IIS.
- ApiTier.sln – Asp.Net Core 2.1 application and a Dockerfile for creating a Linux image to host the application
The application is a simple web page that displays the hostname of the Web Tier then calls the API Tier to get and display the API hostname. When running in containers, the container Id is displayed. Your Id’s will be different
The docker-compose.yml file, located in the root of the solution, adds the platform: windows parameter to the webtier service and the platform: linux parameter to the apitier service.
version: '2.4' services: webtier: image: webtier:win platform: windows ports: - 80 build: context: .\WebTier dockerfile: Dockerfile depends_on: - apitier environment: ApiHost: "apitier" apitier: image: apitier:linux platform: linux expose: - 80 build: context: .\ApiTier dockerfile: Dockerfile
Next, bring up the multi-container, multi-platform application using docker-compose. Start a powershell prompt in the solutions root folder, the same folder that contains the docker-compose.yml file. Type the command:
docker-compose up -d
Docker Compose will create the images if they do not already exist, then create the virtual network to host the containers and finally start the containers in the correct order.
Finally, retrieve the host port of the webtier container
Use your browser to navigate to http://localhost:38736 (your port number will be different)
If the Windows Web Tier was able to communicate with the Linux API Tier, you will see the id for each of the containers (your id’s will be different) on the web page.
Using the platform option in your configuration files allows Docker Compose to mix Windows and Linux containers when hosted on a multi-platform host like Windows. Keep in mind that this is an experimental feature and is not yet ready for production environments.
a very nice article.
I am using LCOW for several months and it works for integration tests ina mixed environment very well.But now I have a problem using microsoft/dotnet:2.2-aspnetcore-runtime(or …-alpine) for the linux images.
Building the docker image on linux with this base image and running it in anLCOW environment works in that way that the container starts and itsfunctions are available.
But if I want to stop the container it takes several minutes to stop thecontainer.
Do you have any idea why it takes so long?
Thanks for the comment Christian. LCOW containers taking a long time to stop is a known issue that hasn’t been fixed yet. I don’t always experience it with ‘docker stop’ but I do with ‘docker-compose down’. Just be patient for now and don’t kill the process. ‘docker-compose down’ will also remove the network it created and if that gets interupted it can be difficult to fix.
Hi Randy, very good article!
Unfortunatly I’m not able to start win & lin containers on my windows 10 docker… 🙁
I try to start a jenkins master and a slave on linux and a slave on windows.
I’ve got quite the same versions (Win10 1803, Docker desktop 188.8.131.52, engine 18.09.2, compose 1.23.2)
I tried both in Linux Container mode and Windows Container mode with the same issue :
>docker-compose up (Switch Windows container)Creating network “jenkins_master_default” with the default driverCreating volume “jenkins_master_jenkins-home” with default driverCreating volume “jenkins_master_jenkins-agent” with default driverPulling jenkins_master (jenkins/jenkins:lts)…ERROR: unsupported os linux
>docker-compose up (Switch Linux Container)Pulling jenkins_slave_win (jonathank/jenkins-jnlp-slave-windows:)…latest: Pulling from jonathank/jenkins-jnlp-slave-windowsERROR: image operating system “windows” cannot be used on this platform
Please find below my docker-compose.yml:
volumes:jenkins-home:services:jenkins_master:container_name: jenkins_masterplatform: linuximage: jenkins/jenkins:ltsrestart: unless-stoppedports:- “8080:8080/tcp”- “50000:50000/tcp”privileged: truevolumes:- jenkins-home:/var/jenkins_homejenkins_slave_lin:container_name: jenkins_slave_linplatform: linuximage: jenkins/jnlp-slaveenvironment:- JENKINS_URL=http://jenkins_master:8080- JENKINS_SECRET=d9daec1f3e00bdbf47ff5326a9b82a00998c729fc850a34624c4f9e5df4bd0f2 – JENKINS_AGENT_NAME=JENKINS_SLAVE_LIN- JENKINS_AGENT_WORKDIR=/home/jenkins/agentrestart: unless-stoppedprivileged: truelinks:- jenkins_masterjenkins_slave_win:container_name: jenkins_slave_winplatform: windowsimage: jonathank/jenkins-jnlp-slave-windowsenvironment:- JENKINS_URL=http://jenkins_master:8080- JENKINS_SECRET=4146d2bad4a2e5317618dd2a2e2e2285b477c2d3ef0bcb8cd9693a36f6c4e1e9 – JENKINS_AGENT_NAME=JENKINS_SLAVE_WINrestart: unless-stoppedprivileged: truelinks:- jenkins_master
Can you help me understand my issue ?