Portainer

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
    

DOCKER NETWORKING

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.

  • Run the command docker exec moby-counter ping -c3 redis inside our application container (moby-counter). What is the displayed IP address?
  • Similarly, display the contents of the /etc/hosts files in the container (use the `cat` command coupled with `docker exec`). We observe that Docker automatically configured the external IP of the container we're in with the container ID. Also, display /etc/resolv.conf: the DNS resolver has been configured by Docker. That's how the container knows the IP address of Redis. To ensure this, query the DNS server of our moby-network network by running the command docker exec moby-counter nslookup redis 127.0.0.11.
  • Create a second network moby-network2.
  • Create a second instance of the application in this network: docker run -d –name moby-counter2 –network moby-network2 -p 9090:80 russmckendrick/moby-counter.

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:

  • Let's create a second Redis with the same domain: docker run -d –name redis2 –network moby-network2 –network-alias redis redis:alpine.
  • When you ping Redis from this new application instance, what IP do you get?
  • Retrieve the local nameserver's IP address for moby-counter2 as before.
  • Then run `nslookup redis <nameserver_ip>` inside the moby-counter2 container to test DNS resolution.
  • You can find the network configuration and the containers linked to it with `docker network inspect moby-network2`. Note the IPAM (IP Address Management) section.
  • Stop our containers: docker stop moby-counter2 redis2.
  • To quickly clean up stopped containers, run `docker container prune`.
  • Similarly, `docker network prune` cleans up networks no longer used by any containers.
teaching_assistant/workflow/docker_networking.txt · Last modified: 2024/05/15 09:55 by Ralph
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0