简体   繁体   中英

Docker does not start cpp application

i am trying to run a cpp application within docker.After i have built the executable and created the Dockerfile , i can not run it inside docker for some reason:

main.cpp

#include<iostream>
#include<chrono>
#include<thread>
#include<string>
#include <unistd.h>

int main(int argc,char *argv[])
{
    std::cout<<"Started daemon..."<<std::endl;
    std::string hostString(argv[1]);
    std::cout<<"HostName:"<<hostString<<std::endl; 
    std::cout<<"Port:"<<std::stoi(argv[2])<<std::endl;
    int i=0;
    while(true){
      std::cout<<"Iterations:"<<i++<<std::endl;
      std::this_thread::sleep_for (std::chrono::seconds(1));
      if(i++>10000) i=0;
    }
    return 0; 
}

Dockerfile

FROM  ubuntu:latest
RUN mkdir -p /home/dockerc
COPY . /home/dockerc
ENTRYPOINT ["/home/dockerc/main","127.0.0.1","8350"]

dockerc folder

  • main.cpp
  • main.exe
  • Dockerfile

I run the following:
g++ main main.cpp
docker build app .
docker images (it shows that app image is created)
docker run app

The build is succesfull but when i hit the run it looks like it blocks.It just does not continue.

在此处输入图片说明

What is wrong?Could someone help me ?I am new to docker.

PS After waiting out for like 10 minutes i get long error message that begins with the following:

$ docker run cpapp
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: container a575e463f193dbc475aab78c1810486e23981a50c                                                           0b731f9c891c4143d0ed5b3 encountered an error during CreateProcess: failure in a Windows system call: The compute system exited unexpecte                                                                                                                                       dly. (0xc0370106)

You should put the complete path in the ENTRYPOINT and add the parameters to your program.

This Dockerfile does the job:

FROM  ubuntu:latest
RUN mkdir -p /home/dockerc
COPY . /home/dockerc
ENTRYPOINT ["/home/dockerc/main", "hostname", "8000"]

replacing hostname and 8000 with the hostname and port that you need.

Edit

I tested your program in Linux, and to make it run I had to:

1) compile for c++11 (because of chrono )

2) add -t to build the docker app

This is the complete list of commands to run:

g++ -o main main.cpp -std=c++11
docker build -t app .
docker run app

and this is the output:

bash输出

replace your RUN cd... by a WORKDIR ...

your cd does not what you expect in this context, and is forgotten at the next line

you can also remove the RUN cd... line and put the whole path in the

ENTRYPOINT

line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM