Code Monkey home page Code Monkey logo

staticdocker's Introduction

A Docker container with nothing but a statically linked C++ executable

An experiment by Richard Eigenmann

Inspired by a talk from Miroszlav Magyarevity at the 17th Docker Switzerland Meetup hosted by Oracle in the Prime Tower in Zürich on 30 May 2018.

Try it

cd <yourWorkDirectory>
git clone [email protected]:richardeigenmann/staticDocker.git
cd staticDocker
docker build --rm=false -t richardeigenmann/hello .
docker run richardeigenmann/hello

How big is it?

# query the built image:
docker images richardeigenmann/hello
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
richardeigenmann/hello   latest              1ee25d730753        28 minutes ago      17MB
richardeigenmann/hello   latest              c6a00ffe0973        23 minutes ago      5.27MB

The first result was from statically linking <iostream> and calling std::cout on a "Hello World" string. 17MB does seem rather a lot but a Google search reveals that this drags a lot of code in. If we switch to <cstdio> and use printf instead, size drops to 5MB. This actually corresponds to the size of the executable as evidenced by the ls -lh command I planted in the compile step.

Explanation

Setting up a development environment for C++ involves intalling a lot of tools and libraries which can lead to problems. A great feature of containers is that you can push this set-up into a container and use different containers for different requirements.

In this project (see the Dockerfile) I have chosen to build a heavyweight 829MB compiling container out of OpenSuse's Tumbleweed distro. This drags in the latest clang and gcc compilers and a number of utilities. If required I would also install libraries here.

The build step then proceeds to copy the CMakeLists.txt and main.cpp files into the compiling conatiner and uses cmake to compile and link the executable.

Using the multi-stage build feature the Dockerfile then specifies that a new container is to be build based on the SCRATCH image which literally is just an empty container. The COPY --from=0 /src/build/hello . statement then copies the executable into the new container and the ENTRYPOINT instruction ensures that it will be called when the container is run.

The TARGET_LINK_LIBRARIES( hello -static ) entry in the CMakeLists.txt (or more specifically the -static keyword) instructs the linker to create a statically linked executable (meaning that there are no external dependencies and the binary has everythzing is needs built in).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.