DEV Community

Harshit Goyal
Harshit Goyal

Posted on

Docker Basics: What Every New Developer Should Know

Docker containers are really useful and often misunderstood, so in this article we will explore what docker is and how it could be used in a dev workflow.

What is Docker

Docker is a platform that allows you to build, run, and manage applications in lightweight, portable containers.
Each container has it own virtual file system.

The docker containers run in an isolated environment, so there are no version conflicts.

Consider a developer working on an application that uses Redis and MongoDB. To get started, they would typically need to install both services on their local machine. If they later need to switch to a different version of MongoDB, they would have to download and install that version separately. Managing multiple versions or running them side by side can become complex and cumbersome.

With docker he can simplify his process of development as he can run these applications on docker in isolated containers.
We will see in the later part of this tutorial, how we can run multiple applications together with docker.

Please install docker locally to follow along
Docker installtion varies according to the OS , check on official docker website for details.


Docker basic commands

To check if docker is correctly installed on your system, try -

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

Your output should be like this

Docker-hello-world

Now docker is correctly installed on your system , lets explore some basic docker commands -

1.

docker images
Enter fullscreen mode Exit fullscreen mode

This command will list all docker images in your local system.

2.

docker pull mongo
Enter fullscreen mode Exit fullscreen mode

This command will pull the image from a docker image repository.
By default it uses dockerHub

Every image has two fields-

  • Image name
  • Version

In our case when we use mongo, mongo is image name and by default version is latest.
So the command is same as -

docker pull mongo:latest
Enter fullscreen mode Exit fullscreen mode

Instead of latest, we can specify the exact version we require.

3.

docker run mongo:latest
Enter fullscreen mode Exit fullscreen mode

This command will start the container.
Now when we start the container, we need to access the port of the docker container.

docker run -p 27017:27017 mongo
Enter fullscreen mode Exit fullscreen mode
  • The first port is the host port
  • Second port is container port

27017 is the default port mongoDb exposes, so we open the same port on our local.
Now, whenever we setup a database, we need an admin username and password.
Its similar for docker as well.
For mongo we can provide environment variables to set these fields.

docker run -d \
  --name my-mongo \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=secretpassword \
  -p 27017:27017 \
  mongo
Enter fullscreen mode Exit fullscreen mode

Now mongoDb is all setup and available on port 27017.

4.
We can also specify volumes to persist the data.
Normally, if we stop a docker container, we loose the data.

docker run -v redisdata:/data redis
Enter fullscreen mode Exit fullscreen mode

This will persist all the cached data.


Docker Compose

Now that we have our MongoDB container set up, we still need a GUI to visualize the data, along with Redis. While it's possible to set up separate containers for each service manually, doing so requires considerable effort—starting each one individually and repeating the setup steps every time we need them can be time-consuming and inefficient.

To ease this process, we have docker compose.

Docker Compose is a tool that helps you define and run multi-container Docker applications using a simple YAML file.

We can create a docker-compose.yml file in the root directory.
The Compose file also requires the same configuration settings that we used when initially starting the MongoDB container.

Here is an example of docker compose file -

version: "3.8"

services:
  mongo-db:
    image: mongo
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: password
    volumes:
      - mongo-data:/data/db

  mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: password
      ME_CONFIG_MONGODB_SERVER: mongo-db
    depends_on:
      - mongo-db

  redis:
    image: redis
    ports:
      - 6379:6379
    volumes:
      - redis-data:/data

# use volumes to persist data
volumes:
  mongo-data:
  redis-data:

Enter fullscreen mode Exit fullscreen mode

To run we can use -

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

This will start 3 containers - mongoDb, mongo-express (GUI for mongo) and redis.
Now with a single command we can spin up any number of containers which are configured in docker compose file.

Docker-desktop-compose-up

When you finish the development, you can use -

docker-compose down
Enter fullscreen mode Exit fullscreen mode

This will stop all the containers (running from docker compose).

Docker-compose-down


Let me know in comments if you would like an advanced use case tutorial.

Top comments (0)