Connecting multiple containers using Docker network

Ewere Ebie
5 min readMay 11, 2023

In addition to isolating applications, Docker also offers a means to streamline container communication through the use of Bridge networks. When you create a user-defined bridge network, containers within the network can resolve each other by aliases, similar to DNS names. This functionality enhances the ease of communication between containers within the Docker environment.

In this article, we will demonstrate how to set up two containers: one for our full-stack application, Bambi Event Manager, and another for a Postgres database. Let’s get started! 😤.

Prerequisites

To complete this lesson you should have

Step 1 — Pull down application

Open your terminal on the folder you wish to have this repository and pull down the repo with the command

git clone https://github.com/caleb-42/event-manager-node.git

once it’s complete open the folder with your code editor.

Step 2 — Create Docker bridge network

docker network create event-manager-network

When you run this command it will create a bridge network that will enable us link our database container to the event-manager-app container when they are created.

Step 3 — Setup Postgres docker image & volume

To create our database we will use a Postgres image from Dockerhub.

docker pull postgres:14.1-alpine

Before creating a Postgres container with this image we need to create a file at the root of our project called db_env. This file will hold our environment variables.

POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_DB=event_manager

Now your folder structure should look this way. Pay close attention to the db_env and db.sql files as they are key to setting up our Postgres container.

To make our container data persistent we need to create a volume using the command below.

docker volume create db

Step 4 — Create Postgres database container

docker run -d -p 5434:5432 --name postgres-db --network event-manager-network --env-file ./db_env -v db:/var/lib/postgresql/data -v $PWD/db.sql:/docker-entrypoint-initdb.d/db.sql postgres

The command above will not only create a Postgres container but will map the database instance to port 5434 on our host so we can view it with a postgres client. We gave our container a name “postgres-db” and added it to the event-manager-network.

We passed environment variables from our env file to the container in order to define the Database name, user and password. The postgres data of the container was mounted to the db volume we created in order to persist data. And Lastly we mounted our db.sql initialisation script to the docker-entrypoint of our Postgres container so it runs the moment postgres-db starts. This is how our database should look on a client

Step 5 — Create docker file for event manager

Create a file titled “Dockerfile at the root folder of your project and populate it with the following content.

FROM node:16.0.0-alpine
WORKDIR /app
COPY ./package.json ./
RUN yarn install --pure-lockfile
COPY ./ ./

EXPOSE 3210

CMD ["yarn", "start"]

This file will enable us generate an image that will be used to create a container for our application. For more details check out this article.

Step 6 — Create .env file using env-sample file

There is a file named env-sample in the project directory. Use its content to create a .env file for the event application.

PORT=3210
NODE_ENV=development
DATABASE_URL=postgres://postgres:password@postgres-db/event_manager
JWT_SECRET=secret

Get API_KEY value by signing up to https://generated.photos. You can also leave it empty because it’s not mandatory. The DATABASE_URL is the most important variable and its a connection string to our database. We used the host name postgres-db because when we add containers to the same network their given names serve as domain names for communication.

Step 7 — Build docker image for event manager

docker build . -t event-manager-image

Run the above command to create an image and if yours completes successfully like mine you should see an output like the one below.

Step 8 — Run event manager docker container

docker run -p 3000:3210 --name event-manager-app --network event-manager-network --env-file ./.env event-manager-image

We named our app event-manager-app and added it to the event manager network which also contains the postgres-db database app. We also passed the env file we created to set the environment variables of our app.

Once the container is created you should see the output above stating our server is listening on port 3210 but if you try to get to localhost port 3210, The app will not display. Why? 🤔

Thats because we mapped our container port 3210 to display on port 3000 on the host machine while running our container start command. You could as well have used the same port on the host, but I did this to make clear the difference between them. So the right url to visit would be http://localhost:3000 and Viola!!!

Conclusion

After some effort, we successfully created bridge networks — hurray! But lets face it, relying solely on plain Docker commands for running multiple connected containers can be quite cumbersome. In my next article we’ll simplify container management using Docker compose. Caio 👋

I’m glad you took the time to read my blog. If you liked it leave a clap and follow my medium account for more content like this. Thanks 👋.

--

--

Ewere Ebie

I write because it’s less exhausting than speaking. And its fun