DEV Community

kai wen ng
kai wen ng

Posted on

Docker as Cross Compilation Platform

Having various dependencies when setting up a software development environment can be frustrating, especially when dealing with legacy C/C++ systems that require specific compiler versions, libraries, and outdated operating systems.

Using Docker as a cross-compilation and development environment can significantly simplify this process.

The main advantages are:

  1. Isolation of dependencies

Docker provides an isolated environment where required compilers, libraries, headers, and system packages can be installed without affecting the host machine. This avoids dependency conflicts and keeps the existing development environment clean.

  1. Reproducible legacy environments

Docker allows developers to recreate older Linux distributions and software environments that match production systems. For example, an Ubuntu 20.04 container can be used to build applications that depend on older libraries or toolchains, even if the host machine is running a newer operating system.

  1. Explicit environment definition

By defining dependencies through Dockerfiles and container configurations, the required build environment becomes clear, reproducible, and easier to maintain. New developers can recreate the same environment without manually installing every dependency.


With this approach, I created a lightweight Docker-based compilation platform for a legacy CGI C/C++ system.

The first step was creating a minimal Docker image based on Ubuntu 20.04. Required header files, compilers, development tools, and libraries were then installed inside the container.

Initially, I considered copying the entire source code and website files into the Docker image and compiling everything during container runtime. However, this approach introduced several issues:

  • The Docker image became unnecessarily large.

  • Every source code change required rebuilding the image.

  • The development cycle became slower due to repeated compilation and image creation.

Instead, I separated the development environment from the source code by mounting the project files into the running container.

The workflow became:

  1. Build the Docker image containing only the required compilation environment and dependencies.

  2. Mount the source code and website resources into the container.

  3. Enter the running container:

sudo docker exec -it <container_name> /bin/bash
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to the mounted project directory.

  2. Run the required compilation commands:

make all
Enter fullscreen mode Exit fullscreen mode

or any other installation/build command required by the project.

With this setup, the host machine does not need to contain the legacy dependencies or libraries. The Docker container provides the complete build environment, while the source code remains directly accessible from the host through volume mounting.

This provides several practical benefits:

  • Source code changes are immediately reflected inside the container.

  • No container rebuild is required after every code modification.

  • No Docker image recompilation is needed during development.

  • The legacy CGI application can be compiled and updated quickly while maintaining a clean host environment.

This approach effectively turns Docker into a portable legacy development workstation, allowing old C/C++ systems to be maintained and modified using modern development workflows.

Top comments (0)