Post

Mini.Project 01-04. Deploy: Integrated CMake, Docker and CI/CD

Mini.Project 01-04. Deploy: Integrated CMake, Docker and CI/CD

Mini.Project 01-04. Deploy: Integrated CMake, Docker and CI/CD


Prerequisites

1
    C++

1. Docker

C++ Project + CMake + Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/Project:
  - "Dockerfile"        # Docker
  - ".dockerignore"     # Docker
  - CMakeLists.txt
  - /cmake:
        - CMakeGenLib.cmake
  - main.cpp
  - /tests:
    - test_svstring.cpp
  - /lib:
    - CMakeLists.txt
    - /structlib:
      - CMakeLists.txt
      - /src:
        - SVBase.cpp
        - SVString.cpp
      - /include:
        - SVBase.h
        - SVString.h
  • Dockerfile acts like “recipe” for building the container image
  • .dockerignore defines which files are excluded when building/pushing the image

2. What is Docker

Docker is a platform that allows developers to package an application together with all its dependencies into a single, portable unit.

Instead of relying on the host system’s environment, the entire runtime—including libraries, configurations, and required binaries—is encapsulated inside a Docker image. This image can then be executed as a container.

By running the application inside a container, Docker ensures that the program behaves consistently across different environments, regardless of the underlying system. In other words, once an application is built as a Docker image, it can be run on any machine with Docker installed, producing the same results every time.

This approach eliminates issues caused by environment differences and simplifies deployment, testing, and distribution.

2-1. Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Builder Stage
FROM ubuntu:24.04 AS builder

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    cmake \
    g++ \
    make && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY . .

RUN cmake -S . -B build -DCMAKE_BUILD_TYPE=Release && \
    echo "===== cmake configure done ====="
RUN cmake --build build && \
    echo "===== cmake build done ====="
RUN echo "===== build dir =====" && \
    ls -la /app
RUN echo "===== build contents =====" && \
    ls -la /app/build

# Runtime Stage
FROM ubuntu:24.04

WORKDIR /app
COPY --from=builder /app/build/deploy-project /app/execute

CMD ["./execute"]

builder environment

1
FROM ubuntu:24.04 AS builder

Choose OS (usually ubuntu) and use LTS version. The builder is just name.

Update packages lists

1
apt-get update

Downloads the latest package index from Ubuntu repositories. Ensures that the package manager installs the most up-to-date versions

Install required packages

1
apt-get install -y --no-install-recommends cmake g++ make
  • -y → Automatically answers “yes” to prompts
  • –no-install-recommends → Installs only essential packages (reduces image size)

Clean required cache

1
rm -rf /var/lib/apt/lists/*
  • Removes cached package lists
  • Reduces final image size

Work directory

1
WORKDIR /app

Copy source code

1
COPY . .

This command copies all files from the current directory on the host machine into the current working directory inside the container.

RUN command

1
2
RUN cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
RUN cmake --build build

For creating images, creating from source code to executable file.

COPY executable file

1
COPY --from=builder /app/build/deploy-project /app/execute

The –from=builder option tells Docker to copy files from a previous build stage instead of the local host.

  • Source: /app/build/deploy-project (inside builder container)
  • Destination: /app/execute (in current container)

CMD

This instruction defines the default command that will be executed when the container starts.

2-2. .dockerignore

/build/
/install/

The .dockerignore file specifies which files and directories should be excluded when copying files into the Docker image.

3. Docker command

1
2
3
4
5
6
7
docker build -t image_name .
docker run --rm image_name
docker tag image_name username/image_name
docker login
docker push username/image_name
docker pull username/image_name
docker run username/image_name

3-1. build the image

1
docker build -t image_name .

Builds a Docker image from the current directory using the Dockerfile.

3-2. run the container

1
docker run --rm image_name

Runs the container to verify that the application works correctly.

  • –rm automatically removes the container after it exits

3-3. tag the image for Docker Hub

1
docker tag image_name username/image_name

Assigns a new tag to the image so it can be pushed to Docker Hub.

3-4. login

1
docker login

Authenticates with Docker Hub (required before pushing images).

3-5. push the image

1
docker push username/image_name

Uploads the image to Docker Hub so it can be accessed remotely.

3-6. pull the image

1
docker pull username/image_name

Downloads the image from Docker Hub.

3-7. run the pulled image

1
docker run username/image_name

Runs the container using the image pulled from Docker Hub.

3-8. check or remove images

1
2
3
4
5
docker ps
docker ps -a
docker images
docker rm address
docker rmi image_name
This post is licensed under CC BY 4.0 by the author.