1.6. Frontend

Frontend

Let’s create a frontend to demonstrate port-forwarding and container connections. We will run a simple Python web server that displays all users in our MariaDB.

First, get the IP of the currently running MariaDB container. By default, all containers are started in the bridge network, where no DNS service is available, so we can’t use container names. As a workaround, we use the IP of the container:

export ip=$(docker inspect mariadb-container-with-existing-external-volume -f '{{ range.NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}')

docker inspect <container> shows details about a running container in JSON format (run it yourself to explore). We filtered the JSON output to get only the container’s IP address.

Alternatively, we could filter the output with grep, as in docker inspect mariadb-container-with-existing-external-volume | grep IPAddress, but our solution is more concise 😊.

Next, we’ll start the frontend container. Fortunately, an image is available online. If you’re interested, you can check the source code here :

docker run -d --name frontend -e username=peter -e password=venkman -e servername=$ip grafgabriel/container-lab-frontend

How do we access it? Try connecting to the server using the container’s Docker-assigned IP address:

docker inspect frontend -f '{{ range.NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}'

This will show only the IP of the container as output:

172.17.0.4

Since we don’t have a browser in the web shell, use curl http://172.17.0.4:5000 to view the page in your terminal. On a local installation, you could simply open http://172.17.0.4:5000 in your browser.

curl http://172.17.0.4:5000

We successfully implemented a frontend for our backend.

Port forwarding for your Docker container

Instead of using the IP of the container we could also have used dockers port-forwarding feature. Docker is able to forward any port you specify to your local machine by using -p <host-port>:<container-port>.

Let us add this option to our frontend container:

docker stop frontend
docker rm frontend
docker run -d --name frontend -e username=peter -e password=venkman -e servername=$ip -p 5000:5000 grafgabriel/container-lab-frontend

If you take a look into docker ps you’ll find an interesting change for the PORTS column

docker ps -l

Now try to curl directly to localhost:

curl http://localhost:5000

Our frontend is now available also on localhost.