Docker Notes-5
Docker Notes-5
We know that container does not have any public ip of its own….
So if the application is running inside the container how we can
access it then
Press ctrl p and ctrl q to come out of the container without stopping
the container
Go to your instance
Copy the public ip of the machine
# Docker files
It is text file which consist of set of instructions
And it automates the docker image creation
Case study:
Lets suppose you are working for Netflix ..and you are devops
engineer…there is an application you need to deploy for netflix…
Now problem which you are facing is that app requires more then
100 dependencies and alot of environment variables need to be
passed when the container was created. You went to Developer
and how to configure so many things inside the container ..
So developer said : let me a dockerfile…which will consist of app,
base image, dependencies , and everything required by the
application to run inside the container..after the developer shared
you that dockerfile you simply executed the dockerfile to create a
custom image…and from the custom image you created n number
of containers.
How to write the dockerfile ?????????????????
Dockerfile is made of key-component system
FROM -> For the base image the command need to be on the top
of the dockerfile
E.g
MAINTAINER akshat<[email protected]>
MAINTAINER akshatgupta
COPY -> COPY the files from local system (Docker virtual
machine/EC2) . For example: lets say there is a index.html file in
your machine …and when the container is getting created you want
to copy that file inside the container in this case you will use COPY
ADD -> Similar to copy…it can copy the file from your machine to
container but can also download the file from internet into your
container
E.g
ADD /home/ubuntu/index.html /var/www/html/index.html
EXPOSE -> It exposes the port of the container like port 80 if your
app is running over internet ….or port 8080 if you are creating
jenkins container ….or port 3306 for mysql…
If you dont put expose then also you will be able to do port
expose…but since the devops engineer they might not aware on
which port the developer has configured the application…if expose
is there it makes it easier devops engineer to deploy the application
Eg
ENV name=akshat
CMD -> Executes the command during the container creation . Lets
say if we want to start a software then we will use CMD
### Practicals
vi Dockerfile
(always remember the Dockerfile name is always Dockerfile with D
capital)
ls
### lets suppose we have installed apache ….we need to start
apache as well…automatically after the container is created
What would be our approach to start the apache during creation of
the container?
vi Dockerfile
FROM ubuntu
RUN echo "hello world" >testfile.txt
RUN touch myfile.txt
RUN apt update
RUN apt install apache2 -y
CMD ["apache2ctl", "-D", "FOREGROUND"]
docker build -t mysecondimg .
docker images
(Here dt means detached terminal which means you will not enter
inside the container….and you will not use /bin/bash as well
because we want to execute apache2ctl (CMD) rather then
bin/bash while executing the container)
vi Dockerfile
FROM ubuntu
RUN echo "hello world" >testfile.txt
RUN touch myfile.txt
RUN apt update
RUN apt install apache2 -y
ENTRYPOINT ["apache2ctl", "-D", "FOREGROUND"]
docker images
vi index.html
Press i to start inserting
Hello this is the file shared by developer
Press :wq to save and quit
vi Dockerfile
FROM ubuntu
RUN echo "hello world" >testfile.txt
RUN touch myfile.txt
RUN apt update
RUN apt install apache2 -y
COPY index.html /var/www/html
ENTRYPOINT ["apache2ctl", "-D", "FOREGROUND"]
Press esc :wq
Select the machine -> security group -> click on security group ->
edit inbound rule -> add new rule -> add port 8099
Save
(if sometimes you get error here…it might be that you are accessing
the application via https
E,g:
http://13.126.192.54:8099/
If we use https://13.126.192.54:8099/
Because we dont have SSL certificate which is paid certificate
…some free are also there but they are not much secured)
vi Dockerfile
FROM ubuntu
MAINTAINER akshat<[email protected]>
WORKDIR mydirectory
ENV name=akshat
RUN echo "hello world" >testfile.txt
RUN touch myfile.txt
RUN apt update
RUN apt install apache2 -y
COPY index.html /var/www/html
ENTRYPOINT ["apache2ctl", "-D", "FOREGROUND"]
###################
PRUNE IN DOCKERS
👍
volumes, or network from the local storage.
It uses the simple syntax
## DOCKER COMPOSE
In cases when we are working with application with alot of
functionality we have separate teams managing each functionality
and have there own dockerfiles…lets suppose in flipkart case study
we have 20+ dockerfiles…now if we want to deploy flipkart we need
to execute all the 20+ dockerfiles
Suppose later on company wants us to migrate the application to
other cloud…again we need to run all the dockerfiles…and if by
chance we forgot to run any dockerfile we will get errors.
READING MATERIAL :
https://drive.google.com/file/d/1YW1OpRPGUsh27brU8ixZKzraOmi
ccZsU/view?usp=drive_link
FOR DOCKER COMPOSE WE WILL CREATE A NEW EC2
MACHINE WITH UBUNTU 22.04 AMI
sudo su
apt update
apt install docker.io -y
apt install docker-compose -y
apt install git -y
git clone https://github.com/akshu20791/docker-compose-lab-01
(this is a multitier application with frontend, API and backend )
cd docker-compose-lab-01
cd api
vi index.php
(update the line 4 with the public ip of your machine)
cd ..
cd frontend
vi index.html
(update line 12 with the public ip of the machine)
docker-compose up -d
(This will take my docker-compose up)
Click on browse
################
# DOCKER NETWORKING : WAY BY WHICH THE CONTAINERS
TALK TO EACH OTHER
There are several drivers available by default , providing core
network functionality
# LAB
docker network ls
Lets create one more container …and from that container we will
see that if we are able to access the server1 container or not
##Now lets create our own custom network with the name
akshatothernet
docker network ls
docker network create akshatothernet
docker network ls
apt update -y
apt install iputils-ping -y
Press ctrl c
ping server1
USE CASE 👍
1) ISOLATION : In some cases you want to run a Docker
container in a compute isolation from the network
2) Security : For security you might want to perform tasks in
isolated environment like malware analysis or testing.
hostname -i
ping google.com
See we are not able see the ip of the container in none network
…also we are not able to ping google.com
###############
Docker Hub simplifies the storage, management, and sharing of Docker images,
making it easy to organize and access container images from anywhere. Enhanced
Security: It runs security checks on images and provides detailed reports on potential
vulnerabilities, ensuring safer deployments
Lets say we want to deploy the containerized app in client machine
PROCESS:
>We will first create a custom image of our container
>We will then push the image to hub.docker.com (it could our own
dockerhub account we can later give collaboration access to client
or we can request the client to give authentication token of there
dockerhub account)
>client will now pull the image and then deploy the container
exit
Now we want to create a custom image of mywebapp container
Account settings
Security
New access token
We will copy these commands and put in machine
(when you put the passwork nothing will come so copy paste
wisely) (password is the token generated previously)