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 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
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