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
a1325b27c09eea18a8cb4e000d91ee048832a7e4d8ab734d728315e78b0b3a76

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 -> 0.0.0.0:32770
80/tcp -> 0.0.0.0:32771

    Access the web-server from localhost by port number, http://127.0.0.1:32771 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
76cfd11698b4cb0867f989f78e7b727067095fdea1e5aa2a1c72223f24d98761

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

    Now access the web-server from localhost by the defined port number, http://127.0.0.1:8181 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
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.

Comments

  1. Be the first to add a comment.