I'm trying to set up a container running MongoDB that gets populated with data using mongorestore when it starts up. The idea is to quickly set up a dummy database for testing and mocking.
My Dockerfile looks like this:
FROM mongo:bionic
COPY ./db-dump/mydatabase/* /db-dump/
and docker-compose.yml looks like this:
version: "3.1"
services:
mongo:
build: ./mongo
command: mongorestore -d mydatabase ./db-dump
ports:
- "27017:27017"
If I run this with docker-compose up
, it pauses for a while and then I get an error saying:
error connecting to host: could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: localhost:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
Opening a CLI on the container and running the exact same command works without any issues, however. I've tried adding -h
with the name of the container or 127.0.0.1, and it doesn't make a difference. Why isn't this command able to connect when it works fine once the container is running?