2

Why my docker data volume is empty mounted on the host system?

When the docker-compose up is done I can exec into the docker container and look at the node_modules directory, there I can see all the modules installed successfully. But when I check my node_modules directory on my host machine I see nothing, its empty.

Here is the Dockerfile:

FROM ubuntu:14.04

WORKDIR /var/www/html

COPY src/package.json /var/www/html/package.json

# install curl, apache, php
RUN DEBIAN_FRONTEND=noninteractive \
apt-get -y update && \
apt-get -y install software-properties-common python-software-properties && \
add-apt-repository ppa:ondrej/php && \
apt-get -y update && \
apt-get install -y --force-yes \
curl \
apache2 \
php5.6 php5.6-mcrypt php5.6-mbstring php5.6-curl php5.6-cli php5.6-mysql php5.6-gd php5.6-intl php5.6-xsl

# install PHPUnit
RUN curl -L https://phar.phpunit.de/phpunit.phar -o phpunit.phar && \
chmod +x phpunit.phar && \
mv phpunit.phar /usr/local/bin/phpunit

# install node js 6
RUN NVM_DIR="/root/.nvm" && \
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash && \
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && \
    nvm install 6 && \
    npm install -g webpack && \
    npm install

COPY src/ /var/www/html

EXPOSE 80

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

and here is my docker-compose.yml file:

version: "2"
services:
  db:
    image: mysql:5.7
    volumes:
      - "./.data/db:/var/lib/mysql"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: abc
      MYSQL_DATABASE: abc
      MYSQL_USER: abc
      MYSQL_PASSWORD: abc

  wordpress:
    build: .
    restart: always
    depends_on:
      - db
    links:
      - db
    ports:
      - "8000:80"
    volumes:
      - ./src:/var/www/html
      - /var/www/html/node_modules

1 Answer 1

4

You're not mounting the node_modules volume, it's just a straight data volume. That means the data is stored on your host, but it will be in a folder buried in the Docker engine's program store.

In the Compose file:

volumes:
  - ./src:/var/www/html
  - /var/www/html/node_modules

The first volume is mounted, so the container uses ./src on the host. The second volume isn't mounted, so it will be in /var/lib/docker on the host.

It's the same with docker -v - this does the equivalent of your Compose file, and the inspect shows you where the volumes are mounted on the host:

> mkdir ~/host
> docker run -d -v ~/host:/vol-mounted -v /vol-not-mounted busybox
89783d441a74a3194ce9b0d57fa632408af0c5981474ec500fb109a766d05370
> docker inspect --format '{{json .Mounts}}' 89
[{
    "Source": "/home/scrapbook/host",
    "Destination": "/vol-mounted",
    "Mode": "",
    "RW": true,
    "Propagation": "rprivate"
}, {
    "Name": "55e2f1dd9e490f1e3ce53a859075a20462aa540cd72fac7b4fbe6cd26e337644",
    "Source": "/var/lib/docker/volumes/55e2f1dd9e490f1e3ce53a859075a20462aa540cd72fac7b4fbe6cd26e337644/_data",
    "Destination": "/vol-not-mounted",
    "Driver": "local",
    "Mode": "",
    "RW": true,
    "Propagation": ""
}]
Sign up to request clarification or add additional context in comments.

1 Comment

I've been successfully using this trick for a while now, but suddenly it stopped working. Now I keep getting an empty folder on my local system which - apparently - in its turn discards the content of the one inside the container. So I need to restart, run npm install again, until it resets.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.