首页 > 解决方案 > How to change context for Dockerfile using docker composer?

问题描述

I'm developing a CMake project that uses Docker to build on Linux. It's 2 libraries and one executable. One of the libraries is made available in source format only and for which I must provide the CMakeLists.txt file myself.

I can't move or copy that directory to my project folder tree on the root of which is my Dockerfile so I need to find a way to reliably ADD that directory to my container every time I build my project. A solution Google returned is to use docker composer to do so, but I can't get it to work.

This is my docker-composer.yml:

version: '3.8'
services:
  dlstreamer:
    build:
      context: ../
      dockerfile: ./PrivacyContainer/Dockerfile

As context shows, the sources I want are in a directory at the same level as my project directory.

And yet, when I open my folder in a container using Visual Studio code that folder is never copied. What should I be looking for?

标签: linuxdockercmakedocker-compose

解决方案


The docker build documentation page indicates the following :

The context is limited to the current directory (and its children) for security reasons, and to ensure repeatable builds on remote Docker hosts.

This could explain why setting ../ as context doesn't work.

So I think you should launch the docker build command from within your parent directory containing your sources (../), specify the path of your dockerFile using -f and indicate the context Path as . like this :

docker build -f PrivacyContainer/Dockerfile .

This way, your context includes the directory you want and you'll have access to the files you need for COPY etc.


推荐阅读