1

Here is my file structure:

-- Dir1
---- Dockerfile
---- index.php
-- Dir2
---- Dockerfile
---- index.php
-- SharedLibrary
---- class.php

I want to build multiple images with docker-compose, and they both need the shared file in the other directory.

But Dockerfile can't add files with a relative path.

What does one usually do to resolve this?

By the way, it's not only run with local docker, so it should not handle with volume.


Additional Information:

  • I want to make a shellscript to copy the required files to each directory and run docker-compose.
  • I just want to know how to solve this issue gracefully.
fire790620
  • 49
  • 8
  • Please don't just ask us to solve the problem for you. Show us how you tried to solve the problem yourself, then show us exactly what the result was, and tell us why you feel it didn't work. Give us a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Jul 08 '20 at 12:11
  • You need to get in the habit of [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which help you to solve your issues. You'll earn points and others will be encouraged to help you. – Jay Blanchard Jul 08 '20 at 12:12
  • I was saw this page , but fail to try last time. That I will try it again. – fire790620 Jul 08 '20 at 13:19

1 Answers1

2

You will have to move the Dockerfiles to the root directory (so above SharedLibrary in your case).

Docker has something called the "Build Context". Usually the directory the Dockerfile is in. The builder only has access to files in and below that, never above.

You can make two Dockerfiles like:

-<root>
-- Dir1
---- index.php
-- Dir2
---- index.php
-- SharedLibrary
---- class.php
-- Dockerfile.app1
-- Dockerfile.app1
-- docker-compose.yaml

And then specify those files in docker-compose.yaml.


Personally I would do it differently: I would merge the two into a multistage Dockerfile and specify a target in docker-compose.yaml. You'll end up with something like:

# docker-compose.yml
version: '3.8'
services:
  app1:
    build:
      context: .              # default
      dockerfile: Dockerfile  # default
      target: app1
  app2:
    build:
      context: .              # default
      dockerfile: Dockerfile  # default
      target: app2    
Rutger de Knijf
  • 1,112
  • 14
  • 23
  • it seem useful , but actually my porject include more than 50 images – fire790620 Jul 08 '20 at 13:15
  • all in one dockerfile is a good idea? – fire790620 Jul 08 '20 at 13:16
  • I'm curious what you are doing that requires 50+ images. But yes, especially when you have so many I expect your Dockerfiles share a lot of the same lines. Multistage allows you to reuse those, making everything faster to build and cleaner to read. – Rutger de Knijf Jul 08 '20 at 13:44
  • I try to separate backend api to each image , and it can build in different k8s pod. I don't know that this is a normal used or not. – fire790620 Jul 08 '20 at 14:03
  • finally i use multi stage to solve this situation. – fire790620 Jul 08 '20 at 14:04
  • This was a great example - for people using ECS/ECR/Fargate, I’d suggest the former option of promoting the Dockerfiles to root if you’re looking to temporarily avoid having to publish your common library – C Bauer Dec 24 '22 at 17:24