Docker - vimlesh-verma16/Notes GitHub Wiki
Docker in one video: https://www.youtube.com/watch?v=9bSbNNH4Nqw
Notes:
Create an instance of EC2 with key value pair and do ssh
click on connect -> do ssh now
B:\Lecture's\AWS\pems>move "C:\Users\Vimlesh\Downloads\Docker_practice.pem" "C:\Users\Vimlesh\.ssh\"
1 file(s) moved.
B:\Lecture's\AWS\pems>icacls C:\Users\Vimlesh\.ssh\Docker_practice.pem /inheritance:r
processed file: C:\Users\Vimlesh\.ssh\Docker_practice.pem
Successfully processed 1 files; Failed processing 0 files
B:\Lecture's\AWS\pems>icacls C:\Users\Vimlesh\.ssh\Docker_practice.pem /grant:r "Vimlesh:R"
processed file: C:\Users\Vimlesh\.ssh\Docker_practice.pem
Successfully processed 1 files; Failed processing 0 files
B:\Lecture's\AWS\pems> ssh -i C:\Users\Vimlesh\.ssh\Docker_practice.pem [email protected]
Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-1021-aws x86_64)
Commands for ubuntu Clear for clearing ubuntu
ubuntu@ip-172-31-22-77:~$ sudo apt-get update
ubuntu@ip-172-31-22-77:~$ sudo apt-get install docker.io
ubuntu@ip-172-31-22-77:~$ sudo systemctl status docker
ubuntu@ip-172-31-22-77:~$ docker ps
ubuntu@ip-172-31-22-77:~$ whoami
ubuntu@ip-172-31-22-77:~$ sudo usermod -aG docker $USER
ubuntu@ip-172-31-22-77:~$ newgrp docker
ubuntu@ip-172-31-22-77:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ubuntu@ip-172-31-22-77:~$ docker login
Log in with your Docker ID or email address to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com/ to create one.
You can log in with your password or a Personal Access Token (PAT). Using a limited-scope PAT grants better security and is required for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/
Username: vimleshverma16
Password:
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
ubuntu@ip-172-31-22-77:~$ docker pull hello-world
ubuntu@ip-172-31-22-77:~$ docker images
ubuntu@ip-172-31-22-77:~$ docker run hello-world
ubuntu@ip-172-31-22-77:~$ docker pull mysql
ubuntu@ip-172-31-22-77:~$ docker images
DockerFile -> Image -> Container
login steps in Docker login
- Use username from dockerhub as vimeshverma16
- generate the Personal Access Token paste the token with right click it will be invisible hit enter
Cloning and running a java Project
Last login: Tue Mar 11 17:16:29 2025 from 101.0.62.207
ubuntu@ip-172-31-22-77:~$ ls
ubuntu@ip-172-31-22-77:~$ mkdir projects
ubuntu@ip-172-31-22-77:~$ cd projects/
ubuntu@ip-172-31-22-77:~/projects$ git clone https://github.com/LondheShubham153/simple-java-docker.git
Cloning into 'simple-java-docker'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 9 (from 1)
Receiving objects: 100% (13/13), done.
ubuntu@ip-172-31-22-77:~/projects$ cd simple-java-docker/
ubuntu@ip-172-31-22-77:~/projects/simple-java-docker$ ls
Dockerfile README.md src
ubuntu@ip-172-31-22-77:~/projects/simple-java-docker$ rm -v Dockerfile
removed 'Dockerfile'
ubuntu@ip-172-31-22-77:~/projects/simple-java-docker$ vim Dockerfile
ubuntu@ip-172-31-22-77:~/projects/simple-java-docker$ docker build -t java-app .
ubuntu@ip-172-31-22-77:~/projects/simple-java-docker$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java-app latest 3315249a58c0 3 minutes ago 326MB
mysql latest 5568fddd4f66 6 weeks ago 797MB
hello-world latest 74cc54e27dc4 6 weeks ago 10.1kB
openjdk 17-jdk-alpine 264c9bdce361 3 years ago 326MB
ubuntu@ip-172-31-22-77:~/projects/simple-java-docker$ docker run java-app
Hello, Docker! Current date: Tue Mar 11 19:01:30 GMT 2025
[Finished] Container ran succesfully
DockerFile Steps :-
# Pull a base image that gives all required Librarries and tools
FROM openjdk:17-jdk-alpine
# Create a folder where app CODE will be stored
WORKDIR /app
# Copy source code from your Host Machine to container
COPY src/Main.java /app/Main.java
# Compile the application code
RUN javac Main.java
# Run the Java application when the container starts
CMD ["java", "Main"]
Exit vim with :wq
ubuntu@ip-172-31-22-77:~/projects/flask-app-ecs$ docker build -t flask-app .
ubuntu@ip-172-31-22-77:~/projects/flask-app-ecs$ docker run -d -p 80:80 flask-app
f37607d23986a9347821a1c19c2f9dd0988563f96cd4f1d8a201363699ef85c7
ubuntu@ip-172-31-22-77:~/projects/flask-app-ecs$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f37607d23986 flask-app "python run.py" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp loving_yonath
ubuntu@ip-172-31-22-77:~/projects/flask-app-ecs$ docker logs f37607d23986
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:80
* Running on http://172.17.0.2:80
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 841-337-435
ubuntu@ip-172-31-22-77:~/projects/flask-app-ecs$ docker attach f37607d23986
ubuntu@ip-172-31-22-77:~/projects/flask-app-ecs$ docker stop f37607d23986
f37607d23986
ubuntu@ip-172-31-22-77:~/projects/flask-app-ecs$ docker start f37607d23986
Making a docker File for Python Flask
FROM Python:3.7
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
ENTRYPOINT ["python","run.py"]
Edit Inbound rules
ubuntu@ip-172-31-22-77:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
cbd041c62e97 bridge bridge local
4ec915716107 host host local
9cdbc1cda4d8 none null local
ubuntu@ip-172-31-22-77:~$ docker network create mynetwork -d bridge
4c149fbdf07da40d4b206831437ded4a446aa399f9272807451bb6cb947d3da0
ubuntu@ip-172-31-22-77:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
cbd041c62e97 bridge bridge local
4ec915716107 host host local
4c149fbdf07d mynetwork bridge local
9cdbc1cda4d8 none null local
ubuntu@ip-172-31-22-77:~$ cd projects/
ubuntu@ip-172-31-22-77:~/projects$ cd two-tier-flask-app/
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ ls
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker build -t two-tier-backend .
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker images
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker run -d -p 5000:5000 -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=root -e MYSQL_DB=devops two-ti
er-backend:latest
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker ps
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker ps -a
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker logs 9249b9f7260a
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker network create -d bridge two-tier
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker network ls
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker run -d --name mysql --network two-tier -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=devops mysql
c09f425d70008ffd9103cb7e801f4b91f8bbfbd206b549c70d3d227d69eea682
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker run -d -p 5000:5000 --network two-tier -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=root -e MYS
QL_DB=devops two-tier-backend:latest
5ab2f42a2d831caad5a47ea7ea4cc6d1b4d7e8142c2f26758933947d3e2b4c19
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker ps
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker network inspect two tier
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ab2f42a2d83 two-tier-backend:latest "python app.py" 27 minutes ago Up 26 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp keen_goldberg
c09f425d7000 mysql "docker-entrypoint.s…" 28 minutes ago Up 28 minutes 3306/tcp, 33060/tcp mysql
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker exec -it c09f425d7000 bash
After that edit inbound rules add custom TCP with 5000 do like this remove 's and with public ip:5000 http://34.235.89.124:5000/
ubuntu@ip-172-31-22-77:~$ docker volume ls
ubuntu@ip-172-31-22-77:~$ docker volume create mysql-data
ubuntu@ip-172-31-22-77:~$ docker inspect mysql-data
ubuntu@ip-172-31-22-77:~$ docker ps -a
ubuntu@ip-172-31-22-77:~$ docker restart 5ab2f42a2d83
5ab2f42a2d83
ubuntu@ip-172-31-22-77:~$ docker rm c09f425d7000
c09f425d7000
ubuntu@ip-172-31-22-77:~$ docker volume inspect mysql-data
ubuntu@ip-172-31-22-77:~$ sudo su
root@ip-172-31-22-77:/home/ubuntu# cd /var/lib/docker/volumes/mysql-data/_data
root@ip-172-31-22-77:/var/lib/docker/volumes/mysql-data/_data# ls
'#ib_16384_0.dblwr' '#innodb_temp' binlog.000002 ca.pem devops mysql mysql_upgrade_history public_key.pem sys
'#ib_16384_1.dblwr' auto.cnf binlog.index client-cert.pem ib_buffer_pool mysql.ibd performance_schema server-cert.pem undo_001
'#innodb_redo' binlog.000001 ca-key.pem client-key.pem ibdata1 mysql.sock private_key.pem server-key.pem undo_002
root@ip-172-31-22-77:/var/lib/docker/volumes/mysql-data/_data# exit
exit
YAML File
version: "3.8"
services:
mysql:
user: "${UID}:${GID}"
image: mysql:5.7
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: devops
MYSQL_USER: admin
MYSQL_PASSWORD: admin
volumes:
- ./mysql-data:/var/lib/mysql
- ./message.sql:/docker-entrypoint-initdb.d/message.sql
networks:
- twotier
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-proot"]
interval: 10s
timeout: 5s
retries: 5
start_period: 60s
flask-app:
image: trainwithshubham/two-tier-flask-app:latest
container_name: flask-app
ports:
- "5000:5000"
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_DB: devops
depends_on:
- mysql
networks:
- twotier
restart: always
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:5000/health || exit 1"]
interval: 10s
timeout: 5s
Docker compose up Docker compose down
ubuntu@ip-172-31-22-77:~/projects/two-tier-flask-app$ docker system prune
ubuntu@ip-172-31-22-77:~$ docker rmi image_id
ubuntu@ip-172-31-22-77:~$ docker rmi $(docker images -aq)
ubuntu@ip-172-31-22-77:~$ docker compose down
ubuntu@ip-172-31-22-77:~$ docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
ubuntu@ip-172-31-22-77:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
two-tier-backend latest 482cfa3e3f79 2 days ago 391MB
mysql latest 5568fddd4f66 7 weeks ago 797MB
python 3.9-slim 1a47c1aaa88f 3 months ago 126MB
python 3.7 16d93ae3411b 20 months ago 994MB
openjdk 17-jdk-alpine 264c9bdce361 3 years ago 326MB
ubuntu@ip-172-31-22-77:~$ docker image tag two-tier-backend:latest vimleshverma16
ubuntu@ip-172-31-22-77:~$ docker image tag two-tier-backend:latest vimleshverma16/two-tier-backend:latest
ubuntu@ip-172-31-22-77:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
two-tier-backend latest 482cfa3e3f79 2 days ago 391MB
vimleshverma16 latest 482cfa3e3f79 2 days ago 391MB
vimleshverma16/two-tier-backend latest 482cfa3e3f79 2 days ago 391MB
mysql latest 5568fddd4f66 7 weeks ago 797MB
python 3.9-slim 1a47c1aaa88f 3 months ago 126MB
python 3.7 16d93ae3411b 20 months ago 994MB
openjdk 17-jdk-alpine 264c9bdce361 3 years ago 326MB
ubuntu@ip-172-31-22-77:~$ docker rmi vimleshverma16
Untagged: vimleshverma16:latest
ubuntu@ip-172-31-22-77:~$ docker push vimleshverma16/two-tier-backend:latest
The push refers to repository [docker.io/vimleshverma16/two-tier-backend]
9b06bd3f2bf0: Pushed
d45ad3879fc8: Pushed
6ff1940f34b1: Pushed
52283558fe76: Pushed
2345815b0277: Pushed
33a5f7ac2300: Pushed
70fff1cc441b: Mounted from library/python
62f5a620d172: Mounted from library/python
4a0e2c276a6a: Mounted from library/python
5f1ee22ffb5e: Mounted from library/python
latest: digest: sha256:768ba0da39d1dd6360f3f7443f874d0f24b13f73ba56307383b5357c43e7bdee size: 2416
ubuntu@ip-172-31-22-77:~$
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ cat Dockerfile-multi
# Build Stage
FROM python:3.9-slim AS builder
WORKDIR /app
# Copy dependency file first for efficient caching
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt --target=/app/deps
# Copy application source code
COPY . .
# Final Distroless Stage
FROM gcr.io/distroless/python3-debian11
WORKDIR /app
# Copy dependencies from the builder stage
COPY --from=builder /app/deps /app/deps
COPY --from=builder /app .
# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH="/app/deps"
# Expose the application port
EXPOSE 80
# Run the application
CMD ["run.py"]
Commands Only
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ docker build -t flaskapp:latest .
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ ls
Dockerfile Dockerfile-multi README.md app.py requirements.txt run.py
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ rm Dockerfile
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ mv Dockerfile-multi Dockerfile
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ docker build -t flask-mini .
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask-mini latest 2dd462ec4c9c 6 seconds ago 62.8MB
<none> <none> f72112c86d2b 12 seconds ago 136MB
flaskapp latest 9d6d2f58daa9 16 minutes ago 137MB
python 3.9-slim 1a47c1aaa88f 3 months ago 126MB
gcr.io/distroless/python3-debian11 latest db7c2faa8433 N/A 54.6MB
Running on http
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ docker build -t flask-mini .
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ docker run -d -p 80:80 flask-mini:latest
with 80 port
http://54.167.171.173:80
333e0a20a3fb829128a39e60ec55ca943fae38be92537eca0707f1b6c45ee464
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
333e0a20a3fb flask-mini:latest "/usr/bin/python3.9 …" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp unruffled_mayer
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ nohup docker attach 333e0a20a3fb &
[1] 4956
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ nohup: ignoring input and appending output to 'nohup.out'
ubuntu@ip-172-31-22-77:~/flask-app-ecs$
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ ls
Dockerfile README.md app.py nohup.out requirements.txt run.py
ubuntu@ip-172-31-22-77:~/flask-app-ecs$ cat nohup.out
101.0.63.45 - - [15/Mar/2025 17:17:21] "GET / HTTP/1.1" 200 -
101.0.63.45 - - [15/Mar/2025 17:17:29] "GET /health HTTP/1.1" 200 -
101.0.63.45 - - [15/Mar/2025 17:17:37] "GET /health/not HTTP/1.1" 404 -
101.0.63.45 - - [15/Mar/2025 17:17:42] "GET / HTTP/1.1" 200 -
101.0.63.45 - - [15/Mar/2025 17:17:42] "GET /favicon.ico HTTP/1.1" 404 -
ubuntu@ip-172-31-22-77:~/flask-app-ecs$