Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A dockerized C++ windows console application exited with code 3221225781

Tags:

c++

docker

I Have written a very simple c++ hello world program

#pragma once
#include <iostream>
#include <fstream>

int main()
{
   std::cout << "Hello Docker world!\n";
   return 0;
}

This is build as a release x64 windows console application and thus produces an exe

than I dockerize this program using the following dockerfile

FROM microsoft/windowsservercore

ADD ./DockerHello.exe /DockerHello.exe

# Run exe when the container launches
CMD C:\DockerHello.exe

However when i use docker run it will show nothing and when i use docker ps -a I see that it has exited with code 3221225781

From some online searching I understand that this apparently means that I am missing some dll or so, but I have no idea how to find out which?

Can anybody help me? Or tell me how I can get a simple c++ console application working in docker?

like image 227
user180146 Avatar asked Sep 14 '25 11:09

user180146


1 Answers

As confirmed in the comments, the issue stems from the runtime library not being present on the Docker image for code compiled from MSVC.

To solve this, you may either:

  1. Statically link to the runtime, using the /MT flag.
  2. Install the correct runtime on the docker image.

Static Linking

The /MT flag (or variants) must be passed to msbuild, which through Visual Studio can be done as follows (from Microsoft's documentation):

Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages. Expand the C/C++ folder. Select the Code Generation property page. Modify the Runtime Library property.

Install Runtime

To install the correct runtime, you must install the redistributable while building of the docker image.

ADD $url /vc_redist.exe
RUN C:\vc_redist.exe /quiet /install

For Visual Studio, these are the correct URL (as of today's date, feel free to edit to update):

Where the $url is the path to the correct Visual Studio Redistributable (links provided below):

  • Visual Studio 2017, x86, x86-64
  • Visual Studio 2015, x86, x86-64
  • Visual Studio 2013, x86, x86-64

Docker Image Selection

As the OP notes in the comments, the microsoft/nanoserver image is sufficient when using static runtime linking, however, if using shared runtime linking, you should use the microsoft/windowsservercore image, otherwise, the installation of the redistributable fails.

like image 130
Alexander Huszagh Avatar answered Sep 16 '25 00:09

Alexander Huszagh