Skip to content

1.4 Docker Networking

1. Fundamentals of Networking

Play

Docker Networking

2. Linking Containers

2.1 Using a Bridge Network

To link two Redis containers in Docker, where one acts as the database and the other as a command-line interface (CLI) client, you can use Docker’s network features to allow the containers to communicate with each other. Here’s a step-by-step guide:

1. Create a Docker Network: This will allow the containers to communicate with each other.

2. Run the Redis Database Container: Start the Redis database container and attach it to the network.

3. Run the Redis CLI Container: Start another container with the Redis CLI, also attached to the same network, and connect it to the Redis database.

Step 1: Create a Docker Network

First, create a custom bridge network:

Terminal window
docker network create redis-net

Step 2: Run the Redis Database Container

Run the Redis database container on the created network:

Terminal window
docker run -d --name redis-db --network redis-net redis

Here, -d runs the container in detached mode, --name redis-db names the container, and --network redis-net attaches the container to the redis-net network.

Step 3: Run the Redis CLI Container

Run another container for the Redis CLI, connecting it to the database container:

Terminal window
docker run -it --rm --network redis-net redis redis-cli -h redis-db

In this command:

  • -it makes the container interactive and attaches a terminal.
  • --rm removes the container once it exits.
  • --network redis-net attaches the container to the redis-net network.
  • redis is the image name.
  • redis-cli is the command to run the Redis CLI.
  • -h redis-db specifies the hostname of the Redis server to connect to, which is the name of the database container.

With this setup, the Redis CLI container will connect to the Redis database container. You can then run Redis commands interactively.

Example Command

Once inside the Redis CLI, you can run commands like:

Terminal window
SET mykey "Hello"
GET mykey

This setup isolates the containers and ensures they can communicate securely within the same network.

To link two Docker containers without using a Docker network, you can use the --link option. Following steps summarizes steps to link a Redis database container and a Redis CLI container using the --link option:

Step 1: Run the Redis Database Container

First, run the Redis database container:

Terminal window
docker run -d --name redis-db redis

Next, run the Redis CLI container and link it to the Redis database container:

Terminal window
docker run -it --rm --link redis-db:redis redis redis-cli -h redis

In this command:

  • -it makes the container interactive and attaches a terminal.
  • --rm removes the container once it exits.
  • --link redis-db:redis creates a link from the current container to the redis-db container and gives the alias redis for connecting to it.
  • redis is the image name.
  • redis-cli is the command to run the Redis CLI.
  • -h redis specifies the hostname (alias) of the Redis server to connect to, which corresponds to the alias defined in the --link option.

This setup will allow the Redis CLI container to connect to the Redis database container using the alias redis without needing to specify an IP address or custom network.

Step 2 (Alternative)

First create a redis container (for the CLI) with the link to redis-db, run bash command at the start.

Terminal window
docker run --rm -it –-link redis-db:redis redis /bin/bash

at the bash prompt (inside the container), run the command redis-cli to start the redis cli, connecting to the database running at port 6379 at the server host redis (alias which is given with the --link option)

Terminal window
root@309-402356254:/data# redis-cli -h redis -p 6379