nirmalakumarsahu

Docker

📄 Articles 👤 My Profile

Docker Containerization

DevOps


📑 Index


What is Docker?

Issues Without Docker

Note: To run our application, three key things are needed:

Installing and configuring all of these manually on different machines can be time-consuming and error-prone. Docker helps by packaging the application, its libraries, and all required dependencies into a single container, ensuring it runs consistently across any environment.

codebuils

Containerization

🔝 Back to Top


Docker Registry

Docker Hub

containerization

Note: In real-time production environments, we use Kubernetes (e.g., EKS) to run containers. Docker is used here for image creation and testing.

🔝 Back to Top


Docker Setup on AWS EC2

Step 1: Launch an EC2 Instance on AWS Cloud

Step 2: Connect to EC2 instance Using MobaXterm from your local machine

Note: If you see the terminal of your EC2, then you’re successfully connected.

terminal

Step 3: Install Docker

sudo yum update -y
sudo yum install docker -y
sudo service docker start
sudo usermod -aG docker ec2-user
exit
sudo apt update
curl -fsSL get.docker.com | /bin/bash
sudo usermod -aG docker ubuntu 
exit
docker -v
docker info

Note: Crate an account on Docker Hub

🔝 Back to Top


Docker Architecture

Note:

docker-architecture

Docker Commands

Command Description
docker images List all local images
docker pull <image-name> Pull an image from Docker Hub
docker run <image-name / image-id> Run a container from an image
docker ps List running containers
docker ps -a List all containers (including stopped)
docker stop <container-id> Stop a running container
docker start <container-id> Start a stopped container
docker rm <container-id> Remove a container
docker rmi <image-id> Remove an image
docker system prune -a Clean up unused containers/images
docker logs <container-id> View logs of a container

Example

docker pull hello-world
docker run hello-world

hello-world

Instead of manually pulling the image, we can directly run it using docker run. Docker will first check if the image exists locally. If it’s not found, it will automatically download (pull) the image from Docker Hub and then run the container.

Let’s run a Spring boot Rest API

port-mapping

docker run -d -p 9090:9090 ashokit/spring-boot-rest-api

Note: The first 9090 is the host port (your EC2 or local machine), and the second 9090 is the container port. This means that requests to port 9090 on the host will be forwarded to port 9090 inside the container.

🔝 Back to Top


Dockerfile

Purpose of a Dockerfile

Dockerfile Instructions or Keywords

FROM

MAINTAINER (Deprecated)

Note: The MAINTAINER instruction is deprecated in favor of LABEL

 LABEL maintainer="nirmalakumarsahu@gmail.com"

COPY

RUN

CMD

Notes:

ENTRYPOINT

EXPOSE

WORKDIR

ENV

Sample Dockerfile

Note: To read more about vi/ vim

Dockerfile

FROM ubuntu

MAINTAINER <Nirmala Kumar Sahu>

RUN echo 'run msg 1'
RUN echo 'run msg 2'

CMD echo 'CMD msg 1'
CMD echo 'CMD msg 2'

To Build a Docker Image

docker build -t <image-name>:<tag> .
# or
docker build -t <image-name> .

Notes: If you use a different name instead of ‘Dockerfile’, you must specify the file name using the -f option before the build context (e.g: .)

 docker build -t <image-name>:<tag> -f <custom docker file> .

To Run a Docker Image

docker run <image-name>

Note:

To Push a Docker Image to Docker Hub

Steps:

  1. Create an image using your Docker Hub username as a prefix, like this:

    docker build -t dockerhub-username/image-name:tag .
    #or
    docker build -t dockerhub-username/image-name .
     
    # Example
    docker build -t nirmalakumarsahu/app1:1.0 .
    
  2. Login to Docker Hub, enter your Docker Hub username and password/ token when prompted.

    docker login
    
  3. Push the image:

    docker push dockerhub-username/image-name:tag
    #or
    docker push dockerhub-username/image-name
       
    #Example
    docker push nirmalakumarsahu/app1:1.0
    

Note:

Dockerfile for Java Web Application

Dockerfile

FROM tomcat-jre-1.8:9.5

MAINTAINER <Nirmala Kumar Sahu>

EXPOSE 8080

COPY target/app.war /usr/app/local/tomcat/webapps

Dockerfile Spring Boot Application

Dockerfile

FROM openjdk:11

LABEL maintainer="Nirmala Kumar Sahu"

WORKDIR /app

COPY target/sbapp.jar sbapp.jar

EXPOSE 8080

ENTRYPOINT [“java”, “-jar”, “sbapp.jar”]

Note: Different programming languages Dockerfile

Dockerizing Spring Boot Application

  1. Install Git and Clone the Repository spring-boot-docker-app

    sudo yum install git -y
    git clone <repo-url>
    
  2. Install Maven

    sudo yum install maven -y
    
  3. Navigate to the Project Directory and Build the Application

    cd <project-directory>
    mvn clean package
    
  4. Build the Docker Image
    docker build -t your-image-name .
    
  5. Run the Docker Container
    docker run -d -p 8090:8090 your-image-name
    
  6. Enable host port in security group and access the application.

    http://Public IP:port/api/v1/home/greeting

Note: Ensure that the Java version used by Maven and on your EC2 instance matches the Java version specified in your project.

🔝 Back to Top


Docker Compose

Key Features

Why Use Docker Compose?

Key Concepts in Docker Compose

services

services:
  <service-name>:
    image: <image-name>

image

image: <image-name>

build

build:
  context: ./app
  dockerfile: Dockerfile

container_name

container_name: <container-name>

command

command: ["java", "start"]

ports

ports:
  - "8080:80"

environment

environment:
  - MYSQL_ROOT_PASSWORD=root
  - MYSQL_DATABASE=mydb

volumes

volumes:
  - ./data:/var/lib/mysql
  - db-data:/var/lib/mysql

networks

networks:
  - backend

depends_on

depends_on:
  - db

Example docker-compose.yml

version: '3.8'
services:
  mysql:
    image: mysql:8
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydatabase
    networks:
      - spring-net

  springboot-app:
    image: my-springboot-app:latest
    container_name: springboot-app
    depends_on:
      - mysql
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/mydatabase
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root
    networks:
      - spring-net

networks:
  spring-net:
    driver: bridge

Docker Compose Commands

Command Description
docker compose up Start services defined in docker-compose.yml.
docker compose up -d Start services in detached mode (background).
docker compose down Stop and remove containers, networks, volumes, and images created by up.
docker compose ps List containers related to the Compose project.
docker compose stop Stop running containers without removing them.
docker compose start Start existing (stopped) containers.
docker compose restart Restart services.
docker compose logs View logs of the services.
docker compose logs -f Follow logs output (real-time).
docker compose build Build or rebuild services.
docker compose pull Pull service images.
docker compose push Push service images.
docker compose exec <service> <command> Run a command inside a running container (e.g., bash).
docker compose run <service> <command> Run one-off command (creates a new container).
docker compose config Validate and view the merged Compose file configuration.

🔝 Back to Top


Docker Networks

Types of Docker Networks

bridge (default)

networks:
  my-bridge:
    driver: bridge

host

network_mode: host

none

network_mode: none

overlay (for Docker Swarm)

networks:
  my-overlay:
    driver: overlay

macvlan

networks:
  macvlan_network:
    ipv4_address: 192.168.1.101

🔝 Back to Top


Dockerizing Spring Boot App with MySQL using Docker Compose

Docker network

Step 1: Install Docker compose if not installed

# Create the CLI plugin directory
mkdir -p ~/.docker/cli-plugins

# Download the latest Docker Compose binary
curl -SL https://github.com/docker/compose/releases/download/v2.35.1/docker-compose-linux-x86_64 \
-o $DOCKER_CONFIG/cli-plugins/docker-compose

# Make the binary executable
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose

# Test version
docker compose version
# Create the CLI plugin directory
mkdir -p ~/.docker/cli-plugins

# Download the latest Docker Compose binary
curl -SL https://github.com/docker/compose/releases/download/v2.35.1/docker-compose-linux-x86_64 \
-o ~/.docker/cli-plugins/docker-compose

# Make the binary executable
chmod +x ~/.docker/cli-plugins/docker-compose

# Test version
docker compose version

Step 2: Clone the Repository spring-boot-docker-app

git clone <repo-url>

Step 3: Navigate to the Project Directory and Build the Application

cd <project-directory>
mvn clean package

Step 4: Build the Docker Image

docker build -t your-image-name .

Note:

Step 5: Run the Docker Container using docker compose

docker compose up -d

Step 6: Enable host port in security group and access the application.

http://Public IP:port/

🔝 Back to Top

📖 Read More ➡️