Docker: RUN touch doesn't create file

Answer #1 100 %

You are doing this during your build:

RUN touch /var/log/node.log && /
    node --help 2>&1 > /var/log/node.log

The file /var/log/node.log is created and fixed immutably into the resulting image.

Then you run the container with this volume mount:

  - ./mongo/log/:/var/log/

Whatever is in ./mongo/log/ is mounted as /var/log in the container, which hides whatever was there before (from the image). This is the thing that's making it look like your touch didn't work (even though it probably worked fine).

You're thinking about this backward - your volume mount doesn't expose the container's version of /var/log externally - it replaces whatever was there.

Nothing you do in Dockerfile (build) will ever show up in an external mount.

Answer #2 100 %

Instead of RUN node mongo-setup.js > /var/log/mongo-setup.log 2> /var/log/mongo-setup.error.log, within the container, what if you just say `RUN node mongo-setup.js'?

Docker recommends using docker logs. Like so:

docker logs container-name

To accomplish what you're after (see the mongo setup logs?), you can split the stdout & stderr of the container by piping the separate streams: and send them to files:

me@host~$ docker logs foo > stdout.log 2>stderr.log

me@host~$ cat stdout.log
me@host~$ cat stderr.log

Also, refer to the docker logs documentation

You’ll also like:

© 2023 -