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.