This page addresses the challenge of enabling communication between containers, particularly in the context of running tests within a pipeline involving a custom language image container and other service containers like Postgres or Redis. It discusses the problem, key concepts about Docker containers, and proposes a solution using Docker networking.
Problem Statement
Let's set the stage. You're running your code in a custom language image container within a pipeline. Everything is going smoothly until you need to run tests that depend on a Postgres database or a Redis instance. The solution seems simple enough: launch additional containers for these services. However, how do you establish communication between your application container and these database or Redis containers? This is the crux of the problem.
Key Concepts
Before we dive into the solution, it's essential to understand a few key concepts about Docker containers:
Isolation by Default: Docker containers are inherently isolated from the host and other containers. This isolation ensures that they don't interfere with each other or with the host system.
Host Networking: Docker containers can be configured to use the host network, essentially sharing the host's network namespace. However, in our problem statement, both the app container and the DB/Redis container are running on the same host, making host networking less relevant.
Solution - Docker Networking
The solution revolves around leveraging Docker's networking capabilities to enable communication among containers running on the same host. To facilitate communication among these containers via localhost, we link the app container and utility containers within the same Docker network. Here's how it works:
Note
All relevant containers, including DB/Redis, must be part of the same network for seamless communication. This approach ensures effective interaction between test scripts and the required services encapsulated within containers.
Create a Common Network: To enable communication between your app container and the DB/Redis containers, you need to create a common network. Containers that are part of the same network can easily talk to each other.
Export Container ID: Retrieve and export the application container's Container ID as an environment variable for reference in subsequent steps.
export CONTAINER_ID="$HOSTNAME"
Connect to the Network: Utilize the Docker socket from the pipeline step to connect the application container to the common network:
docker network connect mynetwork "$CONTAINER_ID"