Docker port expose
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
In such cases we use the concept of port export where we can
expose a port of the container with port of the machine …now if we
open publicip:portofmachine we will be able to access the
application running inside the container
## lets say we have an ubuntu container with a specific port
exposed
docker run -it --name 29juncon -p 8902:80 ubuntu /bin/bash
#in this way we will create a container with name 29juncon with port
8902 of machine (ec2/virtual machine/instance) exposed with port
80 of the container
apt update -y
apt install apache2 -y
service apache2 start
cd /var/www/html
#default location of apache where whatever files i will upload would
be accessible from net
rm index.html
apt install git -y
git clone https://github.com/akshu20791/apachewebsite .
Press ctrl p and ctrl q to come out of the container without stopping
the container
You can see your container is running:
docker ps
### THE FIREWALL OF THE MACHINE (SECURITY GROUP ) OF
THE MACHINE WILL NOT ALLOW ME TO ACCESS THE PORT
8902 from outside the network
We need to enable the inbound rule 8902 of the machine
Edit inbound rule
Add rule
Save
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 FROM ubuntu
FROM alpine
FROM mysql
RUN -> To execute some command . It will create the layer of
image
E,g
RUN apt update
RUN apt install apache2 -y
RUN touch file1
RUN echo “hello world”> myfile.txt
MAINTAINER -> Author /ownerr of the dockerfile. For
documentation purpose this will help .
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
COPY sourcelocation destinationincontianer
E.g COPY /home/ubuntu/index.html /var/www/html/index.html
(it will copy the index.html from the machine present in home
ubuntu to /var/www/html )
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
E.g EXPOSE 3306
EXPOSE 80
WORKDIR -> Set the working directory for the container…which
means lets say you set the
WORKDIR /akshat
RUN touch file1
This file1 would be created in akshat directory inside the container
ENV -> Environment variables …to set some environment variables
which might be utilized by my application
Eg
ENV name=akshat