DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on • Edited 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

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

docker -v
Enter fullscreen mode Exit fullscreen mode

Pull Sample Images

Practice pulling images from Docker Hub.

Example:

docker pull psait/helloacademy:latest
Enter fullscreen mode Exit fullscreen mode

Official example:

docker pull hello-world
Enter fullscreen mode Exit fullscreen mode

Important Docker Commands

Pull Image

Download image from Docker Hub.

docker pull [image-name]
Enter fullscreen mode Exit fullscreen mode

Run Container

Creates and runs a container.

docker run [image-name]
Enter fullscreen mode Exit fullscreen mode

List Running Containers

docker ps
Enter fullscreen mode Exit fullscreen mode

List All Containers

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Stop Container

docker stop [container-id]
Enter fullscreen mode Exit fullscreen mode

Start Container

docker start [container-id]
Enter fullscreen mode Exit fullscreen mode

Remove Container

docker rm [container-id]
Enter fullscreen mode Exit fullscreen mode

Remove Docker Image

docker rmi [image-name or image-id]
Enter fullscreen mode Exit fullscreen mode

Remove Unused Containers and Images

docker system prune -a
Enter fullscreen mode Exit fullscreen mode

Running Container with Port Mapping

Running a container normally:

docker run [image-name]
Enter fullscreen mode Exit fullscreen mode

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


Port Mapping

Syntax:

docker run -p host-port:container-port [image-name]
Enter fullscreen mode Exit fullscreen mode

Example:

docker run -p 9090:9090 [image-name]
Enter fullscreen mode Exit fullscreen mode

Run container in background:

docker run -d -p 9090:9090 [image-name]
Enter fullscreen mode Exit fullscreen mode

Access application:

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

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

sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version
Enter fullscreen mode Exit fullscreen mode

Step 3 — Install Jenkins

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
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
sudo apt-get update
sudo apt-get install jenkins
Enter fullscreen mode Exit fullscreen mode

Step 4 — Start Jenkins

sudo systemctl enable jenkins
sudo systemctl start jenkins
Enter fullscreen mode Exit fullscreen mode

Step 5 — Verify Jenkins

sudo systemctl status jenkins
Enter fullscreen mode Exit fullscreen mode

Step 6 — Access Jenkins

http://public-ip:8080
Enter fullscreen mode Exit fullscreen mode

Step 7 — Get Jenkins Admin Password

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

docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:lts-community
Enter fullscreen mode Exit fullscreen mode

Nexus

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.

FROM openjdk:17
Enter fullscreen mode Exit fullscreen mode

Examples:

FROM python:3.9
FROM openjdk:17
FROM tomcat:9.0
Enter fullscreen mode Exit fullscreen mode

2. MAINTAINER (Deprecated)

Old:

MAINTAINER Name <email>
Enter fullscreen mode Exit fullscreen mode

New:

LABEL maintainer="Name <email>"
Enter fullscreen mode Exit fullscreen mode

3. RUN

Executes commands during image build.

RUN git clone <repo-url>
RUN mvn clean package
Enter fullscreen mode Exit fullscreen mode

4. CMD

Default command when container starts.

CMD "java -jar myapp.jar"
Enter fullscreen mode Exit fullscreen mode

Only the last CMD instruction will run.


Example 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

docker build -t helloacademy/image1 .
Enter fullscreen mode Exit fullscreen mode

. means Dockerfile is in the current directory.


List Docker Images

docker images
Enter fullscreen mode Exit fullscreen mode

Run Image

docker run [image-id]
Enter fullscreen mode Exit fullscreen mode

ENTRYPOINT

Alternative to CMD but cannot be overridden.

Example:

CMD "java -jar app.jar"
Enter fullscreen mode Exit fullscreen mode

Better:

ENTRYPOINT ["java","-jar","app.jar"]
Enter fullscreen mode Exit fullscreen mode

COPY

Copies files from host to container.

COPY target/app.jar /usr/app/
Enter fullscreen mode Exit fullscreen mode

ADD

Similar to COPY but can extract .tar files.

ADD target/app.jar /usr/app/
Enter fullscreen mode Exit fullscreen mode

Note:

ADD cannot download from HTTP/S3 URLs.


WORKDIR

Sets working directory.

WORKDIR /usr/app/
Enter fullscreen mode Exit fullscreen mode

EXPOSE

Specifies application port.

EXPOSE 8080
Enter fullscreen mode Exit fullscreen mode

Dockerizing Spring Boot Application

Spring Boot applications are packaged as JAR files.

Run command:

java -jar app.jar
Enter fullscreen mode Exit fullscreen mode

Default port:

8080
Enter fullscreen mode Exit fullscreen mode

Dockerfile for Spring Boot

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

git clone <repo-url>
Enter fullscreen mode Exit fullscreen mode

Build JAR

mvn clean package
Enter fullscreen mode Exit fullscreen mode

Build Docker Image

docker build -t psait/helloacademy:<tag> .
Enter fullscreen mode Exit fullscreen mode

Tags example:

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

List Images

docker images
Enter fullscreen mode Exit fullscreen mode

Run Container

docker run -d -p 8080:8080 --name psa psait/helloacademy
Enter fullscreen mode Exit fullscreen mode

Check Running Containers

docker ps
Enter fullscreen mode Exit fullscreen mode

View Logs

docker logs <container-id>
Enter fullscreen mode Exit fullscreen mode

Access Application

http://public-ip:8080
Enter fullscreen mode Exit fullscreen mode

Push Image to Docker Hub

Login

docker login
Enter fullscreen mode Exit fullscreen mode

Enter:

username
password
Enter fullscreen mode Exit fullscreen mode

Push Image

docker push psait/helloacademy:<tag>
Enter fullscreen mode Exit fullscreen mode





Enter fullscreen mode Exit fullscreen mode

Top comments (0)