0

docker-compose.yml file:

web:
  build: ./code
  ports:
    - "80:80"
  volumes:
    - ./mount:/var/www/html

dockerfile in ./code:

FROM wordpress
WORKDIR /var/www/html
RUN touch test.txt

This is a production environment I'm using to set up a simple WordPress blog (omitted other services in docker-compose.yml & Dockerfile for simplicity).

Here's what I'm doing:

  • Bind mounting host directory at container destination /var/www/html
  • Create test.txt file during build time

What's NOT working:

  • When I inspect /www/var/html on the container, I don't find my test.txt file

What I DO understand:

  • Bind mounting happens at run-time
  • In this particular case file gets created, but the when you mount the host directory, commands in Dockerfile get overridden
  • When you use volume mount, It works

What I DON'T understand:

  • What are the ways in which you can get your latest code into the container which is using a bind mount to persist data?
  • How can one create a script that can let me achieve this in runtime?
  • How else can I achieve this considering I HAVE to use a bind mount (AWS ECS persists data only when you use a host directory path for a volume)
Anand Naik B
  • 689
  • 1
  • 7
  • 20

1 Answers1

0

Your data will be persisted at runtime. Everything stored in /var/www/html at runtime will be persisted in the host ./mount directory.

At build time, everything happens at docker layer inside the container image.

If you want to do things before anything you could create a script, ADDit to your image and use CMD or ENTRYPOINT to run the script when the container starts.

In summary

  • What are the ways in which you can get your latest code into the container which is using a bind mount to persist data?

You add your latest code to the image (i.e. git clone, COPY, ADD, or what suits you). A container shouldn't be mutable, so you keep your code versioned and you define persist folder (e.g. for uploads)

  • How can one create a script that can let me achieve this in runtime?

If you want to do it at runtime, you add your shell script to the image and then you run it. Although, this is not the best approach for this use case.

  • How else can I achieve this considering I HAVE to use a bind mount (AWS ECS persists data only when you use a host directory path for a volume)

IMHO, you should use your images as your build of your code. Your image should not be mutable and must reflect an instance in your code lifecycle. Define paths with data and those paths would be your mounts at host level.

Sebastian
  • 4,770
  • 4
  • 42
  • 43
  • So why isn't 'test.txt' persisted in this particular case? – Anand Naik B May 29 '18 at 12:11
  • You could check [this similar question](https://stackoverflow.com/questions/26050899/how-to-mount-host-volumes-into-docker-containers-in-dockerfile-during-build) and its answers. – Sebastian May 29 '18 at 12:16