1.4 Docker Networking
1. Fundamentals of Networking
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:
docker network create redis-netStep 2: Run the Redis Database Container
Run the Redis database container on the created network:
docker run -d --name redis-db --network redis-net redisHere, -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:
docker run -it --rm --network redis-net redis redis-cli -h redis-dbIn this command:
-itmakes the container interactive and attaches a terminal.--rmremoves the container once it exits.--network redis-netattaches the container to theredis-netnetwork.redisis the image name.redis-cliis the command to run the Redis CLI.-h redis-dbspecifies 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:
SET mykey "Hello"GET mykeyThis setup isolates the containers and ensures they can communicate securely within the same network.
2.2 Link without Creating Docker 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:
docker run -d --name redis-db redisStep 2: Run the Redis CLI Container with Link
Next, run the Redis CLI container and link it to the Redis database container:
docker run -it --rm --link redis-db:redis redis redis-cli -h redisIn this command:
-itmakes the container interactive and attaches a terminal.--rmremoves the container once it exits.--link redis-db:rediscreates a link from the current container to theredis-dbcontainer and gives the aliasredisfor connecting to it.redisis the image name.redis-cliis the command to run the Redis CLI.-h redisspecifies the hostname (alias) of the Redis server to connect to, which corresponds to the alias defined in the--linkoption.
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.
docker run --rm -it –-link redis-db:redis redis /bin/bashat 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)
root@309-402356254:/data# redis-cli -h redis -p 6379