This docker-compose.yml file creates a MongoDB container with authentication and binds port 27017 to the host machine. It also creates a data volume for persisting the database files.
Docker and Docker Compose installed on your machine.
- Clone or download the repository to your local machine.
- Navigate to the directory containing the docker-compose.yml file.
- Run the following command to start the MongoDB container:
docker-compose up -d
This will start the container in the background and print the container ID.
- Verify that the container is running:
docker ps
This should display a list of running containers on your machine, including the MongoDB container.
- To stop the container, run:
docker-compose down
This will stop and remove the container.
The mongodb_container
service in the docker-compose.yml
file is configured with the following options:
image
: specifies the Docker image to use for the container. In this case, the latest version of the official MongoDB image.environment
: sets environment variables for the container. In this case, sets the MongoDB root user credentials.ports
: maps the container's port 27017 to the host machine's port 27017.volumes
: creates a data volume for persisting the MongoDB data.
The volumes section of the docker-compose.yml file creates a named volume mongodb_data_container for storing the MongoDB data. This allows the data to persist even if the container is removed or recreated.