top of page

Docker to run and compile C++ code

You can use Docker to run and compile C++ code even if your local machine doesn’t have any compiler or build tools installed (like g++, clang, or make).



GOAL

You’ll:

  1. Pull a Docker image that already has a C++ compiler.

  2. Run a container from that image.

  3. Compile and execute a C++ program inside the container.

Step 1. Check Docker is working

Run:

docker --version
docker run hello-world

If you see “Hello from Docker!”, it means Docker is installed and running.


Step 2. Pull a C++-ready image

There are many Docker images that already include C++ compilers.The most common choices:

Image

Includes

gcc

GNU C/C++ compiler

ubuntu

Basic OS (you can install g++)

clang

LLVM/Clang compiler

For most uses:

docker pull gcc
ree

This downloads an image with g++ preinstalled.

Step 3. Run a container interactively

Start a container and enter its shell:

docker run -it gcc bash

You’ll now be inside a Linux shell that already has a C++ compiler.

Check the compiler:

g++ --version
ree

Step 4. Create a small C++ program

Inside the container, use any editor (like nano) or echo command:

echo '#include <iostream>
using namespace std;
int main() {
    cout << "Hello from Docker C++!" << endl;
    return 0;
}' > hello.cpp

ree

Step 5. Compile the code

Run:

g++ hello.cpp -o hello

That creates an executable file hello.

Step 6. Run the program

Execute it:

./hello

You should see:

Hello from Docker C++!
ree

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

Recommended Products For This Post
 
 
 

Recent Posts

See All
Containerization

Docker Zoo to understand containerization Run 3 tiny web apps (Cat, Dog, Cow) in separate containers  and see how containerization isolates them. Folder setup Create a folder for your project: md dock

 
 
 

Comments


© 2023 by newittrendzzz.com 

  • Facebook
  • Twitter
  • Instagram
bottom of page