How to access a web application running on a docker container from a web browser or ssh

Here is our scenario. We have deployed our web application as a docker container in our remote ubuntu VM. We will access the web app on the docker container from our machine and the host system(remote vm).
As a first step, we will need to perform the port mapping.
Port Mapping
To access the webapp from outside the docker container, we need to map the docker container port with a port on the host machine. So if the port on which our webapp is deployed in docker container is 8080, we need to map the port 8080 with a available port on the host machine.
If we are using bash script or ssh the command for port mapping will be
docker run - p 8005:8080 . mywebapp
where mywebapp is a web application created on tomcat server from Dockerfile
If you are using docker compose, you should ensure that port mapping is available for the web application service, similar to the following script.
services:
mywebapp:
build: .
ports:
- "8005:8080"
Access the web application deployed on docker over http and ssh
Now we will discuss the following ways to access this web application
- From web browser over the internet
- From web browser on the host VM/machine
- Via ssh on host VM/machine
- Via ssh on host VM using docker container ip
- Via ssh on docker container
Access the web app from web browser over the internet
Once our application is deployed, we can access the application from our laptop over the Internet. In the browser we will type the public ip of the host vm and the port number on which we have mapped the app on the host vm followed by the app name itself.
The port on which the app is mapped should be open for inbound requests.
For our example, our url will be something like this
http://<public ip of host vm>:8005/mywebapp
Access the web app from web browser on the host VM
This will also be similar to the url mentioned in previous section, however as we are on the host machine itself, we can also access the web app via ip or even the localhost.
Type the url of the host machine and the port on which the web app is mapped on the host VM.
http://localhost:8005/mywebapp
Or
http://<ip of host vm>:8005/mywebapp
Access the web app with ssh from host VM
If the only access that we have to the host vm is via ssh, then we need to install a text browser to view the web app
For our example, we will use links2 as the text browser. You can use another one if you already have that installed.
We have ubuntu 20.04 installed on the host vm. We will use the following commands to install links2 on the host vm
apt-get update
apt install links2
Once links2 is installed, we will use ssh command to open the url
links2 http://localhost:8005/mywebapp
Or
links2 http:<ip of host vm>//:8005/mywebapp
Access the web app over ssh with ip of docker container from host machine
If you want to access the web app hosted in a docker container, then you should first get the ip address of the docker container.
To get the container id run the ps command
docker ps
Then run the inspect command with the container id of the docker container
docker inspect
In the output that follows, look for “IPAddress”: “”
Use this ip address to access web app from the host machine
links2 http://<ip address of docker container>:8080/mywebapp
Access the web app with ssh from within docker container
Last but not the least, accessing the app from docker container
For this you have to ensure the browser is installed within the container.
We have run the following commands to install the links2 browser on our docker container
apt-get update
apt install links2
The command we will run will be similar to the one we were running from the host vm. The only difference will be that we will use the exec command with –it switch to run the command in the docker container in interactive mode
docker run exec –it links2 http://localhost:8080/mywebapp
Or
docker run exec –it links2 http://<ip of docker container>:8080/mywebapp
Conclusion
We have discussed various options to access the same application deployed on a docker with various different ways.
Let us know if you found this post useful.