top of page

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

 
 
 

Updated: Nov 5, 2025

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 docker-zoo
cd docker-zoo

Cat App

Create a new folder:

md cat
cd cat
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Hi, I’m the Cat app! I live inside my own Docker container."

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=4001)

requirements.txt

flask

Dockerfile

FROM python:3.9-slim
WORKDIR /code
COPY . .
RUN pip install -r requirements.txt
EXPOSE 4001
CMD ["python", "app.py"]

Dog App

Go back and make another folder:

cd ..
md dog
cd dog
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Woof! The Dog app is barking happily inside its Docker container."

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=4002)

requirements.txt

flask

Dockerfile

FROM python:3.9-alpine
WORKDIR /project
COPY . .
RUN pip install -r requirements.txt
EXPOSE 4002
CMD ["python", "app.py"]

Cow App

cd ..
cd cow
cd cow
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Moo! The Cow app is grazing peacefully inside its own container."

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=4003)

requirements.txt

flask

Dockerfile

FROM python:3.9-slim
WORKDIR /workspace
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 4003
CMD ["python", "app.py"]

Build all images

From main docker-zoo/ directory:

docker build -t cat-service ./cat
docker build -t dog-service ./dog
docker build -t cow-service ./cow

Run all containers

docker run -d -p 4001:4001 cat-service
docker run -d -p 4002:4002 dog-service
docker run -d -p 4003:4003 cow-service

 
 
 

Part 1: Create Your First Git Repository

Purpose: Learn how Git tracks changes locally — the first step in version control.


Steps

  1. Create a folder

    ree

    You created a project directory to store your code.

  2. Initialize Git

    git init

    ree

    This turns your folder into a Git repository (Git starts tracking changes here).

  3. Create a file

    echo "Hello Git" > hello.txt


    You made your first file to track.

  4. Check the status

    git status


    Git shows that hello.txt is untracked.

  5. Add the file to staging

    git add hello.txt

    ree

    This prepares the file for commit (snapshot).

  6. Commit your changes


    git commit -m "Initial commit: added hello.txt"

    ree

    Congratulations!!! You’ve saved your first version — a snapshot of the file.

Git records your work in small “commits” that you can track or roll back later.


Part 2: Track File Changes and View History

Purpose: Understand how Git saves different versions of your files.

Steps

  1. Edit the file

    echo "This is my second line." >> hello.txt


  2. Check the difference

    git diff

    ree

    Shows what changed since the last commit.

  3. Stage and commit again

    git add hello.txt


    git commit -m "Added a second line to hello.txt"

    ree
  4. View commit history

    git log --oneline

    ree

    See the list of all versions you’ve saved.


Lesson Learned:Git keeps a complete timeline of your work — you can see, compare, or restore any version anytime.


Part 3: Work with Branches

Purpose: Learn how to work on new features safely without disturbing the main code.

Steps

  1. Create and switch to a new branch


    git checkout -b feature-update

ree
  1. Edit the file

    echo "New feature: Git branching!" >> hello.txt


  2. Commit the change

    git add hello.txt

    git commit -m "Added feature update message"

    ree

  3. Switch back to main branch

    git checkout master

    ree

  4. Compare branches

    git diff feature-update

ree


Lesson Learned:Branches let you experiment or develop features independently — and merge them later when ready.

Part 4: Merge Changes

Purpose: Understand how to combine your work from multiple branches.

Steps


  1. Merge feature branch into main

    git merge feature-update

    Git combines the feature branch changes into main.

    ree

  2. Check the merged file

    type hello.txt

    ree


Part 5: Connect with GitHub and Push Code

Purpose: Learn to upload your local project to GitHub and collaborate online.

Steps

  1. Create a new repo on GitHub

    Go to GitHub → “New Repository” → Name it git-demo

    Don’t add README yet.


  2. Connect local Git to GitHub

    git remote add origin https://github.com/<your-username>/git-demo.git

  3. Push your code

    git branch -M master

    git push -u origin master

ree
  1. Check your GitHub accountYour code is now visible online.

    ree

    Lesson Learned:Git + GitHub = version control + cloud collaboration.You can now share your code, work with teams, and track contributions.


 
 
 

© 2023 by newittrendzzz.com 

  • Facebook
  • Twitter
  • Instagram
bottom of page