k0pernikus
k0pernikus

Reputation: 66589

How to keep changes inside a container on the host after a docker build?

I have a docker-compose dev stack. When I run, docker-compose up --build, the container will be built and it will execute

Dockerfile:

RUN composer install --quiet

That command will write a bunch of files inside the ./vendor/ directory, which is then only available inside the container, as expected. The also existing vendor/ on the host is not touched and, hence, out of date.

Since I use that container for development and want my changes to be available, I mount the current directory inside the container as a volume:

docker-compose.yml:

my-app:
    volumes:
        - ./:/var/www/myapp/

This loads an outdated vendor directory into my container; forcing me to rerun composer install either on the host or inside the container in order to have the up to date version.

I wonder how I could manage my docker-compose stack differently, so that the changes during the docker build on the current folder are also persisted on the host directory and I don't have to run the command twice.

I do want to keep the vendor folder mounted, as some vendors are my own and I like being able to modifiy them in my current project. So only mounting the folders I need to run my application would not be the best solution.

I am looking for a way to tell docker-compose: Write all the stuff inside the container back to the host before adding the volume.

Upvotes: 0

Views: 1329

Answers (2)

dnephin
dnephin

Reputation: 28080

Write all the stuff inside the container back to the host before adding the volume.

There isn't any way to do this directly, but there are a few options to do it as a second command.

  • as already suggested you can run a container and copy or rsync the files
  • use docker cp to copy the files out of a container (without using a volume)
  • use a tool like dobi (disclaimer: dobi is my own project) to automate these tasks. You can use one image to update vendor, and another image to run the application. That way updates are done on the host, but can be built into the final image. dobi takes care of skipping unnecessary operations when the artifact is still fresh (based on modified time of files or resources), so you never run unnecessary operations.

Upvotes: 1

BMitch
BMitch

Reputation: 263796

You can run a short side container after docker-compose build:

docker run --rm -v /vendor:/target my-app cp -a vendor/. /target/.

The cp could also be something more efficient like an rsync. Then after that container exits, you do your docker-compose up which mounts /vendor from the host.

Upvotes: 2

Related Questions