If you have already created the Portainer container, you can restart it by running docker start portainer. Otherwise, create it as follows:
docker volume create portainer_data docker run --detach --name portainer \ -p 9000:9000 \ -v portainer_data:/data \ -v /var/run/docker.sock:/var/run/docker.sock \ portainer/portainer-ce
To experiment with networking, we will launch a small example Node.js application (moby-counter) that works with a Redis queue (like a database but for storing simple key/value pairs). Let's fetch the images from Docker Hub:
docker image pull redis:alpine docker image pull russmckendrick/moby-counter
Run the command ip a | tee /tmp/interfaces_before.txt to list your network interfaces and write them to the file.
To connect the two applications, let's manually create a network:
docker network create moby-network
Docker implements these virtual networks by creating interfaces. Run the command `ip a | tee /tmp/interfaces_after.txt` and compare (`diff /tmp/interfaces_before.txt /tmp/interfaces_after.txt`). What has changed?
Now, let's launch the two applications using our network:
docker run -d --name redis --network moby-network redis:alpine docker run -d --name moby-counter --network moby-network -p 80:80 russmckendrick/moby-counter
Visit the page of our application. What do you think? Moby is the Docker mascot 🐳 😊. Create a recognizable pattern by clicking.
How does our application connect to the Redis container? It uses these JS instructions in its server.js file:
var port = opts.redis_port || process.env.USE_REDIS_PORT || 6379; var host = opts.redis_host || process.env.USE_REDIS_HOST || "redis";
In summary, by default, our application connects to the Redis host with port 6379.
Let's explore our Docker network a bit.
When you ping Redis from this new instance moby-counter2, what do you get? Why? You cannot have two containers with the same names, as we have already discovered. However, our second network operates completely isolated from our first network, which means we can still use the redis domain name. To do this, we need to specify the –network-alias option: