DEV Community

Cover image for Day 2 of My DevOps Journey: Dockerizing Applications and Multi-Container Setup 🚀
Pankil Soni
Pankil Soni

Posted on

Day 2 of My DevOps Journey: Dockerizing Applications and Multi-Container Setup 🚀

After getting comfortable with basic Docker concepts yesterday, I dove deep into creating custom Docker images, deploying to AWS, and setting up multi-container applications. Here's my learning journey from Day 2!

1. Creating Custom Dockerfiles 📝

I created two applications and containerized them:

  • A Flask application
  • An Express.js application

Here's the process I followed:

  1. Created the applications locally
  2. Wrote Dockerfiles for each
  3. Built custom images
  4. Pushed them to Docker Hub

Key commands used:

# Building an image with host network access
docker build --network=host -t pankil1812/first-flask-app:0.0.2 .

# Running the containerized application
docker run -d -p 3000:3000 --name first-flask-app pankil1812/first-flask-app:0.0.2

# Checking container logs
docker logs first-flask-app

# Cleaning up unused containers
docker container prune
Enter fullscreen mode Exit fullscreen mode

2. AWS EC2 Deployment 🌩ī¸

Next, I moved to cloud deployment:

  1. Created a free-tier EC2 instance on AWS
  2. Installed Docker on the EC2 instance
  3. Successfully deployed both applications
  4. Accessed them via the EC2 public IP

3. Multi-Container Setup: MongoDB and Mongo Express 🔄

The real challenge came with setting up a multi-container application using MongoDB and Mongo Express.

First, pulled the necessary images:

docker pull mongo
docker pull mongo-express
Enter fullscreen mode Exit fullscreen mode

Then, created a network and ran the containers:

# Create a network for container communication
docker create network mongo-network

# Run MongoDB container
docker run --network mongo-network \
  -d -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=password \
  --name mongo mongo:latest

# Run Mongo Express container
docker run --network mongo-network \
  -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
  -e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
  -e ME_CONFIG_MONGODB_URL="mongodb://admin:password@mongo:27017/" \
  -e ME_CONFIG_BASICAUTH=false \
  -d --name my-mongoexpress \
  -p 8081:8081 mongo-express:latest
Enter fullscreen mode Exit fullscreen mode

But I faced a lot of issues while setting up these because of environment variables. 🤔 But then I tried the new approach of using ME_CONFIG_MONGODB_URL instead of ME_CONFIG_MONGODB_SERVER. 🚀

But finally I Successfully accessed Mongo Express UI through the EC2 public IP on port 8081! 🎉

4. Introduction to Docker Compose 📋

Finally, I learned about Docker Compose and created two compose files:

Express App Compose File

version: '3'
services:
  first-express-app:
    image: pankil1812/first-express-app:0.0.1
    ports:
      - "5000:5000"
Enter fullscreen mode Exit fullscreen mode

MongoDB and Mongo Express Compose File

version: '3'
services:
  mongo:
    image: mongo
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    networks:
      - mongo-network

  my-mongoexpress:
    image: mongo-express
    restart: always
    ports:
      - "8081:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_URL=mongodb://admin:password@mongo:27017/
      - ME_CONFIG_BASICAUTH=false
    depends_on:
      - mongo
    networks:
      - mongo-network

networks:
  mongo-network:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

To implement this:

  1. Stopped all previous containers
  2. Removed the old network
  3. Used docker-compose -f filename up to start the services

Key Learnings đŸŽ¯

  1. Building and pushing custom Docker images
  2. Cloud deployment with AWS EC2
  3. Network creation and container communication
  4. Environment variable configuration
  5. Docker Compose for multi-container orchestration

What's Next? 🚀

For Day 3, I'm planning to explore:

  • Docker volume management
  • Container health checks
  • Docker Compose for development environments
  • Container orchestration concepts

my github repo for the sources is : https://github.com/pankil-soni/my-devops-journey

the sources where I learned docker from are:

https://www.youtube.com/watch?v=rr9cI4u1_88
https://docs.docker.com/

Docker #DevOps #AWS #MongoDB #DockerCompose

Top comments (0)