Skip to main content

Docker Networking

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Beginner

You are developing a new containerized application for a client. The application will consists of two containers: one for the frontend application, and one for the database. Your client has security concerns about the database, and they want it to run on a private network that is not publicly accessible. You will need to create two networks. One will house the frontend application that is publicly accessible, and the other network, which is flagged as internal, is where the database will reside. Create a MySQL container connected to the private network and a Nginx container that is connect to both networks.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Docker Networking

Scenario

We're developing a new containerized application for a client. The application will consist of two containers: one for the frontend application, and one for the database. Our client has security concerns about the database, and they want it to run on a private network that is not publicly accessible.

So, we'll need to create two networks. One will house the frontend application that is publicly accessible, and the other network, which is flagged as internal, is where the database will reside. We have to create a MySQL container connected to the private network and an Nginx container that is connect to both networks.

Get logged in

Use the credentials and server IP in the hands-on lab overview page to log into the server with SSH.

Create the frontend network

We're going to create a bridge network called frontend that will be publicly accessible:

[user@host]$ docker network create frontend

Create the localhost network

Now we've got to create a second bridge network called localhost that will be internal:

[user@host]$ docker network create localhost --internal

Create a MySQL container

We need a database, so we'll deploy a MySQL container called database that uses the localhost network, and runs in the background. We'll use the mysql 5.7 image:

[user@host]$ docker container run -d --name database 
 --network localhost  
 -e MYSQL_ROOT_PASSWORD=P4ssW0rd0! 
 mysql:5.7

Create an Nginx container

Next, we've got to deploy a second container call frontend-app and publish port 80 on the host to port 80 on the container. The container will be attached to both of the frontend networks. We'll use the latest Nginx image, and set the container should run in the background:

[user@host]$ docker container run -d  
 --name frontend-app 
 --network frontend  
 nginx:latest

Connect frontend-app to the internal network

Now that the Nginx container is created, and is connected to the internet, we've got to connect it to the localhost network. To make sure both containers are running, run a quick docker container ls. Once we're sure they're up, we can do the actual connecting:

[user@host]$ docker network connect localhost frontend-app

Now let's peek at the container frontend-app:

[user@host]$ docker container inspect frontend-app

Conclusion

A lot flies by, but in the output, we'll see (In the Networks section) that it's connected to both the frontend and localhost networks. This means we're done. Congratulations!