Shared files between containers with docker












0















I have a project structure like the following



app/
docker-compose.yml
module1/
Dockerfile
module1.py
module2/
Dockerfile
module2.py
common/
common_things.py


In my Dockerfile for module1 I have



COPY module1.py /app
COPY ../common /app/common


But Docker does not like this second line. Error is below



ERROR: Service 'module1' failed to build: COPY failed: Forbidden path outside the build context: ../common ()


How do I tell Docker, through Dockerfile or Docker-compose, that it is okay for module1 to grab files from ../common? I could symlink common so that module1 and module2 have common in their respective dirs but that feels like overkill...



Extra credit: What is best practice for sharing files across Docker containers? Perhaps there is another way that I am unaware of.










share|improve this question























  • Check this question: stackoverflow.com/questions/27068596/… Also this issue: github.com/moby/moby/issues/2745

    – Emruz Hossain
    Nov 22 '18 at 4:04











  • If you need share files between containers, try using -v or volumes options to map host files/dir to your container. with volumes mapping, you don't need to rebuild your image whenever file changed. docs.docker.com/compose/compose-file/…

    – Enix
    Nov 22 '18 at 9:03











  • Possible duplicate of Making a dependencies container and mount its volumes on other containers

    – Siyu
    Nov 22 '18 at 12:28











  • This question is not about volumes as OP searches for a build-time solution.

    – fab
    Nov 22 '18 at 13:24


















0















I have a project structure like the following



app/
docker-compose.yml
module1/
Dockerfile
module1.py
module2/
Dockerfile
module2.py
common/
common_things.py


In my Dockerfile for module1 I have



COPY module1.py /app
COPY ../common /app/common


But Docker does not like this second line. Error is below



ERROR: Service 'module1' failed to build: COPY failed: Forbidden path outside the build context: ../common ()


How do I tell Docker, through Dockerfile or Docker-compose, that it is okay for module1 to grab files from ../common? I could symlink common so that module1 and module2 have common in their respective dirs but that feels like overkill...



Extra credit: What is best practice for sharing files across Docker containers? Perhaps there is another way that I am unaware of.










share|improve this question























  • Check this question: stackoverflow.com/questions/27068596/… Also this issue: github.com/moby/moby/issues/2745

    – Emruz Hossain
    Nov 22 '18 at 4:04











  • If you need share files between containers, try using -v or volumes options to map host files/dir to your container. with volumes mapping, you don't need to rebuild your image whenever file changed. docs.docker.com/compose/compose-file/…

    – Enix
    Nov 22 '18 at 9:03











  • Possible duplicate of Making a dependencies container and mount its volumes on other containers

    – Siyu
    Nov 22 '18 at 12:28











  • This question is not about volumes as OP searches for a build-time solution.

    – fab
    Nov 22 '18 at 13:24
















0












0








0








I have a project structure like the following



app/
docker-compose.yml
module1/
Dockerfile
module1.py
module2/
Dockerfile
module2.py
common/
common_things.py


In my Dockerfile for module1 I have



COPY module1.py /app
COPY ../common /app/common


But Docker does not like this second line. Error is below



ERROR: Service 'module1' failed to build: COPY failed: Forbidden path outside the build context: ../common ()


How do I tell Docker, through Dockerfile or Docker-compose, that it is okay for module1 to grab files from ../common? I could symlink common so that module1 and module2 have common in their respective dirs but that feels like overkill...



Extra credit: What is best practice for sharing files across Docker containers? Perhaps there is another way that I am unaware of.










share|improve this question














I have a project structure like the following



app/
docker-compose.yml
module1/
Dockerfile
module1.py
module2/
Dockerfile
module2.py
common/
common_things.py


In my Dockerfile for module1 I have



COPY module1.py /app
COPY ../common /app/common


But Docker does not like this second line. Error is below



ERROR: Service 'module1' failed to build: COPY failed: Forbidden path outside the build context: ../common ()


How do I tell Docker, through Dockerfile or Docker-compose, that it is okay for module1 to grab files from ../common? I could symlink common so that module1 and module2 have common in their respective dirs but that feels like overkill...



Extra credit: What is best practice for sharing files across Docker containers? Perhaps there is another way that I am unaware of.







docker docker-compose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 1:54









cjavier70cjavier70

981112




981112













  • Check this question: stackoverflow.com/questions/27068596/… Also this issue: github.com/moby/moby/issues/2745

    – Emruz Hossain
    Nov 22 '18 at 4:04











  • If you need share files between containers, try using -v or volumes options to map host files/dir to your container. with volumes mapping, you don't need to rebuild your image whenever file changed. docs.docker.com/compose/compose-file/…

    – Enix
    Nov 22 '18 at 9:03











  • Possible duplicate of Making a dependencies container and mount its volumes on other containers

    – Siyu
    Nov 22 '18 at 12:28











  • This question is not about volumes as OP searches for a build-time solution.

    – fab
    Nov 22 '18 at 13:24





















  • Check this question: stackoverflow.com/questions/27068596/… Also this issue: github.com/moby/moby/issues/2745

    – Emruz Hossain
    Nov 22 '18 at 4:04











  • If you need share files between containers, try using -v or volumes options to map host files/dir to your container. with volumes mapping, you don't need to rebuild your image whenever file changed. docs.docker.com/compose/compose-file/…

    – Enix
    Nov 22 '18 at 9:03











  • Possible duplicate of Making a dependencies container and mount its volumes on other containers

    – Siyu
    Nov 22 '18 at 12:28











  • This question is not about volumes as OP searches for a build-time solution.

    – fab
    Nov 22 '18 at 13:24



















Check this question: stackoverflow.com/questions/27068596/… Also this issue: github.com/moby/moby/issues/2745

– Emruz Hossain
Nov 22 '18 at 4:04





Check this question: stackoverflow.com/questions/27068596/… Also this issue: github.com/moby/moby/issues/2745

– Emruz Hossain
Nov 22 '18 at 4:04













If you need share files between containers, try using -v or volumes options to map host files/dir to your container. with volumes mapping, you don't need to rebuild your image whenever file changed. docs.docker.com/compose/compose-file/…

– Enix
Nov 22 '18 at 9:03





If you need share files between containers, try using -v or volumes options to map host files/dir to your container. with volumes mapping, you don't need to rebuild your image whenever file changed. docs.docker.com/compose/compose-file/…

– Enix
Nov 22 '18 at 9:03













Possible duplicate of Making a dependencies container and mount its volumes on other containers

– Siyu
Nov 22 '18 at 12:28





Possible duplicate of Making a dependencies container and mount its volumes on other containers

– Siyu
Nov 22 '18 at 12:28













This question is not about volumes as OP searches for a build-time solution.

– fab
Nov 22 '18 at 13:24







This question is not about volumes as OP searches for a build-time solution.

– fab
Nov 22 '18 at 13:24














1 Answer
1






active

oldest

votes


















0














You can achieve it by passing the entire app directory as build-context.



The docker-compose.yml would look like this:



version: '3'
services:
module1:
build:
context: ./
dockerfile: module1/Dockerfile
...


The (first) Dockerfile would look like this:



...
COPY module1/module1.py /app
COPY common /app/common
...


Some further comments:



Normally you would publish common parts of your two docker images as a shared library. I'm not very familiar with python but I believe it would boil down to:




  • publish your common_things.py as a python package on some python repository, so that it can be installed through pip install

  • add a requirements.txt file in each of your Docker-images and refer to your python package inside it

  • run pip install -r requirements.txt during your Docker-build to install your common_things-package in the docker-image.


However, this might be overkill for your usecase, so I think your solution for sharing the file might be the good choice for you.






share|improve this answer


























  • Changing context was exactly what I needed. Thank you

    – cjavier70
    Nov 24 '18 at 20:46












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422846%2fshared-files-between-containers-with-docker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can achieve it by passing the entire app directory as build-context.



The docker-compose.yml would look like this:



version: '3'
services:
module1:
build:
context: ./
dockerfile: module1/Dockerfile
...


The (first) Dockerfile would look like this:



...
COPY module1/module1.py /app
COPY common /app/common
...


Some further comments:



Normally you would publish common parts of your two docker images as a shared library. I'm not very familiar with python but I believe it would boil down to:




  • publish your common_things.py as a python package on some python repository, so that it can be installed through pip install

  • add a requirements.txt file in each of your Docker-images and refer to your python package inside it

  • run pip install -r requirements.txt during your Docker-build to install your common_things-package in the docker-image.


However, this might be overkill for your usecase, so I think your solution for sharing the file might be the good choice for you.






share|improve this answer


























  • Changing context was exactly what I needed. Thank you

    – cjavier70
    Nov 24 '18 at 20:46
















0














You can achieve it by passing the entire app directory as build-context.



The docker-compose.yml would look like this:



version: '3'
services:
module1:
build:
context: ./
dockerfile: module1/Dockerfile
...


The (first) Dockerfile would look like this:



...
COPY module1/module1.py /app
COPY common /app/common
...


Some further comments:



Normally you would publish common parts of your two docker images as a shared library. I'm not very familiar with python but I believe it would boil down to:




  • publish your common_things.py as a python package on some python repository, so that it can be installed through pip install

  • add a requirements.txt file in each of your Docker-images and refer to your python package inside it

  • run pip install -r requirements.txt during your Docker-build to install your common_things-package in the docker-image.


However, this might be overkill for your usecase, so I think your solution for sharing the file might be the good choice for you.






share|improve this answer


























  • Changing context was exactly what I needed. Thank you

    – cjavier70
    Nov 24 '18 at 20:46














0












0








0







You can achieve it by passing the entire app directory as build-context.



The docker-compose.yml would look like this:



version: '3'
services:
module1:
build:
context: ./
dockerfile: module1/Dockerfile
...


The (first) Dockerfile would look like this:



...
COPY module1/module1.py /app
COPY common /app/common
...


Some further comments:



Normally you would publish common parts of your two docker images as a shared library. I'm not very familiar with python but I believe it would boil down to:




  • publish your common_things.py as a python package on some python repository, so that it can be installed through pip install

  • add a requirements.txt file in each of your Docker-images and refer to your python package inside it

  • run pip install -r requirements.txt during your Docker-build to install your common_things-package in the docker-image.


However, this might be overkill for your usecase, so I think your solution for sharing the file might be the good choice for you.






share|improve this answer















You can achieve it by passing the entire app directory as build-context.



The docker-compose.yml would look like this:



version: '3'
services:
module1:
build:
context: ./
dockerfile: module1/Dockerfile
...


The (first) Dockerfile would look like this:



...
COPY module1/module1.py /app
COPY common /app/common
...


Some further comments:



Normally you would publish common parts of your two docker images as a shared library. I'm not very familiar with python but I believe it would boil down to:




  • publish your common_things.py as a python package on some python repository, so that it can be installed through pip install

  • add a requirements.txt file in each of your Docker-images and refer to your python package inside it

  • run pip install -r requirements.txt during your Docker-build to install your common_things-package in the docker-image.


However, this might be overkill for your usecase, so I think your solution for sharing the file might be the good choice for you.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 13:19

























answered Nov 22 '18 at 13:03









fabfab

91811326




91811326













  • Changing context was exactly what I needed. Thank you

    – cjavier70
    Nov 24 '18 at 20:46



















  • Changing context was exactly what I needed. Thank you

    – cjavier70
    Nov 24 '18 at 20:46

















Changing context was exactly what I needed. Thank you

– cjavier70
Nov 24 '18 at 20:46





Changing context was exactly what I needed. Thank you

– cjavier70
Nov 24 '18 at 20:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422846%2fshared-files-between-containers-with-docker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?