DevOps Journey Week 4: Diving Deep into Docker 🐳

Hey everyone! 👋
I’m thrilled to share my learning from Week 4 of my DevOps journey. This week was all about Docker – exploring best practices for creating, managing, and handling containers. From working with images to setting up networks, this was a hands-on week where I got a real feel for containerization and the power it brings to DevOps.

Let me walk you through what I learned and some essential Docker commands for developers.


🚀 Understanding Docker Basics

In simple terms, Docker helps in packaging applications and all their dependencies into "containers" so they can run consistently in different environments. Here are some key concepts I tackled:

  • Containers: Think of these as isolated environments that hold your application and everything it needs.

  • Images: The blueprint of a container, with everything pre-installed.

  • Volumes: Storage for your data, even when the container stops.

I used both the Docker CLI and Docker Compose to experiment with these concepts. Below are some examples and essential commands I found super helpful.


🛠️ Important Docker Commands

Here’s a list of commands I practiced that are especially useful for anyone starting out with Docker:

  • Pull an Image
    docker pull <image-name>
    This command lets you download an image from Docker Hub. It’s like grabbing a pre-made recipe!

  • List Containers
    docker ps -a vs. docker container ls
    docker ps -a shows all containers (even stopped ones), while docker container ls only lists the running containers.

  • Run a Container
    docker run <image>
    To run a container based on an image. Example:
    docker run --name <container-name> -it <image>
    This starts a container with an interactive terminal.

  • Stop a Container
    docker stop <container>
    Stops the container and frees up resources.

  • Run Multiple Containers on Different Ports
    If you want to run two containers with the same service on different ports, try this:

      docker run --name mongo-one -p 4000:27017 -d mongo
      docker run --name mongo-two -p 5000:27017 -d mongo
    
  • Prune Containers
    docker container prune
    This removes all stopped containers, keeping things clean.

  • View Logs
    docker logs <container-name>
    See what’s happening in your container by checking its logs.

  • Create a Network
    docker network create <network-name>
    A custom network makes it easy for multiple containers to communicate.


🔗 Connecting MongoDB and Express in Docker

To really test Docker’s networking power, I tried setting up MongoDB and Mongo Express containers. Here's how I did it:

  1. Create a MongoDB Container with a Custom Network

     shCopy codedocker run -p 27017:27017 \
     -e MONGO_INITDB_ROOT_USERNAME=admin \
     -e MONGO_INITDB_ROOT_PASSWORD=password \
     --net mongo-network \
     --name mongodb -d mongo
    
  2. Run Mongo Express with the Same Network

     shCopy codedocker run -d \
     -p 8081:8081 \
     -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
     -e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
     -e ME_CONFIG_MONGODB_SERVER=mongodb \
     --net mongo-network \
     --name mongo-express \
     mongo-express
    

    Now, Mongo Express can easily communicate with MongoDB through the mongo-network.


🧩 Managing Containers with Docker Compose

Using Docker Compose to manage multi-container applications makes things even simpler. Here’s an example configuration to run MongoDB and Mongo Express together:

  1. Create a YAML File

  2. Write the Setup Steps Declaratively

     version: "3"
     services:
       mongo:
         image: mongo
         ports:
           - "27017:27017"
         environment:
           - MONGO_INITDB_ROOT_USERNAME=admin
           - MONGO_INITDB_ROOT_PASSWORD=password
         volumes:
           - my-mongo:/data/db
       mongo-express:
         image: mongo-express
         ports:
           - "8081:8081"
         environment:
           - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
           - ME_CONFIG_MONGODB_ADMINPASSWORD=password
           - ME_CONFIG_MONGODB_SERVER=mongodb
     volumes:
       my-mongo:
         driver: local
    

With a simple docker-compose up, both services start together, connected and ready to use!


🛠️ Building Custom Images with Dockerfile

Creating a Dockerfile allows you to make custom images suited to your application’s specific needs. Here’s a basic example I tried for a Python app:

FROM python:3-alpine3.15
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 3000
CMD python ./index.py

To build the image, I used:
docker build -t <username>/<name>:0.0.1.RELEASE
This command builds and tags the image, ready for deployment!


🔍 What’s Next?

Next up, I’m planning to work on a project where I can practice my Docker skills further. Plus, I’ll be exploring Docker alternatives like Podman, CRI-O, and rkt to see which tools suit different scenarios.

You can check out more about my journey on my Hashnode blog.


Learning Docker has been both challenging and rewarding. It’s amazing to see how it simplifies deployment, and I’m excited to dive even deeper. Thanks for following along, and let’s keep learning together! 😊

0
Subscribe to my newsletter

Read articles from Abhishek Prajapati directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhishek Prajapati
Abhishek Prajapati

I am a passionate Software Engineer with over two years of experience in the tech industry, specializing in Full Stack Development and DevOps practices. Currently, I work on innovative projects in the automotive sector, where I develop and automate solutions that enhance user experience and streamline operations. With a keen interest in cloud technologies, automation, and infrastructure management, I am dedicated to mastering the DevOps landscape. I believe in simplifying complex concepts and sharing practical insights to help others in their learning journeys. Join me as I document my experiences, challenges, and triumphs in the world of DevOps!