Mounting a Volume Using Compose and Sharing Images

Learn how to mount a volume using Compose and how to share images.

Mounting a volume

Before we leave our docker-compose.yml, let’s make a small addition. The -v option allows us to mount a local directory inside a container with docker run. By using this, the Rails project files generated inside the container would be available on our local machine. A mounted local volume represents some filesystem that is shared between your local machine and the container. Files in the mounted volume have synced both ways between your local filesystem and the container.

Because of this, a local volume mount can allow us to develop locally and have the Rails server running in the container automatically pick up the file changes without restarting, just like we are used to. We are going to set up this locally mounted volume using Compose rather than docker run this time. In our docker run command, we used the option -v $PWD:/usr/src/app to mount our current, local directory inside the Rails container at /usr/src/app.

Editing docker-compose.yml

We can achieve the same with Compose by adding the following to our docker-compose.yml:

Get hands-on with 1200+ tech skills courses.