Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose volume mount before run

I have a Dockerfile I'm pointing at from a docker-compose.yml.

I'd like the volume mount in the docker-compose.yml to happen before the RUN in the Dockerfile.

Dockerfile:

FROM node  WORKDIR /usr/src/app  RUN npm install --global gulp-cli \  && npm install  ENTRYPOINT gulp watch 

docker-compose.yml

version: '2'  services:   build_tools:     build: docker/gulp     volumes_from:       - build_data:rw    build_data:     image: debian:jessie     volumes:       - .:/usr/src/app 

It makes complete sense for it to do the Dockerfile first, then mount from docker-compose, however is there a way to get around it.

I want to keep the Dockerfile generic, while passing more specific bits in from compose. Perhaps that's not the best practice?

like image 559
DanielM Avatar asked May 07 '16 13:05

DanielM


People also ask

How do I mount a volume in running docker container?

Cloning From An Existing Container But, if you do need to add a volume to a running container, you can use docker commit to make a new image based on that container, and then clone it with the new volume. Then, you can run the new image, replacing the old image with the cloned one.

Are docker volumes available during build?

Although there's no functionality in Docker to have volumes at build-time, you can use multi-stage builds, benefit from Docker caching and save time by copying data from other images - be it multi-stage or tagged ones.

Can data volumes can be mounted in read only mode?

Use a read-only volume Remember that multiple containers can mount the same volume, and it can be mounted read-write for some of them and read-only for others, at the same time.

Can you mount a docker volume on the host?

You can mount host volumes by using the -v flag and specifying the name of the host directory. Everything within the host directory is then available in the container. What's more, all the data generated inside the container and placed in the data volume is safely stored on the host directory.


1 Answers

Erik Dannenberg's is correct, the volume layering means that what I was trying to do makes no sense. (There is another really good explaination on the Docker website if you want to read more). If I want to have Docker do the npm install then I could do it like this:

FROM node  ADD . /usr/src/app WORKDIR /usr/src/app  RUN npm install --global gulp-cli \  && npm install  CMD ["gulp", "watch"] 

However, this isn't appropriate as a solution for my situation. The goal is to use NPM to install project dependencies, then run gulp to build my project. This means I need read and write access to the project folder and it needs to persist after the container is gone.


I need to do two things after the volume is mounted, so I came up with the following solution...

docker/gulp/Dockerfile:

FROM node  RUN npm install --global gulp-cli  ADD start-gulp.sh .  CMD ./start-gulp.sh 

docker/gulp/start-gulp.sh:

#!/usr/bin/env bash  until cd /usr/src/app && npm install do     echo "Retrying npm install" done gulp watch 

docker-compose.yml:

version: '2'  services:   build_tools:     build: docker/gulp     volumes_from:       - build_data:rw    build_data:     image: debian:jessie     volumes:       - .:/usr/src/app 

So now the container starts a bash script that will continuously loop until it can get into the directory and run npm install. This is still quite brittle, but it works. :)

like image 50
DanielM Avatar answered Sep 24 '22 18:09

DanielM