Bind Host Port to container PortΒΆ

Make a docker file directory

$ mkdir docker-file

Make a docker file

$ cd docker-file/
$ touch Dockerfile

Add content to docker file

$ cat > Dockerfile
FROM ubuntu:14.04
MAINTAINER abc "abc@abc.com"
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Our first Docker image for Nginx' > /usr/share/nginx/html/index.html
EXPOSE 80

Ctrl+D to save the changes

Create your own image

  1. Build a docker image
$ sudo docker build -t="test/test_nginx_image" .

You will see output as shown below

Sending build context to Docker daemon 2.56 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:14.04
---> 9cbaf023786c
Step 1 : MAINTAINER abc "abc@abc.com"
---> Using cache
---> 912452fdbe6d
Step 2 : RUN apt-get update
---> Using cache
---> d711127e4d76
Step 3 : RUN apt-get install -y nginx
---> Running in 4fab72b24686
Processing triggers for libc-bin (2.19-0ubuntu6.3) ...
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
---> b9f58e96b137
Removing intermediate container 4fab72b24686
Step 4 : RUN echo 'Our first Docker image for Nginx' > /usr/share/nginx/html/index.html
---> Running in 1d1702c4dae4
---> c46b140fd8ad
Removing intermediate container 1d1702c4dae4
Step 5 : EXPOSE 80
---> Running in a98f7685870a
---> 728d805bd6d0
Removing intermediate container a98f7685870a
Successfully built 728d805bd6d0
  1. Check that image has been created
$ docker images | grep nginx
test/test_nginx_image latest 1 day ago 2.433 MB
  1. Run the newly created test-ngnix_image with port binding
$ sudo docker run -d -p 8080:80 --name test_container test/test_nginx_image nginx -g "daemon off;"
356389b43b02c5afb55de8145cb33a3e6539c671a97c2c6974a6308f1d7bac8d

The -p 8080:80 option will bind the host port 8080 to the container port 80. So we will be able to see the default web page of “Our first Docker image for Nginx” by simply visting the IP address of our docker host.

$ curl http://docker-host-ip:8080
Our first Docker image for Nginx