Day 2 of My DevOps Journey: Dockerizing Applications and Multi-Container Setup ๐Ÿš€

Pankil SoniPankil Soni
3 min read

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

# 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

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

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

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"

MongoDB and Mongo Express Compose File

yamlCopyversion: '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

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

0
Subscribe to my newsletter

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

Written by

Pankil Soni
Pankil Soni

As a third-year B.Tech. AI & ML student, I am driven by a passion for technology and a desire to contribute to the field of computer science. With a strong foundation in programming languages, algorithms, and data structures, I am eager to learn more about the ever-evolving world of technology. As a student, I am committed to continuous learning and growth. I am eager to explore new technologies, develop new skills, and expand my knowledge in computer science. I am seeking opportunities to apply my skills in a dynamic and challenging environment, and I am excited to connect with professionals in the industry who share my passion for technology.