Earlier we seen the basic commands to work with docker which gives a better idea to download,run,stop and delete a docker container. Let us go little deep like how to access an application which are running inside a container.

Docker Container:

    Docker containers are advanced OS level virtualization which runs software application from the guest operating system.

    To explain this we will run a web-server inside a container and try to access the web-server from our host machine. i.e Running an OS image which runs a web-server such as Apache or nginx and we will be accessing that web-server.

    The web-server is already created for this demo and hosted on my registry - jhony9/test-site.

    A registry is like an account in Docker Hub where the docker images are stored so it would be easy for us to get the required image. Click here to see some of my images.

    We will download and run the image directly in one go using docker run.

$ docker run -it jhony9/test-site sh

    The above command will pull the image and creates a container and starts the webserver.

jhony@ljlinux:~$ sudo docker run -it jhony9/test-site sh
* Starting Apache httpd web server apache2
root@9dc524182244:/var/www/html# pidof apache2
39 38 35
root@9dc524182244:/var/www/html# exit

Fair well the web-server is running but how to access it from our host machine??? and if we exit the container the web-server will be stopped.

    For this we need to run the container with detached mode and ports and a name for our container.

    The flag -d is used to run the container without logging into it. This mode is called detached mode.

    The flag -P randomly selects ports for our web-server which helps to access it on our localhost.

    --name will have a name for the container.

    The command will be as follows,

$ docker run -d -it -P --name test-site jhony9/test-site

    This one creates a hash for the container.

jhony@ljlinux:~$ sudo docker run -d -it -P --name test-site jhony9/test-site

Finding the ports:

    The web-server is started automatically. Now we need to access it,

    As i told the -P flag will randomly assign ports to the web-server. For finding the ports we can use docker port name_container command.

$ sudo docker port Name_of_Container

jhony@ljlinux:~$ sudo docker port test-site
443/tcp ->
80/tcp ->

    Access the web-server from localhost by port number, or http://localhost:32771.

Defining a Custom port:

    If we want to run the webserver in a custom port then we need to use -p flag with port number. The below command runs the container on the given port.

$ docker run -d -it -p 8181:80 --name test-site1 jhony9/test-site

sudo docker run -d -it -p 8181:80 --name test-site1 jhony9/test-site

jhony@ljlinux:~$ sudo docker port test-site1
80/tcp ->

    Now access the web-server from localhost by the defined port number, or http://localhost:8181.

Stopping the container:

    The command docker stop container_name will stop the container.

$ docker stop Name_of_Container

jhony@ljlinux:~$ sudo docker stop test-site

    That's all folks for a running an application inside a container. On the upcoming article we will see about docker image. Feel free to ask if you have any questions.


  1. Be the first to add a comment.