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.
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: [email protected]
LETSENCRYPT_HOST: api.xxxxx.com
mongodb:
build: ./MongoDB
ports:
- "27017:27017"
- "27018:27018"
- "27019:27019"
- "28017:28017"
volumes:
- dbvolume:/var/lib/mongodb
volumes:
dbvolume:
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With