MongoDB – Create & launch containers with Docker Compose
Preamble
In this tutorial I would like to show you how to build your local „server environment“ with docker-compose. Docker-compose offers the possibility to combine several containers in one file. The central configuration file is called docker-compose.yml.
MongoDB with Docker-Compose
From our last tutorial Docker Container with MongoDB we still have the example that how to start a MongoDB via docker. Now we want to realize the whole thing with docker-compose.
First we need a configuration file named docker-compose.yml. Here we will now enter our mongodb as an example. Just like in the previous tutorial we will start and stop a MondoDB.
The content of our docker-compose.yml file looks like this:
version: '3' services: mongo: image: mongo ports: - 27017:27017
This is all we need to start a MondoDB with docker-compose. Since we are using a finished image, it is possible that the image will be downloaded from the net the first time it is called.
The MongoDB is started with the following command:
docker-compose up -d
The parameter -d specifies that the container is started in detach mode. So in the background without blocking the console. It is also possible to start single services with docker-compose. The following command would also start the MongDB
docker-compose up -d mongo
The mongodb is stopped with the following command:
docker-compose stop
Here it is also possible to stop individual services/containers.
This was a small example how you can use docker-compose to start and stop single services. With docker-compose it is also possible to start and stop multiple services. How to do this will be described in another tutorial.