March 21st, 2019

Mixing Windows and Linux containers with Docker Compose

Developer Support
Cloud Solution Architects

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.

C:\Users\ranpat\AppData\Local\Temp\SNAGHTML9efec91.PNG

If you are running different versions of Windows or Docker your mileage may vary.

Source Code

Next, grab the source code located in the GitHub repo here containing two solutions

  1. Webtier.sln – Asp.Net MVC application running .NET Framework 4.6.x and a Dockerfile for creating a Windows image hosting IIS.
  2. 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

Docker-Compose

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.

C:\Users\ranpat\AppData\Local\Temp\SNAGHTML9a0e9f1.PNG

Finally, retrieve the host port of the webtier container

docker-compose ps

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.

Author

Developer Support
Cloud Solution Architects

Microsoft Developer Support helps software developers rapidly build and deploy quality applications for Microsoft platforms.

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • Nicolas SONNIER

    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 2.0.0.3, 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...

    Read more
  • Christian Panten

    Hi,
    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...

    Read more
    • Randy PattersonMicrosoft employee

      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.  

      Read more