Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

416
Views
Docker-Compose: how to mapping volume from host into container?

The Problem:

I'd like to share a folder from the docker host with a specific container using docker-compose. A container running MongoDB shall place its data files into /var/lib/mongodb, but this folder shall be a mount to the hosts folder /var/lib/mongodb.

But it still remains an own local directory within the container, not the shared one from the host.

Sometimes it also ends up with the following warning:

WARNING: Service "mongodb" is using volume "/var/lib/mongodb" from the previous container. Host mapping "name_dbvolume" has no effect. Remove the existing containers (with docker-compose rm mongodb) to use the host volume mapping.

docker-compose.yml

version: '2'
services:
  loopback:
    build: .
    expose:
      - "80"
      - "443"
      - "28015"
      - "29015"
    ports:
      - 3000:3000
    links:
      - mongodb
    environment:
      VIRTUAL_HOST: api.xxxxxx.com
      VIRTUAL_PORT: 3000
      LETSENCRYPT_EMAIL: yyyyyy@xxxxxxx.com
      LETSENCRYPT_HOST: api.xxxxx.com
  mongodb:
    build: ./MongoDB
    ports:
      - "27017:27017"
      - "27018:27018"
      - "27019:27019"
      - "28017:28017"
    volumes:
        - dbvolume:/var/lib/mongodb
volumes:
    dbvolume:

Dockerfile for MongoDB-Container:

FROM mongo:latest

RUN  mkdir -p /var/lib/mongodb && \
     touch /var/lib/mongodb/.keep && \
     chown -R mongodb:mongodb /var/lib/mongodb


ADD mongodb.conf /etc/mongodb.conf

VOLUME [ "/var/lib/mongodb" ]

# EXPOSE 27017

USER mongodb
WORKDIR /var/lib/mongodb

ENTRYPOINT ["/usr/bin/mongod", "--config", "/etc/mongodb.conf"]
CMD ["--quiet"]

The mongodb-container is configured that MongoDB uses /var/lib/mongodb as data directory. With the following command we try to use this folder from the host:

VOLUME [ "/var/lib/mongodb" ]

What did i wrong here?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your compose config sets up a named data volume called dbvolume

A named volume will be stored in /var/lib/docker by default:

→ docker volume inspect dbvolume
[
    {
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/dbvolume/_data",
        "Name": "dbvolume",
        "Options": {},
        "Scope": "local"
    }
]

Mounting a host directory as a volume is slightly different in a compose file.

    volumes:
      - /var/lib/mongodb:/var/lib/mongodb

The source is still on the left, but you specify the full path instead of a name.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!