docker-compose
Learn how to set up MongoDB and MongoDB Express using Docker Compose. This guide provides a simple docker-compose.yml file for easy deployment.
Docker Compose for MongoDB
Introduction to Docker Compose for MongoDB
Docker Compose is a powerful tool for defining and running multi-container Docker applications. This guide demonstrates how to use a docker-compose.yml
file to easily set up and manage a MongoDB instance along with MongoDB Express, a web-based GUI for MongoDB.
Docker Compose Configuration for MongoDB
Below is a sample docker-compose.yml
file that configures two services: a MongoDB database and MongoDB Express for easy management. This setup is ideal for development environments or quick deployments.
---
# https://hub.docker.com/_/mongo
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
Explanation of Services
MongoDB Service
The mongo
service uses the official MongoDB Docker image. It's configured to restart automatically and sets the root username and password for database access. This ensures your MongoDB instance is always available.
MongoDB Express Service
The mongo-express
service uses the MongoDB Express image, providing a web interface to interact with your MongoDB database. It maps port 8081 on your host to the container's port 8081, allowing you to access it via your browser. It's configured to use the same root credentials as the MongoDB service and connects to the MongoDB instance using the service name mongo
.
Running the Docker Compose Setup
To deploy this setup, save the content above as docker-compose.yml
in a directory. Then, navigate to that directory in your terminal and run the command: docker compose up -d
. You can then access MongoDB Express at http://localhost:8081
.