How to Extract Files from Docker Container


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content


This tutorial will be using several software packages. How to use these packages are outside of the scope of this tutorial. Nevertheless, the prerequisites to understand this tutorial are listed below:

  • Proficiency in compiling code, preferably in C++
  • Knowledge of the CMake software build system
  • Linux
  • Shell scripting, preferably in Bash
    • Bash documentation link
  • Docker

Problem Statement

There is a source code repository. Compile the source code in a Docker container. Extract the output of the compilation in the host system. Hence, the host system will only require Docker to be installed to compile the code and get output.

The source code is in C++. The repository is set up to use CMake to compile the code. There is a build script ( in the repository written for Bash. Calling this script will compile the code. The build directory, where the output will be located, can be found in the build script.

Host system

The code in this tutorial was run on Debian Linux with Docker CLI installed in it.

The Dockerfile

Let's start with the Dockerfile. It will define a Docker image that will be used to run a container.

FROM debian:12.1

# Install necessary software packages and clean up
RUN apt update && apt install -y \
  cmake \
  gcc \
  g++ \
  git && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/*

# Define working directory

# Download source code
RUN git clone

# Mark the build script as executable
RUN chmod +x starter/

# Start bash shell for an interactive session
CMD ["/bin/bash"]

Save this file as Dockerfile.

Building a Docker Image

Let's have a convenient script to help with various docker commands. Write a file called with the following code:




# Build docker image

  docker build \
    --file Dockerfile \
    --metadata-file docker-build.log \
    --progress auto \
    --tag "${image}" \

${FUNCTION} $2 $3

A Docker image can be built by invoking the following command:

./ build <name> <tag>

Replace <name> & <tag> with your desired name and tag for the image. Once built, it can be listed by docker images command.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content