Write a simple C++ program and a Dockerfile to run it?Sure, here's a simple C++ program that prints
Here is the C++ program in a file named main.cpp:
#include<iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}Next, you need a Dockerfile to set up the environment, compile, and run the program. Here's a basic Dockerfile:
# Use the official gcc image as base
FROM gcc:latest
# Set the working directory
WORKDIR /app
# Copy the C++ file into the container
COPY main.cpp .
# Compile the C++ file
RUN g++ main.cpp -o output
# Run the output file
CMD ["./output"]To build and run the Docker container:
Save the C++ program in a file called
main.cpp.Save the Dockerfile in the same directory as
main.cpp.Run
docker build -t my_cpp_app .in your terminal to build the Docker image.Run
docker run -it --rm my_cpp_appto run the C++ program in a Docker container.
This will create a Docker image with the GCC compiler, copy your source code into the image, compile it into an executable file named output, and run it when the container is started.
PreviousCPP_Program_v2_DockerfileNextQ: Can you explain with examples the C++ Template programming model, and how to compile and run a Te
Last updated