How can I create a dockerfile for .deb package containing a c++ source file?

时间:2018-12-03 13:06:53

标签: c++ docker debian dockerfile

C++ file with name first.cpp

#include<iostream>

using namespace std;

int main(){
    cout << "Hello world..!!";
    return 0;
}

Below is my dockerfile..

from debian:latest
LABEL:first
LABEL DESCRIPTION:Print Hello world on the screen
MAINTAINER saurav <sauravgore97@gmail.com>
COPY . /var/www/deb
WORKDIR /var/www/deb
RUN ./first
CMD ["./first"]

I tried with above dockerfile but I keep failing..gives me the error that the directory "deb" does not exist. In fact it is in the same directory as the dockerfile where I build.

1 个答案:

答案 0 :(得分:2)

要使cpp文件运行,首先需要对其进行编译,为此我们需要一个编译器。由于g ++并未预装在debian:stretch上(这是我所使用的),因此需要首先安装它。

FROM debian:stretch
# Set the working directory
WORKDIR /tmp 
# Install the compiler
RUN apt-get update && apt-get install g++ -y
# Copy the file containing the source code to WORKDIR/first.cpp
COPY first.cpp first.cpp
# Compile the program
RUN g++ first.cpp -o first
# Set the compiled program as the main command of the container
CMD ["./first"]

使用以下方法构建它:

docker build -f Dockerfile  . -t=first-cpp

并使用以下命令运行它:

docker run -ti first-cpp

这将依次运行容器并仅打印出:

Hello world..!!