human
human

Reputation: 736

Creating a file on a mounted volume while building a docker container

I am trying to create a file while building a docker container, when I build it I don't get any error and it seems like the file was written correctly, however when I ssh into the container the file is not there.

Any ideas?

Upvotes: 4

Views: 3686

Answers (2)

Nikhil Chhabria
Nikhil Chhabria

Reputation: 107

You should run the command in docker-compose.yml file - version: '2'

services:
web:
    build: web/
    ports:
        - "8080:80"
    volumes:
        - ./web/:/var/www/
    env_file:
        - web/.env
    command:"echo "Some line to add to a file" >> /var/www/public/text.txt "

Since you are mounting the volumes after building the image.

Upvotes: 0

BMitch
BMitch

Reputation: 263726

Volume mounts apply at run time, not during a build. The output of a build is a portable image, not an image and some persistent data you would need to ship separately. That file should exist inside your image, depending on the rest of your Dockerfile.

When you mount a host volume at run time, anything inside the built image is hidden by the host mount. You only see files (along with permissions, uid, and gid) as they exist in the host.

However, if you switch to named volumes, docker will initialize that volume when it's new/empty with the contents of the image at that location.

Upvotes: 3

Related Questions