I have an example script called main.cpp which reads as follows:
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main() {
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin) {
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
The output should be
Foo
Bar: 4
Bar: 4
It is compiled with g++ -o myprogram main.cpp -lboost_signals
With a dockerfile containing:
# Use an official Ubuntu image as the base image
FROM ubuntu:20.04
# Set the working directory inside the container
WORKDIR /app
# Update apt repository and install build tools and Boost library
RUN apt-get update && apt-get install -y \
build-essential \
libboost-all-dev \
&& rm -rf /var/lib/apt/lists/*
# Copy the C++ source code into the container
COPY main.cpp /app/main.cpp
# Compile the program using g++
RUN g++ -o myprogram main.cpp -lboost_signals
# Set the default command to run the compiled program
CMD ["./main"]
The thing is, I'm not convinced this dockerfile is 1) optimal 2) reliable for my needs. How can I improve my dockerfile to further streamline my work? TIA
Further, when running the container, it fails:
docker build -t cpp-boost-example .
[+] Building 33.7s (11/11) FINISHED docker:desktop-linux
=> [internal] load build definition from dockerfile 0.0s
=> => transferring dockerfile: 1.16kB 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:20.04 0.4s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [1/7] FROM docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4ead070431d29b576a530d3166df73ec44affc1cd27555141b 0.0s
=> => resolve docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4ead070431d29b576a530d3166df73ec44affc1cd27555141b 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 114B 0.0s
=> [2/7] RUN ln -snf /usr/share/zoneinfo/$CONTAINER_TIMEZONE /etc/localtime && echo $CONTAINER_TIMEZONE > /etc/timezone 0.1s
=> [3/7] RUN apt-get update && apt-get install -y tzdata 2.7s
=> [4/7] WORKDIR /app 0.0s
=> [5/7] RUN apt-get update && apt-get install -y build-essential libboost-all-dev && rm -rf /var/lib/apt/lists/* 29.8s
=> [6/7] COPY main.cpp /app/main.cpp 0.1s
=> ERROR [7/7] RUN g++ -o myprogram main.cpp -lboost_signals 0.5s
------
> [7/7] RUN g++ -o myprogram main.cpp -lboost_signals:
0.439 /usr/bin/ld: cannot find -lboost_signals
0.439 collect2: error: ld returned 1 exit status
------
dockerfile:23
--------------------
21 | COPY main.cpp /app/main.cpp
22 | # Compile the program using g++
23 | >>> RUN g++ -o myprogram main.cpp -lboost_signals
24 | # Set the default command to run the compiled program
25 | CMD ["./main"]
--------------------
ERROR: failed to solve: process "/bin/sh -c g++ -o myprogram main.cpp -lboost_signals" did not complete successfully: exit code: 1
mainbinary and any runtime dependencies like boost.libboost-all-devdoesn't includeboost_signalslaunchpad.net/ubuntu/focal/amd64/libboost-all-dev/…, signals doesn't exist in modern versions of boost bugs.launchpad.net/ubuntu/+source/boost-defaults/+bug/1876788, you're not using it so why link to it?<regex>library instead of boost. If you need boost for anything more, learn to link it as static library or identify, locate and copy the required dll files; On a typical POSIX system,lddcommand could work.