Docker: RUN touch does not create a file

While trying to debug RUN statements in my Docker file, I tried to redirect the output to a file in the linked volume ( ./mongo/log ).

To my surprise, I was unable to create files using the RUN command or to transfer the output of another command to a file using the redirect / add operators ( > , >> ). However, I was able to complete the specified task by logging into the running container via docker exec -ti mycontainer /bin/sh and issuing the command from there.

Why is this behavior happening? How can I touch the file in the Dockerfile / redirect file to the file or to the console from which the Docker file is launched?

Here is my Docker file:

 FROM mongo:3.4 #Installing NodeJS RUN apt-get update && \ apt-get install -y curl && \ curl -sL https://deb.nodesource.com/setup_6.x | bash - && \ apt-get install -y nodejs #Setting Up Mongo WORKDIR /var/www/smq COPY ./mongo-setup.js mongo-setup.js ##for testing RUN touch /var/log/node.log && / node --help 2>&1 > /var/log/node.log ##this was the command to debug #RUN node mongo-setup.js > /var/log/mongo-setup.log 2> /var/log/mongo-setup.error.log 

Here is an excerpt from my docker-compose.yml:

 mongodb: build: context: ./ dockerfile: ./mongodb-dockerfile container_name: smqmongodb volumes: - /var/lib/mongodb/data - ./mongo/log/:/var/log/ - ../.config:/var/www/.config 
+6
source share
2 answers

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

Docker recommends using docker logs . For instance:

 docker logs container-name 

To accomplish what you need (see the mongo configuration logs?), You can separate the stdout and stderr of the container by connecting 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, see the documentation for docker logs.

+4
source

You do this during 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 invariably in the resulting image.

Then you start the container with this mount volume:

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

Everything that is in ./mongo/log/ is mounted as /var/log in the container, which hides everything that was before (from the image). This is what makes it look like your touch didn't work (although it probably worked fine).

You think about it backwards - your mount of the volume does not display the version of the /var/log container from the outside - it replaces everything that was there.

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

+10
source

Source: https://habr.com/ru/post/1013672/


All Articles