Docker - Create MongoDB Container with Docker

In this tutorial I want to show you how to start and stop MongoDB under Docker Container. This tutorial can also be used for MySQL and MariaDB.

Docker – Create MongoDB Container with Docker


As a developer you are confronted with the question if you install software or not. Personally I think the times when you installed software on your computer are over. The best example are databases. Depending on the project and the customer, you may have to install different databases. After the project, the database will either remain installed or will be uninstalled.

It is much more elegant with Docker to start a container that starts the database as an image, depending on the project. This of course has the advantage that you don’t have to install the complete database and that you can access the same image later on in production.

In this tutorial I want to show you how to start and stop MongDB under Docker Container. This tutorial can also be used for MySQL and MariaDB.


  • Existing Docker 8 Installation
  • Linux knowledge

Creating Docker Containers with MongoDB

Docker is a platform or software with which it is possible to run images as containers. Images are ready-to-use environments that can be used without having to make major adjustments. The main advantage of images is that they always run in the same environment. This ensures that software running on an image in development will run in production later.

To use mongodb as an image we first have to look for a suitable image on the Docker Hub page.

In the search bar you can now search for images. Here we enter mongodb and get a list of results. Now you ask yourself which image to use. Here it’s up to you which image you want to use. I personally prefer the official image of mongoDB.

As we can see from the picture above, the name of the image is „mongo“. This information is sufficient for us to start the mongodb locally as a container with a single command.

For this you enter the following command in the console.

docker run -p 27017:27017 --name some-mongo -d mongo

After the line has been executed in the console, Docker starts downloading the image for the first time. This may take some time depending on your internet speed.

After the image has been downloaded and started, an ID appears. This is the ID of the container.

We can check that the container has started properly by running the following command on the console: docker ps

We see in the console output that our Docker Container is started. It is now ready to accept requests on the local port 27017.

To stop the container a command is also sufficient. With the following command you stop the Docker Container.

docker stop mein-mongo

If you try to start the Docker Container again, you will get an error message. The following error message will then appear in the console

etoker@etoker-XPS:~$ docker run -p 27017:27017 --name mein-mongo -d mongo
docker: Error response from daemon: Conflict. The container name "/mein-mongo" is already in use by container "0ffa16796e55c4348c2337af538cae54683fc1cea472bf531ff6236d18784f4c". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.

This error occurs only because you have assigned a name to the container. There can only be one docker container with a unique name. To work around this error, you can do the following:

  • Delete Docker Container with docker rm <container_id>
  • Starting a Docker Container with a Different Name
  • Or use Docker Compose!

How to manage it easier with Docker Compose I will show you in my next tutorial MongoDb with Docker Compose.

The Result

I hope you enjoyed my little tutorial. If you have any questions, criticism or suggestions, please post them as a comment.

Verwandte Beiträge

Leave a comment