Fly with You

多谷黑芝麻 & 蜂蜜柚子茶

0%

Run a cpp Example on Win10 Docker with VS Build Tools

Sometimes when you need to use a cpp runtime environments on Windows system for your special applications, you can just install a Visuo Studio xxx to do it. But if you want to dilivery it to cloud environment, maybe you need make it into a Docker just like me.

Install Docker into Win10

  1. Install Docker CE in one computer with Windows 10 system, which its Virtualization must to be turn on firstly in your BIOS.

  2. Note that, you need to log out once to complete installation when the installation succeeded.

  3. After it, when you start Docker, it will check your computer setting of Hyper-V.

    Windows Feature
    Hyper-v must be turn on status when you run Docker, which means your Virtual Box can not be used in the meantime. maybe there will have one computer restart here, don’t panic, this is the last restart if you only use Docker instead of virtual machines.

  4. After Docker started, you can find a penguin log in your taskbar. here has some tips to help you test Docker or improve Docker.

    1. Switch Registry:

      You can switch your registry to China Official Registry by edit the Daemon which by right click your Docker to choose Setting. Or you can change registry to a private registry like your aliyun’s speeder address.
      Switch Regostry

    2. Switch to Windows Container Mode:

      In order to use windows images in Docker, you must change mode from Linux Container to Windows Container. after that, you can check it through docker version command.

      switch

    3. First example of windows container: execute docker version in your cmd.exe, then the client and server information for Docker will be shown. After that, you can execute docker run microsoft/sample-dotnet in your cmd.exe or Windows PowerShell.exe (more popular and more convenient) . if there has a logo output, your installation would be correct.

Load a Windows Image with VS Build Tools

  1. Basic Commands:

    After your Docker installation succeeded, you can always use it in your PowerShell through some usual commands to get some basic status like the bellowed.

    1
    2
    3
    4
    5
    6
    7
    8
    #list running containers
    docker ps
    #list all containers(include running/exited/created)
    docker ps -a
    #list images
    docker images
    #check basic info of your Docker
    docker info
  2. Load Images:

    So, if you want to run a container with cpp files in it, you need to have a windows image with VS build tools environment first. I already packed a ready image, this image is built by Dockerfile, which is from a source image microsoft/windowsservercore with Visual Studio Build Tools in it. You just need to load my image into a new folder on your system through docker load -i \\cd-srv04\pxa\Projects\simulation\docker-images\winservercore-vstools.tar.

    PS: if you want to what’s the difference of windowsservercore and nanoserver, please visit here.

    if you want to know how to build your own windows image, please visit here.

    and if you want to know the how to build and save the image we are using, and the please see here.

  3. Check Images:

    Use docker images to check if this image already in your system when it load finished.

    Check Images

Start a Container with a Interactive Cmdlet

  1. After the image load successfully, you can run a container with this image, or you can build other images using this image.

  2. Start a Container:

    Use docker run -it -v $host-code-addr:$container-code-addr win-vs to start a container with a interactive cmdlet, just like the below picture. Then you can do everything you like in this container.

    Start Container

  3. About the above command, just give you some tips for it:

    1
    2
    3
    4
    5
    6
    7
    $host-code-addr = c:/xxx/code   #code address in host computer, means container outer
    $container-code-addr = c:/code #code address in container inner

    docker run -it -v $host-code-addr:$container-code-addr win-vs
    # -i,--Interactive: keep STDIN open
    # -t,--tty : allocate a pseudo-TTY
    # -v,--volume list: Bind mount a volume
  1. Compile and Run a Hello World:

    1
    2
    3
    4
    5
    cd code
    #compile your files
    cl .\hello.cpp /EHsc
    #run your application
    .\hello

    Run Hello-World

  2. Move your whole application of vc++ code to your shared volume on your host computer, and make it in your container. just have a try !

  3. After test or debug on this pseudo cmdlet, you can use exit or ctl+P+Q to quit this window. Then you can use docker start/restart -i $containerID to go back this container’s cmdlet.

QA and Issues

Have a fun and if you have any issues or problem, please don’t hesitate to contact me with kubertqiu@hotmail.com.