dockerdocker-composedocker-volume

How do I mount a host directory as a volume in docker compose


I have a development environment I'm dockerizing and I would like the ability to livereload my changes without having to rebuild docker images. I'm using docker compose because redis is one of my app's dependencies and I like being able to link a redis container

I have two containers defined in my docker-compose.yml:

node:
  build: ./node
  links:
    - redis
  ports:
    - "8080"
  env_file:
    - node-app.env

redis:
  image: redis
  ports:
    - "6379"

I've gotten to the point in my node app's dockerfile where I add a volume, but how do I mount the the host's directory in the volume so that all my live edits to the code are reflected in the container?

Here's my current Dockerfile:

# Set the base image to Ubuntu
FROM    node:boron

# File Author / Maintainer
MAINTAINER Amin Shah Gilani <amin@gilani.me>

# Install nodemon
RUN npm install -g nodemon

# Add a /app volume
VOLUME ["/app"]

# TODO: link the current . to /app

# Define working directory
WORKDIR /app

# Run npm install
RUN npm install

# Expose port
EXPOSE  8080

# Run app using nodemon
CMD ["nodemon", "/app/app.js"]

My project looks like this:

/
- docker-compose.yml
- node-app.env
- node/
  - app.js
  - Dockerfile.js

Solution

  • Checkout their documentation

    From the looks of it you could do the following on your docker-compose.yml

    volumes:
        - ./:/app
    

    Where ./ is the host directory, and /app is the target directory for the containers.


    EDIT:
    Previous documentation source now leads to version history, you'll have to select the version of compose you're using and look for the reference.

    For the lazy – v3 / v2 / v1

    Side note: Syntax remains the same for all versions as of this edit