Understanding Docker volumes

Hi folks I hope you are doing well,

I’m trying to be sure if all I’m doing is fine with my deployment stack with Docker as I changed lately.

My image relies on mongo docker image, which has a persistence volume host-db/mongo/ (host) mapped to data/db (mongo image).

The thing now is with files in my app image and in host, I find nearly the same files in both, normally I would find nothing inside my app image !!

I’m listing here content of these folders in three places: host, mongo image and my app image:

Host

total 280
-rw------- 1 lxd docker 50 Jul 15 14:20 WiredTiger
-rw------- 1 lxd docker 21 Jul 15 14:20 WiredTiger.lock
-rw------- 1 lxd docker 1455 Jul 16 07:14 WiredTiger.turtle
-rw------- 1 lxd docker 61440 Jul 16 07:14 WiredTiger.wt
-rw------- 1 lxd docker 4096 Jul 15 14:54 WiredTigerHS.wt
-rw------- 1 lxd docker 36864 Jul 16 05:09 _mdb_catalog.wt
-rw------- 1 lxd docker 20480 Jul 15 14:54 collection-0-7700572892100174683.wt
-rw------- 1 lxd docker 12288 Jul 16 06:55 collection-10-9148746666568851397.wt
-rw------- 1 lxd docker 20480 Jul 16 05:05 collection-8-9148746666568851397.wt
drwx------ 2 lxd docker 4096 Jul 16 07:15 diagnostic.data
-rw------- 1 lxd docker 20480 Jul 15 14:54 index-1-7700572892100174683.wt
-rw------- 1 lxd docker 12288 Jul 16 06:55 index-11-9148746666568851397.wt
-rw------- 1 lxd docker 12288 Jul 16 07:14 index-12-9148746666568851397.wt
-rw------- 1 lxd docker 20480 Jul 16 05:05 index-9-9148746666568851397.wt
drwx------ 2 lxd docker 4096 Jul 15 14:55 journal
-rw------- 1 lxd docker 2 Jul 15 14:54 mongod.lock
-rw------- 1 lxd docker 36864 Jul 16 06:55 sizeStorer.wt
-rw------- 1 lxd docker 114 Jul 15 14:20 storage.bson

MyApp
total 1716
-rw------- 1 root root 50 Jul 15 14:20 WiredTiger
-rw------- 1 root root 21 Jul 15 14:20 WiredTiger.lock
-rw------- 1 root root 1447 Jul 15 14:49 WiredTiger.turtle
-rw------- 1 root root 135168 Jul 15 14:49 WiredTiger.wt
-rw------- 1 root root 4096 Jul 15 14:48 WiredTigerHS.wt
-rw------- 1 root root 36864 Jul 15 14:48 _mdb_catalog.wt
-rw------- 1 root root 20480 Jul 15 14:48 collection-0-7700572892100174683.wt
-rw------- 1 root root 454656 Jul 15 14:21 collection-11-7700572892100174683.wt
-rw------- 1 root root 4096 Jul 15 14:20 collection-13-7700572892100174683.wt
-rw------- 1 root root 4096 Jul 15 14:20 collection-15-7700572892100174683.wt
-rw------- 1 root root 36864 Jul 15 14:49 collection-2-7700572892100174683.wt
-rw------- 1 root root 20480 Jul 15 14:25 collection-4-7700572892100174683.wt
-rw------- 1 root root 20480 Jul 15 14:21 collection-7-7700572892100174683.wt
-rw------- 1 root root 4096 Jul 15 14:20 collection-9-7700572892100174683.wt
drwx------ 2 root root 4096 Jul 15 14:51 diagnostic.data
-rw------- 1 root root 647168 Jul 15 14:20 index-0-4356988171584090514.wt
-rw------- 1 root root 20480 Jul 15 14:48 index-1-7700572892100174683.wt
-rw------- 1 root root 4096 Jul 15 14:49 index-10-4356988171584090514.wt
-rw------- 1 root root 4096 Jul 15 14:20 index-10-7700572892100174683.wt
-rw------- 1 root root 24576 Jul 15 14:21 index-12-7700572892100174683.wt
-rw------- 1 root root 4096 Jul 15 14:20 index-14-7700572892100174683.wt
-rw------- 1 root root 4096 Jul 15 14:20 index-16-7700572892100174683.wt
-rw------- 1 root root 57344 Jul 15 14:20 index-2-4356988171584090514.wt
-rw------- 1 root root 36864 Jul 15 14:49 index-3-7700572892100174683.wt
-rw------- 1 root root 24576 Jul 15 14:20 index-4-4356988171584090514.wt
-rw------- 1 root root 20480 Jul 15 14:25 index-5-7700572892100174683.wt
-rw------- 1 root root 36864 Jul 15 14:20 index-6-4356988171584090514.wt
-rw------- 1 root root 20480 Jul 15 14:49 index-6-7700572892100174683.wt
-rw------- 1 root root 4096 Jul 15 14:20 index-8-4356988171584090514.wt
-rw------- 1 root root 20480 Jul 15 14:21 index-8-7700572892100174683.wt
-rw------- 1 root root 20480 Jul 15 14:21 index-9-4356988171584090514.wt
drwx------ 2 root root 4096 Jul 15 14:51 journal
-rw------- 1 root root 2 Jul 15 14:48 mongod.lock
-rw------- 1 root root 36864 Jul 15 14:48 sizeStorer.wt
-rw------- 1 root root 114 Jul 15 14:20 storage.bson

Mongo

-rw------- 1 mongodb mongodb 50 Jul 15 14:20 WiredTiger
-rw------- 1 mongodb mongodb 21 Jul 15 14:20 WiredTiger.lock
-rw------- 1 mongodb mongodb 1455 Jul 16 07:13 WiredTiger.turtle
-rw------- 1 mongodb mongodb 61440 Jul 16 07:13 WiredTiger.wt
-rw------- 1 mongodb mongodb 4096 Jul 15 14:54 WiredTigerHS.wt
-rw------- 1 mongodb mongodb 36864 Jul 16 05:09 _mdb_catalog.wt
-rw------- 1 mongodb mongodb 20480 Jul 15 14:54 collection-0-7700572892100174683.wt
-rw------- 1 mongodb mongodb 12288 Jul 16 06:55 collection-10-9148746666568851397.wt
-rw------- 1 mongodb mongodb 20480 Jul 16 05:05 collection-8-9148746666568851397.wt
drwx------ 2 mongodb mongodb 4096 Jul 16 07:14 diagnostic.data
-rw------- 1 mongodb mongodb 20480 Jul 15 14:54 index-1-7700572892100174683.wt
-rw------- 1 mongodb mongodb 12288 Jul 16 06:55 index-11-9148746666568851397.wt
-rw------- 1 mongodb mongodb 12288 Jul 16 07:13 index-12-9148746666568851397.wt
-rw------- 1 mongodb mongodb 20480 Jul 16 05:05 index-9-9148746666568851397.wt
drwx------ 2 mongodb mongodb 4096 Jul 15 14:55 journal
-rw------- 1 mongodb mongodb 2 Jul 15 14:54 mongod.lock
-rw------- 1 mongodb mongodb 36864 Jul 16 06:55 sizeStorer.wt
-rw------- 1 mongodb mongodb 114 Jul 15 14:20 storage.bson

To finish with this is my understanding:
MyApp/host-db/mongo should contain no files (I thought Docker would deal with that without adding to .dockerignore. If not, should I add

host-db/mongo/
host-db/mongo/*

to my .dockerignore ?

Thanks a lot !!

I am not realy sure what to make of your post… Without knowing the exact(!) commands or compose file used to start your container, it’s impossible to see what actualy happens.

Regarding the .dockerignore: docker build uses it to know which files to skip when copying files/folders from the build dir into the build context.

Thanks @meyay
I thought lets mention mapped volumes as I don’t deal with these files otherwise (in my build scripts). But this is my docker-compose.yml

version: '3.7'
services:
  classified-ads:
    container_name: classified-ads
    depends_on:
      - mongodb-service
      - redis-service
      - caddy-service
    ports:
      - 3000:3000
    build:
      context: ./
    # restart: unless-stopped
    environment:

  mongo:
      container_name: mongodb-service
      image: mongo
      volumes:
        - ./host-db/mongo-data:/data/db
        - ./bootstrap/init-db.js:/docker-entrypoint-initdb.d/init-db.js:ro
      ports:
        - '27017:27017'
      restart: unless-stopped

This is Dockerfile

FROM node:14.19.3-alpine3.14
WORKDIR /classified-ads
COPY package.json ./
RUN npm i -g npm
RUN rm -rf /node_modules

RUN apk add --no-cache --update --virtual .gyp \
    build-base vips-dev python3 go && npm i @smodin/fast-text-language-detection annoy && \
    apk del .gyp

RUN npm i
COPY . ./

WORKDIR /classified-ads/client
#COPY /client/package.json ./
#RUN rm -rf /node_modules

RUN npm i
# RUN chmod -R a+rwx node_modules
RUN npm run build

WORKDIR /classified-ads
# EXPOSE 3000
CMD [ "npm", "run", "start" ]

As I mentioned I do COPY . ./ but I thought host volume is not copied even if not specified in .dockerignore
Thanks a lot

Your build dir is the folder where the compose file is:

build:
  context: ./

As a result all files will be copied into the (private) build context. Whatever is in there can be added to the image using COPY/ADD (the first is prefered!).

Without a .dockerignore file, your COPY . ./ instruction will copy the the whole content of the build dir into the build context and from there to the image. Since your bind volumes are subdirectories of the build dir, of course those files will be copied over as well.

Thus, you will want to create a .dockerignore file that prevents unwanted files/folders to be copied into the build-context, so they can not be copied into the image.

Btw: those are not docker volumes, in the sense of volumes. Those are binds . volumes can be listed using docker volume ls and your binds won’t be listed there… With binds, the host folder mapepd to a container folder will eclipse the content of the container folder.

1 Like

@meyay Thanks a lot for the insights, and also about volumes which are binds. I didn’t know about that.

Have a great day

Welcome!

Thanks, have a great weekend yourself!

1 Like