DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on

docker setup/install in Ec2 AWS

Below is your content converted into a clean, well-structured Markdown (.md) document with a short analysis section at the beginning (as you requested). I also fixed spelling, formatting, headings, and diagrams so it renders nicely in GitHub, Notion, or any Markdown viewer.

You can save it as: docker-notes.md


# Docker Notes

## Analysis
Docker is a containerization platform that solves software compatibility issues across different environments. Instead of installing software stacks (Java, Node, Angular, etc.) separately on every server, Docker packages the application along with its dependencies into an image. This image can then run consistently on any system that has Docker installed.

Key benefits include:
- Eliminates compatibility issues
- Simplifies deployment across multiple servers
- Enables faster software upgrades
- Ensures consistent environments from development to production

---

# Docker

## Why Docker?

- Resolves **compatibility problems** using containerization.
- Helps install software on different servers **without worrying about dependency conflicts**.
- Allows deploying the **same application on multiple servers for load balancing** without manually installing the technology stack.
- Enables **easy software upgrades across multiple machines**.

---

## Important Notes**Docker Engine** acts as a bridge between the container and the host OS.  
✔ Containers **do not have their own kernel**; they use the **host's kernel**.  
✔ Docker Engine **manages containers but does not create them** — it runs and controls them.

---

## Basic Architecture Flow

Enter fullscreen mode Exit fullscreen mode

Linux VM

Install Docker Engine

Docker Engine acts as bridge between Linux Kernel and Docker Containers

Provides isolated environment to run Docker Images

Enter fullscreen mode Exit fullscreen mode

Docker Container

Docker Engine

Linux VM


---

# Docker Architecture

## Components

### Dockerfile
Contains instructions to build a Docker image.

### Docker Image
Application package including:
- Application code
- Dependencies
- Libraries
- Runtime environment

### Docker Registry
Repository to store Docker images.

Examples:
- Docker Hub
- Private registries

### Docker Container
An isolated environment where a Docker image runs.

---

# What is Containerization?

Containers package an application **along with its dependencies** (libraries, configuration files, etc.) ensuring it runs consistently across different computing environments.

Enter fullscreen mode Exit fullscreen mode


plaintext

Dockerfile

Docker Image

Docker Registry / Docker Hub

Docker Container


Example flow:

Enter fullscreen mode Exit fullscreen mode


plaintext

Dockerfile
(Contains instructions
to download dependencies)

Docker Image
(Application + Dependencies)

Docker Hub / Registry
(Collection of images)

Containers
(Container1, Container2)


Running inside:

Enter fullscreen mode Exit fullscreen mode


plaintext

Linux Server


---

# Installing Docker

## Install Docker on Amazon Linux

```

bash
sudo yum update -y
sudo yum install docker -y
sudo service docker start
sudo usermod -aG docker ec2-user
exit


Enter fullscreen mode Exit fullscreen mode

Install Docker on Ubuntu


bash
sudo apt update
curl -fsSL get.docker.com | /bin/bash
sudo usermod -aG docker ubuntu
exit


Enter fullscreen mode Exit fullscreen mode

Verify Docker Installation


bash
docker -v


Enter fullscreen mode Exit fullscreen mode

Pull Sample Images

Practice pulling images from Docker Hub.

Example:


bash
docker pull psait/helloacademy:latest


Enter fullscreen mode Exit fullscreen mode

Official example:


bash
docker pull hello-world


Enter fullscreen mode Exit fullscreen mode

Important Docker Commands

Pull Image

Download image from Docker Hub.


bash
docker pull [image-name]


Enter fullscreen mode Exit fullscreen mode

Run Container

Creates and runs a container.


bash
docker run [image-name]


Enter fullscreen mode Exit fullscreen mode

List Running Containers


bash
docker ps


Enter fullscreen mode Exit fullscreen mode

List All Containers


bash
docker ps -a


Enter fullscreen mode Exit fullscreen mode

Stop Container


bash
docker stop [container-id]


Enter fullscreen mode Exit fullscreen mode

Start Container


bash
docker start [container-id]


Enter fullscreen mode Exit fullscreen mode

Remove Container


bash
docker rm [container-id]


Enter fullscreen mode Exit fullscreen mode

Remove Docker Image


bash
docker rmi [image-name or image-id]


Enter fullscreen mode Exit fullscreen mode

Remove Unused Containers and Images


bash
docker system prune -a


Enter fullscreen mode Exit fullscreen mode

Running Container with Port Mapping

Running a container normally:


bash
docker run [image-name]


Enter fullscreen mode Exit fullscreen mode

You cannot access it in a browser unless you map ports.


Port Mapping

Syntax:


bash
docker run -p host-port:container-port [image-name]


Enter fullscreen mode Exit fullscreen mode

Example:


bash
docker run -p 9090:9090 [image-name]


Enter fullscreen mode Exit fullscreen mode

Run container in background:


bash
docker run -d -p 9090:9090 [image-name]


Enter fullscreen mode Exit fullscreen mode

Access application:


shell
http://public-ip:host-port


Enter fullscreen mode Exit fullscreen mode

Important Notes

  1. Enable Inbound Rules in the security group for the host port.
  2. If running multiple containers, each must use a different host port.

Install Jenkins using Docker


bash
docker run -d -p 8080:8080 jenkins/jenkins


Enter fullscreen mode Exit fullscreen mode

Enable port 8080 in the security group.


Installing Jenkins Without Docker

Step 1 — Create Linux VM

Use Ubuntu EC2 instance (t2.medium).

Enable port 8080 in security group.


Step 2 — Install Java


bash
sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version


Enter fullscreen mode Exit fullscreen mode

Step 3 — Install Jenkins


bash
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key


Enter fullscreen mode Exit fullscreen mode

bash
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null


Enter fullscreen mode Exit fullscreen mode

bash
sudo apt-get update
sudo apt-get install jenkins


Enter fullscreen mode Exit fullscreen mode

Step 4 — Start Jenkins


bash
sudo systemctl enable jenkins
sudo systemctl start jenkins


Enter fullscreen mode Exit fullscreen mode

Step 5 — Verify Jenkins


bash
sudo systemctl status jenkins


Enter fullscreen mode Exit fullscreen mode

Step 6 — Access Jenkins


shell
http://public-ip:8080


Enter fullscreen mode Exit fullscreen mode

Step 7 — Get Jenkins Admin Password


bash
sudo cat /var/lib/jenkins/secrets/initialAdminPassword


Enter fullscreen mode Exit fullscreen mode

Step 8 — Create Admin User and Install Plugins


Example Docker Installations

SonarQube


bash
docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:lts-community


Enter fullscreen mode Exit fullscreen mode

Nexus


bash
docker run -d -p 8081:8081 --name nexus sonatype/nexus3


Enter fullscreen mode Exit fullscreen mode

Creating a Dockerfile

Dockerfile instructions are case-sensitive and must be written in uppercase.


1. FROM

Defines base image.


dockerfile
FROM openjdk:17


Enter fullscreen mode Exit fullscreen mode

Examples:


docker
FROM python:3.9
FROM openjdk:17
FROM tomcat:9.0


Enter fullscreen mode Exit fullscreen mode

2. MAINTAINER (Deprecated)

Old:


docker
MAINTAINER Name <email>


Enter fullscreen mode Exit fullscreen mode

New:


docker
LABEL maintainer="Name <email>"


Enter fullscreen mode Exit fullscreen mode

3. RUN

Executes commands during image build.


dockerfile
RUN git clone <repo-url>
RUN mvn clean package


Enter fullscreen mode Exit fullscreen mode

4. CMD

Default command when container starts.


dockerfile
CMD "java -jar myapp.jar"


Enter fullscreen mode Exit fullscreen mode

Only the last CMD instruction will run.


Example Dockerfile


dockerfile
FROM openjdk:17
MAINTAINER Hello
RUN echo 'run-1'
RUN echo 'run-2'
CMD echo 'cmd-1'
CMD echo 'cmd-2'


Enter fullscreen mode Exit fullscreen mode

Build Docker Image


bash
docker build -t helloacademy/image1 .


Enter fullscreen mode Exit fullscreen mode

. means Dockerfile is in the current directory.


List Docker Images


bash
docker images


Enter fullscreen mode Exit fullscreen mode

Run Image


bash
docker run [image-id]


Enter fullscreen mode Exit fullscreen mode

ENTRYPOINT

Alternative to CMD but cannot be overridden.

Example:


docker
CMD "java -jar app.jar"


Enter fullscreen mode Exit fullscreen mode

Better:


docker
ENTRYPOINT ["java","-jar","app.jar"]


Enter fullscreen mode Exit fullscreen mode

COPY

Copies files from host to container.


dockerfile
COPY target/app.jar /usr/app/


Enter fullscreen mode Exit fullscreen mode

ADD

Similar to COPY but can extract .tar files.


dockerfile
ADD target/app.jar /usr/app/


Enter fullscreen mode Exit fullscreen mode

Note:

ADD cannot download from HTTP/S3 URLs.


WORKDIR

Sets working directory.


dockerfile
WORKDIR /usr/app/


Enter fullscreen mode Exit fullscreen mode

EXPOSE

Specifies application port.


dockerfile
EXPOSE 8080


Enter fullscreen mode Exit fullscreen mode

Dockerizing Spring Boot Application

Spring Boot applications are packaged as JAR files.

Run command:


bash
java -jar app.jar


Enter fullscreen mode Exit fullscreen mode

Default port:


plaintext
8080


Enter fullscreen mode Exit fullscreen mode

Dockerfile for Spring Boot


dockerfile
FROM openjdk:17

COPY target/demo-app.jar /usr/app/

WORKDIR /usr/app/

EXPOSE 8080

ENTRYPOINT ["java","-jar","demo-app.jar"]


Enter fullscreen mode Exit fullscreen mode

Steps to Build and Run

Clone Repository


bash
git clone <repo-url>


Enter fullscreen mode Exit fullscreen mode

Build JAR


bash
mvn clean package


Enter fullscreen mode Exit fullscreen mode

Build Docker Image


bash
docker build -t psait/helloacademy:<tag> .


Enter fullscreen mode Exit fullscreen mode

Tags example:

  • prod-v1
  • dev-v1
  • test-v1
  • staging-v1

List Images


bash
docker images


Enter fullscreen mode Exit fullscreen mode

Run Container


bash
docker run -d -p 8080:8080 --name psa psait/helloacademy


Enter fullscreen mode Exit fullscreen mode

Check Running Containers


bash
docker ps


Enter fullscreen mode Exit fullscreen mode

View Logs


bash
docker logs <container-id>


Enter fullscreen mode Exit fullscreen mode

Access Application


plaintext
http://public-ip:8080


Enter fullscreen mode Exit fullscreen mode

Push Image to Docker Hub

Login


bash
docker login


Enter fullscreen mode Exit fullscreen mode

Enter:


plaintext
username
password


Enter fullscreen mode Exit fullscreen mode

Push Image


bash
docker push psait/helloacademy:<tag>


Enter fullscreen mode Exit fullscreen mode





Enter fullscreen mode Exit fullscreen mode

Top comments (0)