Welcome to the next pikoTutorial!
If you ever wanted to ensure a consistent environment for your application, you most probably know that containerization is the way to go. To containerize a C++ application, let’s first set up a simple file structure:
cpp_docker
|- src
| |- main.cpp
|- Dockerfile
|- CMakeLists.txtNow add a simple main function:
#include <iostream>
#include <thread>
#include <chrono>
int main(int argc, char** argv)
{
while (true) {
std::cout << "Hello from the inside of the container" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}And a simple CMakeLists.txt file:
cmake_minimum_required(VERSION 3.15)
project(CppDocker)
add_executable(${PROJECT_NAME})
target_sources(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)
Having this, it’s time to write a Dockerfile:
# Use an official Ubuntu base image
FROM ubuntu:20.04
# Assure non-interactive mode of installers
ENV DEBIAN_FRONTEND=noninteractive
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
cmake
# Copy necessary files
COPY ./src /app/src
COPY ./CMakeLists.txt /app
# Build the application
WORKDIR /app/build
RUN cmake ..
RUN cmake --build .
# Run the application
CMD ["./CppDocker"]
AI is powerful. Snippets are instant.
Stop prompting for the same patterns repeatedly. Get almost 100 free VS Code snippets for C++, Python, CMake and Bazel from piko::snippets GitHub repository.
When the Dockerfile is ready, you can build a Docker image by running the following command:
docker build -t cpp_docker_image .After you get familiar with docker build, you may want to get familiar with more powerfull docker buildx.
If you don’t want to use sudo every time when calling docker command, you may want to change the permissions of one of the docker files by running sudo chmod 666 /var/run/docker.sock.
Note for beginners: remember that changing permissions of /var/run/docker.sock may have consequences related to security, so you want to think twice before you do this in the production environment!
After successful build, you can show the list of existing images by running:
docker imagesThis will show you a table like the following:
REPOSITORY TAG IMAGE ID CREATED SIZE
cpp_docker_image latest e132d245100b About a minute ago 450MBTo create a container out of your image, use docker run command:
docker run cpp_docker_imageThis is the simplest form of this command, but most likely you would like to extend it with the following option and flags:
--rm– removes container after it has been stopped--name– allows you to assign a custom name to your container (otherwise Docker will choose some random name)-d– detaches process from your current terminal what effectively makes the container to run in the background.
Eventually our command looks like this:
docker run --rm --name my_cpp_application -d cpp_docker_imageNow you can check the table of currently running containers with docker ps command. If everything went ok, you should see such output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a99c1e9e0bbc cpp_docker_image "./CppDocker" 12 seconds ago Up 11 seconds my_cpp_applicationFinally, let’s look at the logs from the application. Run docker logs my_cpp_application and check if you see the following output:
Hello from the inside of the container
Hello from the inside of the container
Hello from the inside of the containerIf yes, you’ve just successfully containerized a C++ application.
Note for beginners: if you leave the container like this, it will run in the background forever. To stop the application, run
docker container stop my_cpp_applicationcommand.
Note for advanced: if you need more in-depth look into your container, you can get inside the container’s shell terminal using command
docker container exec -it my_cpp_application /bin/bash. You can also do this at the container startup by using commanddocker run -it cpp_docker_image /bin/bash.
Read also:
- GTest and short-circuit evaluation in C++
- AI is powerful. Snippets are instant.
- From AUTOSAR to S-Core: the first C++ pub/sub implementation
- How to write Arduino Uno code with Python?
- Combining Bazel with Docker
- Running commands with timeout on Linux
- Running Python unit tests with CMake
- Thirdparty dependencies with FetchContent
- Bug of the week #11
- Combining CMake with Docker
- How to search the internet from Linux terminal?
- Folding expressions in C++
- How to derive from an enum in Python?
- Bug of the week #10
- Trying ROS2: client/server within a single container
- Make C++ a better place #4: Go as an alternative
- How to convert hex to dec in Linux terminal?
- Setting up a Python project with CMake
- Separating builds for different configs with Bazel
- Trying ROS2: pub/sub within a single container
- Bug of the week #9
- UDP multicasting with Python
- Destruction order vs thread safety in C++
- Let’s review some code: C++ #2
- Make C++ a better place #3: D as an alternative
- Registering callback using std::function in C++
- Bug of the week #8
- TCP client/server with Python
- Simple menus in Bash scripts with select
- Calling member function on a nullptr in C++
- Bug of the week #7
- Python lru_cache explained
- How to dockerize a Python application?
- Make C++ a better place #2: CppFront as an alternative
- Parameters combinations in GoogleTest
- Data transfer with curl
- Python reduce explained
- Bug of the week #6
- Custom literals in C++
- Linux and hash command
- 5 Python good practices which make life easier
- Let’s review some code: Python #1
- Make C++ a better place #1: What does better mean
- Enums vs enum class in C++
- Bug of the week #5
- UDP client/server with Python
- Hard links in Linux
- Functions calling order in unit tests in C++
- Bug of the week #4
- Yield in Python – state machines, coroutines and more
- Copy files from another branch with Git
- Make C++ a better place #0: Introduction
- 5 misconceptions about std::move in C++
- How to use xargs on Linux?
- How to test method call order with unittest in Python?
- Bug of the week #3
- Build & run C++ unit tests with CMake
- Arrange text with sort on Linux
- Key derivation function with Python
- Let’s review some code #1: C++









