how to import docker mongo data to local mongodb
package main
import (
"context"
"fmt"
"log"
"os/exec"
)
func main() {
// Step 1: Run a Docker container with MongoDB, and import data into it
// Command to run a MongoDB Docker container
cmdRunContainer := exec.Command("docker", "run", "--name", "mongo_import", "-d", "-p", "27017:27017", "-v", "$(pwd)/data:/data/db", "mongo")
// Execute the command to start the MongoDB container
if err := cmdRunContainer.Run(); err != nil {
log.Fatalf("Error running MongoDB container: %s", err)
}
fmt.Println("MongoDB Docker container is running")
// Command to import data from a MongoDB dump file (replace '/path/to/dump' with your dump file path)
cmdImportData := exec.Command("docker", "exec", "-i", "mongo_import", "mongorestore", "--archive", "--gzip", "--db", "your_database_name", "--drop", "/path/to/dump")
// Execute the command to import data into the MongoDB container
if err := cmdImportData.Run(); err != nil {
log.Fatalf("Error importing data to MongoDB: %s", err)
}
fmt.Println("Data imported to MongoDB successfully")
// Step 2: Clean up - stop and remove the Docker container
cmdStopContainer := exec.Command("docker", "stop", "mongo_import")
if err := cmdStopContainer.Run(); err != nil {
log.Fatalf("Error stopping MongoDB container: %s", err)
}
fmt.Println("MongoDB Docker container stopped")
cmdRemoveContainer := exec.Command("docker", "rm", "mongo_import")
if err := cmdRemoveContainer.Run(); err != nil {
log.Fatalf("Error removing MongoDB container: %s", err)
}
fmt.Println("MongoDB Docker container removed")
}
Explanation of steps:
1. Import the required packages for handling system commands, logging, and formatting output.
2. The main()
function starts.
3. Step 1: Run a Docker container with MongoDB and import data into it.
- cmdRunContainer
: Creates a command to run a MongoDB Docker container using the docker run
command.
- docker run --name mongo_import -d -p 27017:27017 -v $(pwd)/data:/data/db mongo
: This command creates a Docker container named mongo_import
running MongoDB in detached mode (-d
), mapping the host's port 27017
to the container's port 27017
, and linking a local directory ($(pwd)/data
) to the container's data directory (/data/db
).
- Executes the command to start the MongoDB container. If an error occurs, it logs the error and exits.
- Prints a message confirming that the MongoDB Docker container is running.
- cmdImportData
: Creates a command to import data into the MongoDB container using docker exec
.
- docker exec -i mongo_import mongorestore --archive --gzip --db your_database_name --drop /path/to/dump
: This command restores data from a MongoDB dump file (/path/to/dump
) into a specified database (your_database_name
) within the running MongoDB container. It uses --archive
and --gzip
options to read a compressed archive file.
- Executes the command to import data into the MongoDB container. If an error occurs, it logs the error and exits.
- Prints a message confirming successful data import.
4. Step 2: Clean up - stop and remove the Docker container.
- cmdStopContainer
: Creates a command to stop the MongoDB container (mongo_import
) using docker stop
.
- Executes the command to stop the MongoDB container. If an error occurs, it logs the error and exits.
- Prints a message confirming that the MongoDB Docker container has stopped.
- cmdRemoveContainer
: Creates a command to remove the stopped MongoDB container (mongo_import
) using docker rm
.
- Executes the command to remove the MongoDB container. If an error occurs, it logs the error and exits.
- Prints a message confirming that the MongoDB Docker container has been removed.