Assigning ports to containers in docker

Assigning ports to containers in docker 1

the containers we create should be accessible to the outer world so we port containers to our main ip

the default port of apache webserver is 80 so we can use the below command to port
docker run -it -p <port>:80 imagename
for example, let us create an apache image

1.make a directory name with  name abc

mkdir abc

2.go into the abc dirctory

cd /abc

3.create a docker file

nano dockerfile

4.paste the below code

  FROM ubuntu:16.04
LABEL maintainer="[email protected]"

RUN apt-get update \
   && apt-get install -y apache2

COPY html/* /var/www/html/

WORKDIR /var/www/html
CMD ["apachectl", "-D", "FOREGROUND"]
EXPOSE 80

5.run the build command

docker build .

6.give the port for the server ip

docker run -it -p 8080:80 <imagename>

7.now open your browser and trigger http://yourip:8080

Default image
Vishal Devxo
Vishal Devxo is a DevOps engineer and a Backend developer, he spends all his time for creating good tutorials with better visuals and blogging, developed some projects based on Python-Django, some hacking modules and scripts in python