2

I want to write log files to the host file system, so it is persisted, even if the Docker container dies.

Do I need to mount a volume in my Docker yaml?

VOLUME /var/log/myApp

Then do I just reference the mount like this?

var stream = fs.createWriteStream(`/var/log/myApp/myLog.log`);
stream.write('Hello World!');

Then outside of my container, I can go to the /var/log/myApp/ directory and see my logs.

I am trying to find an example of this, but haven't seen anything.

1 Answer 1

7

When you're setting up your container, you just use the -v argument:

-v ./path/to/local/directory:/var/log/myApp

The first path is where the volume is available on the host system (the period at the beginning means it's relative to where you're running the docker command). The path on the right hand side is where it's available in the container.

Once more, in docker-compose:

    volumes:
        - "./path/to/local/directory:/var/log/myApp"

And yes, this will allow the data stored in the volume to be persistent.

3
  • 1
    +1 You can get different fail behavior too if desired with -v vs --mount (mutually exclusive options). -v will create the specified folder, if it does not exist, on the Docker host, while --mount will make the Docker command fail if the volume does not exist on the Docker host more info in Dockers docs.
    – thmsdnnr
    Jun 17, 2019 at 17:07
  • Thanks, going to try this and I'll come back and mark it as answer.
    – Matt Kuhns
    Jun 17, 2019 at 18:39
  • This answer is helpful. I also needed to add this to the Dockerfile: RUN mkdir /var/log/myApp
    – Matt Kuhns
    Jun 18, 2019 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.