Using Docker to keep your dev environment clean

Hello everyone! In this post, I will touch on a workflow subject. More specifically, on how to keep your local environment clean from project-related dependencies.

All software projects, no matter how big or small, have external dependencies. Let's use a database as an example since it's one of the most common cases I can imagine. At some point during development, you will need to install the database software on your local machine. You can directly install the software on your local system, and if you work on a single project with very few dependencies, this is a viable solution.
However, imagine a situation where you work on multiple projects with multiple dependencies, and each project depends on different versions of the same software. It can get out of hand quite fast. You can end up installing and uninstalling packages, and this doesn't always work out smoothly since you can end up with misconfigurations. Having mentioned configuration, you will often need to configure different variables on the software dependencies you install, making it even more challenging to keep track of what changes you made.
In the end, we have a snowflake system that we are not sure how to recreate, and gives us many opportunities to exclaim the famous phrase: "It works on my machine!"

A common way to avoid polluting our environment is by using Docker. Docker allows us to install and run isolated containers that encapsulate the external dependencies we need without polluting our system.
Next up, I will showcase how to use Docker to install and manage MongoDB on your local machine.

Setting up Docker

First of all, you need to install Docker, follow the installation process according to your platform. I'm using Linux. Depending on how you installed it, the Docker daemon process might already be running or not.

Run the following command to fetch a test image and start a container:

sudo docker run hello-world

If you get the following error, it means that the docker daemon process is not running:

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

To start the daemon process:

sudo dockerd

If you want the docker daemon to start on system boot, you can follow the instructions here on how to do it with systemd.
One last thing I would recommend is to create a group for Docker and add our user there to avoid using sudo in our commands. You can look up the details here. To do this run:

sudo groupadd docker          # Creates the docker group
sudo usermod -aG docker $USER # Adds the current user to the docker group
newgrp docker                 # Login to the new group

If you don't know what these commands do I encourage you to look up the following links:

Now you should be able to run the hello world example without sudo:

docker run hello-world

Setting up MongoDB using Docker

First, we need to pull the image of the MongoDB version we want to use. You can browse images in DockerHub. We pull the latest version of MongoDB:

docker pull mongo:latest

Next up, we create and run a container named mongodb-latest-local. We can use this name to refer to the specific container in other Docker commands.

docker run -d -p 27017:27017 --name mongodb-latest-local mongo:latest

To stop the container:

docker container stop mongodb-latest-local

The next time we want to start the container, we can just run:

docker container start mongodb-latest-local

We successfully installed MongoDB on our machine, but what if we want to log in to the mongo shell to create a database and some entries? To do so, we have to start an interactive shell that connects to the container and starts the mongo shell. We can do this with the following command:

docker exec -it mongodb-latest-local mongo


In this short post, we set up Docker and used it to keep our local development environment clean. We used MongoDB as an example of how to create, start, and stop a container.
I hope you found this post informative! Stay tuned!

Subscribe to Backend Definite

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.