Help with mounting volumes

Hi,

I have an application I have written inside of a container that runs and works all fine.

However obviously if that container is deleted or newly created again then I would lose that data as all of the data is uses is stored within a json file inside of the container.

My question is, how would I go about mounting a folder from inside the container on the host machine so that I can link it in to any new containers that I run?

I know this is a bit of a nube question but would be a huge help if someone could explain.

Hi,

In Docker there are two ways you can mount volumes to the container.

  • The first method is to directly use any of the host directory to map to a directory inside container.
  • The second method is using a data volume container. In this method the host directory is mounted to a container and we refer to this data volume container as the volume source to mount to our application container.

Just to give you a basic idea, consider the following scenario.

  • /opt/host/config is the directory on host
  • /opt/container/config is the directory inside the container where your app writes the json configuration
  • Lets say you are using centos:6 image

Now to mount the host directory to container directory you can run the container as shown below.

docker run -it --rm -v /opt/host/config:/opt/container/config centos:6

Now you will be able to access the host directory at /opt/container/config directory.

Just a point to note is , if your app modifies the json file at runtime, you should not be mounting the same directory to multiple containers as each one modifying the config may result in issues, and this is purely dependent on the design of your app.

You should read this page to understand better. https://docs.docker.com/userguide/dockervolumes/