I think the mistake you are making is you are trying to build your program in docker. What you should be doing is using your favourite build system to build your artifact, and that build system should call docker, if you select a target to build the container. The only docker commands you then need are a COPY to get them from where they were built, and the RUN command to run whatever the artifact is. I'm a bit confused by your example because it appears to be building a library, and you can't run a library in a container.. at least not by itself, you need some kind of server program to run.