Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
In this tutorial, you'll learn how to create a Docker container running MongoDB, a popular NoSQL database. We'll use the official MongoDB image provided by the Docker community.
Prerequisites:
Pull the official MongoDB image:
To pull the official MongoDB image, run the following command:
docker pull mongo
This will download the latest version of the MongoDB image to your system.
Create a new container with MongoDB:
To create a new Docker container running MongoDB, use the docker run
command. We'll map the container's port 27017 (the default MongoDB port) to the host's port 27017, so you can access the MongoDB server from your host machine:
docker run --name mongo-container -p 27017:27017 -d mongo
This command creates a new container named mongo-container
, maps the container's port 27017 to the host's port 27017, and runs the container in detached mode (-d
flag). The container will run the MongoDB server using the default configuration.
Verify the MongoDB container is running:
To check if the MongoDB container is running, use the docker ps
command:
docker ps
You should see your mongo-container
listed as running.
Connect to the MongoDB server:
You can connect to the MongoDB server running inside the container using a MongoDB client or driver. If you have the MongoDB command line client (mongo
) installed on your host machine, you can connect to the server using:
mongo --host localhost --port 27017
This command connects to the MongoDB server running at localhost:27017
, which is the mapped port of the mongo-container
.
Configure MongoDB authentication (Optional):
By default, the MongoDB server in the container runs without authentication. To enable authentication, you'll need to create an admin user and restart the container with authentication enabled.
First, connect to the MongoDB server as described in step 5.
Then, create the admin user by running the following commands inside the mongo
shell:
use admin db.createUser( { user: "admin", pwd: "your_secure_password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
Replace "your_secure_password"
with a secure password for the admin user.
Exit the mongo
shell by typing exit
.
Stop and remove the existing mongo-container
:
docker stop mongo-container docker rm mongo-container
Finally, create a new container with authentication enabled:
docker run --name secure-mongo-container -p 27017:27017 -d mongo --auth
This command creates a new container named secure-mongo-container
, maps the container's port 27017 to the host's port 27017, runs the container in detached mode, and starts the MongoDB server with authentication enabled.
To connect to the MongoDB server with authentication enabled, use the following command:
mongo --host localhost --port 27017 -u admin -p your_secure_password --authenticationDatabase admin
Replace your_secure_password
with the password you set for the admin user.
In this tutorial, we covered how to create a Docker container running MongoDB, access the server from your host machine, and configure MongoDB authentication. You can now use this containerized MongoDB server for development, testing, or deployment purposes.
Running MongoDB in Docker Container:
docker run -d -p 27017:27017 --name my-mongodb-container mongo:latest
How to Create a Docker Image with MongoDB:
FROM mongo:latest # Add custom configurations or scripts
Docker-Compose MongoDB Installation:
version: '3' services: mongodb: image: mongo:latest ports: - "27017:27017"
MongoDB Dockerfile Example:
FROM mongo:latest COPY my-mongod.conf /etc/mongod.conf
Setting Up MongoDB in a Docker Environment:
version: '3' services: mongodb: image: mongo:latest ports: - "27017:27017" environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: example
Configuring MongoDB in Docker-Compose:
version: '3' services: mongodb: image: mongo:latest ports: - "27017:27017" volumes: - ./mongo-data:/data/db
Docker MongoDB Image Customization:
FROM mongo:latest COPY my-scripts /scripts RUN chmod +x /scripts/setup.sh
Docker MongoDB Volume Mounts:
version: '3' services: mongodb: image: mongo:latest volumes: - ./mongo-data:/data/db
Custom MongoDB Image Creation in Docker:
FROM mongo:latest COPY my-scripts /scripts RUN chmod +x /scripts/setup.sh
Docker Swarm and MongoDB Service:
version: '3' services: mongodb: image: mongo:latest deploy: replicas: 3 # Other Swarm deployment configurations
Troubleshooting MongoDB in Docker:
docker logs <mongodb_container_id_or_name>