4

I'm using docker and docker-compose for building my app. There are two developers now for the project hosted on github.

Our project structure is:

  • sup
    • dockerfiles
      • dev
        • build
          • .profile
          • Dockerfile
        • docker-compose.yml

Now we have ./dockerfiles/dev/docker-compose.yml like this:

app:
    container_name: sup-dev
    build: ./build

and ./dockerfiles/dev/build/Dockerfile:

FROM sup:dev

# docker-compose tries to find .profile relative to build dir:
# ./dockerfiles/dev/build
COPY .profile /var/www/

We run container like so:

docker-compose up -d

Everything works fine, but due to different OS we have our code in different places: /home/aliance/www/project for me and /home/user/other/path/project for the second developer. So I can not just add volume instruction into Dockerfile.

Now we solve this problem in this wrong way: - I am using lsyncd with my personal config to transfer files into the container - While the second one uses volume instruction into Dockerfile but not commited it.

May be you know how can I write an unified Dockerfile for docker-compose to volume out code into app container from different paths?

Aliance
  • 847
  • 1
  • 11
  • 23

2 Answers2

3

The file paths on the host shouldn't matter. Why do you need absolute paths?

You can use paths that are relative to the docker-compose.yml so they should be the same for both developers.

The VOLUME instructions in the Dockerfile are always relative to the build context, so if you want, you can use something like this:

app:
    container_name: sup-dev
    build: ..
    dockerfile: build/Dockerfile

That way the build context for the Dockerfile will be the project root.

dnephin
  • 25,944
  • 9
  • 55
  • 45
  • Hello @dnephin, thanx for your answer. Please correct me if I'm wrong, but will parent paths works? As I remember in security reasons they are disabled. – Aliance Nov 26 '15 at 06:52
  • I’ve confused build path and `ADD` instruction into `Dockerfile`. Your solution works for me. What changes I have done: 1) in `docker-compose.yml` I've changed `build` to `../..` and set `dockerfile` to `./dockerfiles/dev/build` and 2) changed path to `.profile` file into my `Dockerfile`, now it became relative to build context — `COPY ./dockerfiles/dev/build/.profile /var/www/` (see my edited question above, I’ve clarified project structure, It has 2 level nesting). Everything looks good now, yeap? – Aliance Nov 26 '15 at 12:33
  • Hmm, but does not work with volume. As a read [here](http://stackoverflow.com/questions/25311613/docker-mounting-volumes-on-host), the `Dockerfile` `VOLUME` and `docker-compose.yml` `volumes` (same as `docker run -v`) is not the same. I want to use the second one, so I tried to add this `../..:/var/www/project` in my `docker-compose.yml` (notice, that path is relative to yml, not build context), all files from app root (build context) are copied to container root path `/var/www/project` but they changed its owner:group at my local host to `www-data`. Is it okay? Or what should I do? – Aliance Nov 26 '15 at 13:32
  • Yes, there are two types of volumes, container volumes (`VOLUME` in a `Dockerfile`) and host volumes (mounting a host path into a container). It sounds like you want a host volume. If they are changing ownership, there must be something that runs in the container which changes their ownership. Ownership of files is preserved. I should be ok, you can just add your user to the www-group and give them `chmod g+w` permision. – dnephin Nov 26 '15 at 21:39
  • Yeap, host volume suits me the best. No, there are noone who changes files permissions. I found your answer to the [similar problem](http://stackoverflow.com/q/33508123/3456488) so this is not only my _local_ problem? I've already thought about adding my local user to `www-data` group, but don't sure this is ok. Ok, I'll try and post a feedback. Thanx for your help! – Aliance Nov 27 '15 at 13:09
  • Tried to add my local user to `www-data` group - everything works fine! Used command: `sudo usermod -aG www-data ` (needs shell restart). Thanx a lot! – Aliance Nov 29 '15 at 12:09
0

Maybe you should keep your Dockerfile at the root of your project. Then you could add an instruction in the Dockerfile:

COPY ./ /usr/src/app/

or (not recommended in prod)

VOLUME /usr/src/app

+ (option while running the container as I don't know docker-compose)

-v /path/to/your/code:/usr/src/app
Abd
  • 16
  • 2
  • Thanx for the answer. «you should keep your Dockerfile at the root of your project» — That doesn’t suits me. «not recommended in prod» — this is need for dev platform, ’cause only dev platform can be run into different environment. Can you explain a little bit more about your solution with «volume path», can’t understand how it can helps me. – Aliance Nov 26 '15 at 06:45