How to import data into my local atlas setup

Hi there,
I have following the official tutorial here to setup my local atlas deployment:

I can successfully connect to the DB via Compass but my data is not imported correctly. Here is my docker-compose.yaml:

services:
  mongodb:
    hostname: mongodb
    image: mongodb/mongodb-atlas-local
    environment:
      - MONGODB_INITDB_ROOT_USERNAME=${MONGODB_INITDB_ROOT_USERNAME}
      - MONGODB_INITDB_ROOT_PASSWORD=${MONGODB_INITDB_ROOT_PASSWORD}
    ports:
      - 27018:27017
    volumes:
      - ${PWD}/data/users.json:/tmp/users.json
      - ./init:/docker-entrypoint-initdb.d

The users.json seems to be correctly imported into the /temp folder. However, the import fails. Here is my init/00-import-users.sh:

#!/bin/bash

# Import the test data.
mongoimport --uri "mongodb://user:pass@localhost:27018/?directConnection=true" --collection=users --db=mydb --file=/tmp/users.json || echo "Failed to import user data." > /tmp/import-error-users.txt

Any idea on what might be going wrong?

Hi @Leon_D you can just use set -e in your script so exit 1 will happen on an error (which will avoid needing the echo part).

Can you add RUNNER_LOG_FILE=/dev/stderr to the environment section of your compose file (example shown half way down the tutorial you linked) and share the output back here, please? Hopefully this should give us a bit more insight.

Thank you for your reply.
This is returned as a log:

Error: error initializing: error seeding: error seeding file "/docker-entrypoint-initdb.d/00-import-users.sh": exit status 1

Usage:

  runner server [flags]


Flags:

  -h, --help   help for server


panic: error initializing: error seeding: error seeding file "/docker-entrypoint-initdb.d/00-import-users.sh": exit status 1


goroutine 1 [running]:

main.main()

	/app/cmd/runner/main.go:65 +0x174

Hello, i’m not on the mongo team but am very good at docker/mongo and i’m pretty sure i know what is going on (like 99% confident):

Your mongodb container is starting up, trying to run your import script, but the script is failing because it’s trying to connect to the wrong address with likely incorrect credentials. (the main thing is the wrong address)

The script is running inside the container but is trying to connect as if it were running from outside. The error is fatal enough that it’s causing the entire initialization process to abort, which is why your data isn’t being imported even though the container itself starts.

to fix it do this:

# like the mongo team said 
set -e

# Import the test data using the correct internal hostname and credentials
mongoimport --uri "mongodb://${MONGODB_INITDB_ROOT_USERNAME}:${MONGODB_INITDB_ROOT_PASSWORD}@mongodb:27017/?directConnection=true" \
  --collection=users \
  --db=mydb \
  --file=/tmp/users.json

the main bit being:

  • Using “mongodb:27017” as the connection string (using the service name defined in your docker-compose)
  • Using port 27017 (internal container port) instead of 27018 (host mapped port)
2 Likes

Thank you for your answer.
Unfortunately, it still does not seed the db.

I wrote the log to a file and it gives me:

2025-05-22T05:13:38.469+0000	error connecting to host: failed to connect to mongodb://user:pass@mongodb:27018/?directConnection=true: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: mongodb:27018, Type: Unknown, Last error: dial tcp xxxx:27018: connect: connection refused }, ] }
[ERROR] mongoimport failed

With port 27017 I get the following:

2025-05-22T05:24:12.294+0000	error connecting to host: failed to connect to mongodb://user:pass@mongodb:27017/?directConnection=true: connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
[ERROR] mongoimport failed

@Leon_D I think we might need to understand a bit more about your setup. I’ll drop you a DM with an invite to jump on a quick call to see if we can get to the bottom on this one.

this is dope. if its ok please share an update. always down to learn more

1 Like

@Leon_D can you try updating your import users script to:

set -e

mongoimport --uri "${CONNECTION_STRING}" \
  --collection=users \
  --db=mydb \
  --file=/tmp/users.json

And let me know how you get on, please?

Hi @Jonny_Roberts,
Thank you for the input.
Although I definitely tried the "${CONNECTION_STRING}" option before, it now worked, I was probably missing something else. Here is the full script I used:

#!/bin/bash

LOG_FILE="/tmp/mongoimport_error.log"

# Import the test data.
mongoimport --uri "${CONNECTION_STRING}" \
    --collection=users \
    --db=mydb \
    --file=/tmp/users.json \
    --jsonArray \
    2>> "${LOG_FILE}"

if [ $? -ne 0 ]; then
  echo "[ERROR] mongoimport failed" >> "$LOG_FILE"
fi

Thank you so much for resolving this.
Best,
Leon

1 Like