This project demonstrates a modern C++ development setup using Docker containers for building and running applications. The setup provides both Docker-based and local development workflows.
todo/
βββ src/ # Source code directory
β βββ hello.cpp # Main C++ source file
βββ builds/ # Output directory for compiled executables
β βββ hello # Release version (optimized)
β βββ hello-debug # Debug version (with symbols)
βββ .vscode/ # VS Code configuration
β βββ tasks.json # Build and run tasks
β βββ launch.json # Debug configuration
β βββ c_cpp_properties.json # IntelliSense settings
βββ Dockerfile # Unified Docker configuration
βββ docker-compose.yml # Docker Compose configuration
βββ build.sh # Build script
βββ Makefile # Simplified build automation
βββ .dockerignore # Docker ignore file
βββ README.md # This file
- Docker installed on your system
- Docker Compose (usually included with Docker)
- Ubuntu Linux system or similar
- g++ compiler and build tools
- VS Code with C++ extensions
# Build both debug and release versions with one command
make
# Run release version
./builds/hello
# Run debug version
./builds/hello-debug# Open interactive shell in container with all dev tools
make docker-shell
# Debug with GDB in container
make docker-debug# Build and run with docker-compose
make compose-run
# Interactive debug shell with docker-compose
make compose-debug# Update package index
sudo apt update
# Install prerequisites
sudo apt install apt-transport-https ca-certificates curl software-properties-common
# Add Docker's GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add Docker repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Install Docker
sudo apt update
sudo apt install docker-ce
# Add user to docker group (optional, to run without sudo)
sudo usermod -aG docker $USER
# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Verify installation
docker --version
docker-compose --version# Install build essentials and debugger
sudo apt update
sudo apt install build-essential gdb
# Verify installation
g++ --version
gdb --versionNote: If you don't have gdb installed, you can still build and run the program, but VS Code debugging won't work. Use the "C++ Run (No Debug)" configuration in that case.
Install these extensions for the best development experience:
- C/C++ (ms-vscode.cpptools)
- C/C++ Extension Pack (ms-vscode.cpptools-extension-pack)
- Docker (ms-azuretools.vscode-docker)
# Show all available commands
make help
# Production builds
make # Build with Docker (default)
make docker-build # Build production Docker image
make docker-run # Run in production container
# Development builds (with debugging tools)
make docker-build-dev # Build development image with GDB, Valgrind, etc.
make docker-dev # Run development container interactively
make compose-dev # Run development setup with docker-compose
# Local development
make dev # Build and run locally
make local-build # Build locally only
# Container orchestration
make compose-run # Run with docker-compose (production)
# Cleanup
make clean # Clean all artifacts and images
make docker-purge # Nuclear option: stop and remove ALL Docker containers/images/volumesUse Ctrl+Shift+P β "Tasks: Run Task" and choose:
- Docker Build - Build the application in Docker
- Docker Build and Extract - Build and copy executable to local builds/
- Docker Run - Run the application in Docker
- Local Build (Development) - Build locally for debugging
- Run Local Build - Build and run locally
# Build Docker image
docker build -t hello-world-cpp .
# Run in Docker container
docker run --rm hello-world-cpp
# Copy executable from container to local builds/
docker create --name temp-container hello-world-cpp
docker cp temp-container:/app/builds/hello ./builds/
docker rm temp-container# Use the automated build script
./build.sh- Edit source files in
src/ - Build with Docker:
make docker-build - Test locally:
./builds/hello - For debugging: Use local build and VS Code debugger
- Edit source files in
src/ - Build locally:
make local-buildormake dev - Debug with VS Code: Press
F5
- Edit source files in
src/ - Test in container:
make docker-run - For production: Use
docker-compose up
- Use the "Local Build (Development)" task to build with debug symbols
- Set breakpoints in VS Code
- Press
F5to start debugging - Use the "C++ Debug (Local)" configuration
F5- Start/ContinueF10- Step overF11- Step intoShift+F11- Step outShift+F5- Stop
- Base: Ubuntu 22.04 for stability
- Build Tools: build-essential, g++, make for compilation
- Debugging: GDB for interactive debugging, Valgrind for memory analysis
- Editors: Vim, Nano for in-container editing
- Dual Builds: Creates both optimized (-O2) and debug (-g -O0) versions
- Clean Design: Single Dockerfile handles all use cases
- cpp-app: Runs the optimized release version
- cpp-app-debug: Interactive shell for development and debugging
- Volume Mounting: Live sync of source code and build artifacts
- Flexible Commands: Easy switching between run modes
# Quick build and test
make docker-build && ./builds/hello
# Interactive development
make docker-shell # Full shell access with all tools
# Debugging session
make docker-debug # Direct GDB access
# Memory analysis
make docker-shell
# Inside container:
valgrind ./builds/hellogdb ./builds/hello-debug # Debug with GDB valgrind ./builds/hello # Check for memory leaks strace ./builds/hello # Trace system calls
## Troubleshooting
### Docker Issues
1. **Permission denied**: Add user to docker group
```bash
sudo usermod -aG docker $USER
# Log out and back in
-
Build fails: Check Docker daemon is running
sudo systemctl status docker sudo systemctl start docker
-
Out of space: Clean Docker images
docker system prune
-
g++ not found locally: Install build-essential
sudo apt install build-essential
-
VS Code debugging not working: Install GDB debugger
sudo apt install gdb
If you can't install GDB, use the "C++ Run (No Debug)" configuration instead.
-
VS Code IntelliSense issues: Reload window
Ctrl+Shift+P β "Developer: Reload Window"
When you run the application, you should see:
hello world
- Add more C++ source files to
src/ - Implement CMake for complex build systems
- Set up CI/CD with GitHub Actions using Docker
- Add unit testing framework
- Explore multi-stage Docker builds for smaller production images
Happy coding! π
The project includes configuration files in the .vscode directory:
tasks.json- Build configurationlaunch.json- Debug configurationc_cpp_properties.json- IntelliSense configuration
The setup uses g++ with the following default settings:
- C++17 standard
- Debug symbols enabled
- All warnings enabled
- Open the project in VS Code
- Press
Ctrl+Shift+Pto open Command Palette - Type "Tasks: Run Task" and select it
- Choose "Build C++" to compile
- Choose "Run C++" to execute
# Compile the program
g++ -std=c++17 -Wall -g -o hello hello.cpp
# Run the program
./hello- Open
hello.cppin VS Code - Press
Ctrl+Alt+Nor right-click and select "Run Code"
# Build the program
make
# Build and run the program
make run
# Clean build artifacts
make clean- Open
hello.cppin VS Code - Set a breakpoint by clicking in the gutter next to line numbers
- Press
F5or go to Run β Start Debugging - Choose "C++ (GDB/LLDB)" if prompted
- The debugger will start and stop at your breakpoints
F5- Start/Continue debuggingF10- Step overF11- Step intoShift+F11- Step outShift+F5- Stop debugging
todo/
βββ hello.cpp # Main C++ source file
βββ hello # Compiled executable (created after build)
βββ Makefile # Build automation file
βββ README.md # This file
βββ .vscode/
βββ tasks.json # Build tasks configuration
βββ launch.json # Debug configuration
βββ c_cpp_properties.json # IntelliSense configuration
Ctrl+Shift+P- Command PaletteCtrl+Shift+B- Build (run build task)F5- Start debuggingCtrl+F5- Run without debuggingCtrl+Alt+N- Run code (Code Runner extension)Ctrl+Shift+X- Extensions viewCtrl+``- Open integrated terminal
-
"g++ not found"
sudo apt update sudo apt install build-essential
-
IntelliSense not working
- Make sure C/C++ extension is installed
- Check
c_cpp_properties.jsonconfiguration - Reload VS Code window (
Ctrl+Shift+Pβ "Developer: Reload Window")
-
Debugging not working
- Ensure
gdbis installed:sudo apt install gdb - Check
launch.jsonconfiguration - Make sure program is compiled with debug symbols (
-gflag)
- Ensure
Now you have a complete C++ development environment! You can:
- Create more complex C++ programs
- Use external libraries
- Set up CMake for larger projects
- Explore advanced debugging features
- Install additional extensions like GitLens, Bracket Pair Colorizer, etc.
When you run the hello world program, you should see:
hello world
Happy coding! π