Using C++ with Docker Engine

Using C++ with Docker Engine

Posted by Deepak Vohra

  • 0.25
    0.50
    0.75
    1.00
    1.25
    1.50
    1.75
    2.00
    2.25
    2.50
    2.75
    3.00
    3.25
    3.50
    3.75
    4.00
    4.25
    4.50
    4.75
    5.00

Although some other alternatives are available, such as the rkt container engine, Docker Engine has become the de facto containerization platform in the past 2-3 years. In "Using Java with Docker Engine," we discussed creating a Java application with Docker Engine. Docker Engine makes better use of the operating system kernel in comparison to a virtualization platform such as VirtualBox or VMWare because a single Docker container does not make use of a whole OS kernel, whereas a single virtual machine does. Each Docker container includes its own filesystem and networking, which makes it an isolated process on the Docker Engine. A single Docker Engine with multiple Docker containers running in isolation makes it feasible to run different applications and even have some containers make use of other containers. One of the main benefits of Docker Engine is the ease of installation and configuration for software. In this tutorial, we shall discuss using C++ on Docker Engine. This tutorial has the following sections:

  • Setting the Environment
  • Creating a C++ Application
  • Creating and Running a Docker Image with the g++ Compiler
  • Creating and Running a Docker Image with the gcc Compiler
  • Compiling and Running C++ Applications Separately
  • Removing Docker Containers and Images

Setting the Environment

Docker is pre-installed on some OSes, such as CoreOS, and needs to be installed if some other OS—such as Ubuntu, Amazon Linux, or Redhat Linux—is used. We shall be using CoreOS. We shall use the CoreOS Linux (Stable) on EC2, which may be accessed at https://aws.amazon.com/marketplace/pp/B01H62FDJM?qid=1478625912213&sr=0-1&ref_=srh_res_product_title. Click Continue. Select 1-Click Launch and the default Version, which is the latest available. Select a Region and a EC2 Instance Type.

Figure 1: Launching

Select the "default" security group, which provides access from all Source IPs. Select a Key Pair, which may have been created previously.

Figure 2: Selecting a security group

Click Launch with 1-click.

Figure 3: Launching with 1-click

A single instance of CoreOS Linux gets started on EC2. Obtain the Public DNS or the Public IP address for the CoreOS instance from the Console.

Figure 4: Obtaining the public information

Using the Key Pair and the Public IP (or Public DNS) SSH, log in into the CoreOS instance.

  1. ssh -i "coreos.pem" core@54.197.150.238

The CoreOS instance gets logged into and the command prompt gets displayed.

Figure 5: Displaying the command prompts

Creating a C++ Application

We shall use a Hello World C++ application to demonstrate the use of C++ on Docker, and the application HelloWorld.cpp is listed:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout << "Hello world" << endl;
  6. cout << "From a C++ Program" << endl;
  7. return 0;
  8. }

Create a file called HelloWorld.cpp in a vi editor and copy the listing to the file.

Figure 6: Creating the HelloWorld file

Two options are available to run the C++ application:

  • Create a Docker image and subsequently run a Docker container.
  • First, compile the Docker application and subsequently run the application.

Creating and Running a Docker Image with the g++ Compiler

We shall be using the Docker image "gcc" available on the Docker Hub. The "gcc" Docker image is the GNU Compiler Collection with support for several languages, including C and C++. Two of the main commands that could be used for C/C++ application are gcc and g++. The differences between the two are discussed in the following table.

gcc g++
GNU C Compiler GNU C++ Compiler
Compiles *.c and *.cpp files as C and C++, repectively Compiles *.c and *.cpp, but they are treated as C++ files
Does not link in the C++ libraries Automatically links in the C++ libraries
Should be used with C applications Should be used with C++ applications

Next, we shall use the g++ command to run a C++ application. Create a Dockerfile (in a vi editor) in the same directory as the HelloWorld.cpp file. A Dockerfile contains instructions to build a Docker image that could be used to run a Docker container. Copy the following listing to the Dockerfile.

  1. FROM gcc:4.9
  2. COPY . /HelloWorld
  3. WORKDIR /HelloWorld
  4. RUN g++ --o HelloWorld HelloWorld.cpp
  5. CMD ["./HelloWorld"]

The Dockerfile instructions are as follows.

Dockerfile Instruction Description
FROM gcc:4.9 The Docker image to use as the base image is gcc with tag 4.9.
COPY . /HelloWorld Copy the files in the current directory to the /HelloWorld directory.
WORKDIR /HelloWorld Set the /HelloWorld directory as the working directory.
RUN g++ --o HelloWorld HelloWorld.cpp Run the gcc command g++ with output as "HelloWorld" and input as "HelloWorld.cpp". The command generates a runnable application called "HelloWorld"
CMD ["./HelloWorld"] Run the compiled, runnable application ./HelloWorld

The Dockerfile is shown in the vi editor.

Figure 7: The Dockerfile

The root directory should list two files: Dockerfile and HelloWorld.cpp.

Figure 8: The two files in the root directory

Before creating a Docker image, create the directory /HelloWorld and set its permissions to global (777).

  1. sudo mkdir /HelloWorld
  2. chmod 777 /HelloWorld

Run the docker build command to create a Docker image called helloworld:v1 from the Dockerfile.

  1. docker build -t helloworld:v1 .

The Docker image gets created.

Figure 9: The newly created Docker image

Subsequently, list the Docker images.

  1. docker images

The helloworld image tag v1 gets listed.

Figure 10: The new helloworld image tag

Having created the Docker image, run the a Docker container with the docker run command. The Docker container may optionally be named, "HelloWorld" for example, with the --name option. If the --name option is not used, a random name is used for the Docker container. The --rm option is called the "Clean up" option and removes the Docker container and the filesystem & volumes associated with the container after it has run. Run the following docker run command for the Docker image helloworld:v1.

  1. docker run -it --rm --name HelloWorld helloworld:v1

The C++ application in the Docker image runs to produce an output.

Figure 11: The Docker image producing output

Creating and Running a Docker Image with the gcc Compiler

It was mentioned previously that the g++ command is used for C++ applications and the gcc command is used for C applications. The main difference between the two is that gcc does not link the standard C++ libraries. But, the standard C++ libraries could be linked explicitly when using the gcc commnd; this is what we shall discuss in this section. Before creating a Docker image using the gcc command, remove the Docker image helloworld:v1 because we will create the same name Docker image with gcc.

  1. docker rmi helloworld:v1

The standard C++ libraries could be linked when using the gcc command with the --lstdc++ option. Modify the Dockerfile to replace the g++ command with the gcc command as in following listing.

  1. FROM gcc:4.9
  2. COPY . /HelloWorld
  3. WORKDIR /HelloWorld
  4. RUN gcc --o HelloWorld HelloWorld.cpp --lstdc++
  5. CMD ["./HelloWorld"]

The modified Dockerfile is shown in the vi editor.

Figure 12: The modified Dockerfile

The same two files, Dockerfile and HelloWorld.cpp, should get listed.

Figure 13: Listing the same two files

Run the same docker build command to create the Docker image helloworld:v1.

  1. docker build -t helloworld:v1

A Docker image gets generated, but contains a different command, the gcc command instead of the g++ command to run the C++ application. List the Docker images, and the helloworld:v1 Docker image should get listed in addition to the gcc Docker image.

Figure 14: Listing the Docker images

Next, run the same docker run command to run a Docker container for the Docker image helloworld:v1.

  1. docker run --it --rm --name HelloWorld helloworld:v1

The same output get generated.

Figure 15: Generating the same output

The Docker containers get removed when using the --rm option. List the running and exited Docker containers with the following commands, respectively:

  1. docker ps
  2. docker ps -a

Because the Docker containers have been removed no Docker container should get listed running or exited.

Figure 16: No Docker container is listed

In the previous two sections, we compiled the Docker application into a Docker image with docker build and subsequently ran a Docker container with docker run. In the next section, we shall not create a Docker image but instead only compile the HelloWorld.cpp file into a runnable application with docker run with the g++ command being invoked as a command arg to the docker run command. Subsequently, we shall run the application binaries HelloWorld, separately.

Compiling and Running C++ Applications Separately

To compile only the HelloWorld.cpp application into HelloWorld runnable application, run the following command that invokes the g++ GNU C++ compiler.

  1. docker run --rm --v "$PWD":/HelloWorld
  2. --w /HelloWorld gcc:4.9 g++
  3. --o HelloWorld HelloWorld.cpp

The --rm option removes the container after it has run, but does not remove the application binaries generated. The --v option adds the current directory as a volume and the --w option sets the working directory to the volume.

Figure 17: Removing the container

Subsequently, listing the files lists the HelloWorld application generated.

Figure 18: Listing the files to see HelloWorld

Next, run the HelloWorld application.

  1. ./HelloWorld

The C++ application output gets generated.

Figure 19: Generating the C++ output

We used the g++ compiler with the docker run command to generate the application binaries, but the gcc command with the --lstdc++ option may be used just as well.

  1. docker run --rm --v /HelloWorld
  2. --w /HelloWorld gcc:4.9 gcc
  3. --o HelloWorld HelloWorld.cpp
  4. --lstdc++

Removing Docker Containers and Images

The Docker images and containers, if any, may be removed after the C++ application has been run. Remove all Docker containers that have exited.

  1. sudo docker rm $(sudo docker ps --a --q)

All exited Docker container should get removed.

Figure 20: The Docker containers are removed

Remove the Docker image helloworld:v1 with the docker rmi command.

  1. docker rmi helloworld:v1

The Docker image gets removed.

Figure 21: The Docker images are removed

Listing the Docker images may still list some images and some images could be called <none>.

  1. docker images

The <none> images are called dangling images. These are just some images that did not get downloaded properly or built properly.

Figure 22: Showing the dangling images

Remove all the dangling images.

  1. sudo docker rmi $(sudo docker images --f
  2. "dangling=true" --q)

All the dangling images should get removed.

Figure 23: The dangling images are now gone

Listing the images lists only the gcc:4.9 Docker image, which could be kept for subsequent use.

Figure 24: The remaining gcc:4.9 Docker image

Conclusion

In this tutorial, we introduced using C++ with Docker Engine.

(0)

相关推荐

  • Kubernetes官方java客户端之四:内部应用

    内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS等: 概览 本文是<Kubernetes官方java客户端>系列的第四篇,以下提到的ja ...

  • 设置非root账号不用sudo直接执行docker命令

    https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS等: 环境信息 操作系统: ...

  • 在RK3399开发板香橙派Orange Pi 4B上使用docker

    香橙派4B是一款开源的单板电脑,采用瑞芯微RK3399芯片,4G内存+16GB emmc存储,支持双频wifi和千兆网口,有PCIE接口,支持双路摄像头同时输入,支持四路显示,可配置任意两路同时输出, ...

  • Docker定时备份MySQL数据到七牛云

    前言:我Linux服务器安装了docker,docker容器跑了springboot项目,用到了mysql数据库.所以必须准备程序,数据备份功能,万一哪天系统挂了,数据丢了,我可以随时恢复.因为没钱开 ...

  • .NET之Docker部署详细流程

    dotNET跨平台 今天 以下文章来源于鹏祥 ,作者AZRNG 开篇语 自己从头开始走一遍docker部署.net的流程,作为一种学习总结,以及后续会写一些在该基础之上的文章. 本次示例环境:vs20 ...

  • Docker 兴衰记:关于开源的一些思考

    Docker support in the kubelet is now deprecated and will be removed in a future release. The kubelet ...

  • (40条消息) 云原生的 WebAssembly 能取代 Docker 吗?

    WebAssembly 是一个可移植.体积小.加载快并且兼容 Web 的全新格式.由于 WebAssembly 具有很高的安全性,可移植性,效率和轻量级功能,因此它是应用程序安全沙箱方案的理想选择.现 ...

  • 创建用于AI和机器学习的Docker容器

    容器技术(例如Docker)极大地简化了依赖性管理和软件的可移植性.在本系列文章中,我们将探讨Docker在机器学习(ML)场景中的用法. 本系列假定您熟悉ML,一般的容器化,尤其是Docker.欢迎 ...

  • 在启用GPU的Docker容器中运行AI模型

    容器技术(例如Docker)极大地简化了依赖性管理和软件的可移植性.在本系列文章中,我们将探讨Docker在机器学习(ML)场景中的用法. 本系列假定您熟悉ML,一般的容器化,尤其是Docker.欢迎 ...

  • 教你如何使用群暉Docker安裝比emby plex更好用Jellyfin家庭影院HTPC

    大家好,俺又來了,這次給大家分享群暉Docker安裝Jellyfin了! 這個是算是俺的壓軸節目了,最近一口氣寫了好多關於群暉NAS軟體的分享,腦袋都是疼的! 關於jellyfin,最開始知道這個的時 ...

  • (40条消息) WASM能否取代Docker?

    云计算.微服务计算.无服务器计算.可扩展计算.可负担计算等等,这一切主要靠一项杰出的技术--Linux容器(LXC)来实现. Linux容器(LXC)提供了操作系统级的虚拟化沙箱.简而言之,容器允许在 ...

  • 群晖docker安装Emby并开启硬件解码

    最近在群晖中安装了Emby,使用体验很不错,现在将自己的安装过程分享出来 目前群晖中安装emby主要有两种方式,一是通过在套件中心中添加源来安装,二是通过docker来安装.我选择的是通过docker ...