Mar 3, 2023

Docker - working steps | Part-4 Port expose

What is Docker Expose Port?

The expose keyword in a Dockerfile tells Docker that a container listens for traffic on the specified port. So, for a container running a web server, you might add this to your Dockerfile.

Assume, your web application running on one container with specific port. But when any applucation accessed from outside world, first connection comes to host machine then it ll go the container. So you nned to expose a port to container to access your web application port.

# mkdir app_dir
# cd app_dir/
docker run -it --name webserver -p 8080:80 ubuntu /bin/bash
root@080e8df03b2c:/# 
app_dir]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                  PORTS                                   NAMES
080e8df03b2c   ubuntu         "/bin/bash"   55 seconds ago   Up 54 seconds           0.0.0.0:808o->80/tcp, :::8080->80/tcp   webserver


Note:
port mapping:
<custom-port(host-machne)> : <actual-port(container)>

Lunching one more container with detach mode:

# docker run -td --name webserver1 -p 8081:80 ubuntu
0aa9f6d250c4f224fbd60cc6bc33dd16a00b01c5b9360bbd257243ac96e57290

# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                  PORTS                                   NAMES
0aa9f6d250c4   ubuntu         "/bin/bash"   56 seconds ago   Up 55 seconds           0.0.0.0:8081->80/tcp, :::8081->80/tcp   webserver1
080e8df03b2c   ubuntu         "/bin/bash"   9 minutes ago    Up 9 minutes            0.0.0.0:8080->80/tcp, :::8080->80/tcp   webserver
here, host (0.0.0.0:8081) forwardig port to 80.


we can verify also using docker port command:

[root@ip-172-31-38-12 app_dir]# docker port webserver1
80/tcp -> 0.0.0.0:8081
80/tcp -> :::8081

Let us attach the container and create a web page and access it. install nginx to access webpage

root@0aa9f6d250c4:/# apt-get update -y
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
...
Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [22.4 kB]
Fetched 26.1 MB in 7s (3668 kB/s)
Reading package lists... Done

root@0aa9f6d250c4:/#

let us install web application. example - ngnix.

root@0aa9f6d250c4:/# apt-get install nginx -y

-- check the nginx version

root@0aa9f6d250c4:/# nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
root@0aa9f6d250c4:/#
root@0aa9f6d250c4:/# which nginx
/usr/sbin/nginx
root@0aa9f6d250c4:/# cd /var/www/html
root@0aa9f6d250c4:/var/www/html# pwd
/var/www/html
root@0aa9f6d250c4:/var/www/html# cat > index.html
Hello world, this is nginx web container running on ec2 instance

-- restart nginx
root@0aa9f6d250c4:/var/www/html# service nginx restart

Now, you can access the web page from nginx web server.

http://54.169.93.29:8081

Note: you need to allow 8081 port to ec2 machine security group.
after adding this port to security group, webpage is accessible.






It works!!!

similarly you can run many applications in different containers with different ports.

No comments:

Post a Comment

Translate >>