API calls between docker instances
By: Stein Janssen
Tags: Docker Compose, Docker, Software Development, Network

Running applications in docker can be a good idea. However, it can take some time to configure it properly and you might run into certain issues. When I first used docker locally I couldn’t get multiple instances to communicate with each other. So I decided to write a small article about it.

For this example, we will set up 2 projects. An API and a client which will do a curl call to the API.

Prerequisites

Have docker installed

Have docker-compose installed — https://docs.docker.com/compose/install/#install-compose

Creating the API

Create a Dockerfile at the root of the project, for this example, we will use Node, but of course, you can use anything you like.

at the root of the project, for this example, we will use Node, but of course, you can use anything you like. Open the file and add the following:

FROM node:12-alpine WORKDIR /var/www CMD node src/index.js

This will download the node:12-alpine image from docker hub, set the current folder to /var/www , and run Node.

Now create a docker-compose.yml in the root of the project

version: '3' services:

api:

build:

context: .

ports:

- 3000:3000

volumes:

- .:/var/www

networks:

default:

aliases:

- my-api.dev

In the docker-compose.yml file we configured the API service.

The important part is the networks.default.aliases . This property will make sure our API is known as my-api.dev in the docker network. We will connect both services to this network and use the alias to communicate.

You can find information about all the properties in the documentation: https://docs.docker.com/compose/compose-file/

Now run the following in your terminal to set up a project

docker-compose run --rm api npm init

You will get some questions about the project name, version, etc. After you answered all the questions you should see a package.json in your project folder.

Now install express

docker-compose run --rm api npm i express

Create an src folder and add the following to the index.js

folder and add the following to the Let’s start the API!

docker-compose -p my-project up api

The -p (project name) flag will make sure this project uses the my-project_default network. We will start both services with this flag to make sure they are on the same docker network. If we don’t pass this flag docker-compose will use the folder name as default.

When you go to http://127.0.0.1:3000/hello-docker you should see `Hello Docker!`

Creating the second project

Open a new terminal window as we want our API to keep running.

Create a new folder for the second project and create a Dockerfile

# We don't actually need the node image for this, it’s just for this example.

FROM node:12-alpine RUN apk add curl CMD curl -s my-api.dev:3000/hello-docker

We will install curl and call the alias that we defined in our API project.

Create a docker-compose.yml file in the root folder

version: '3' services:

client: # It’s important that the name is different than the one we used in the API project

build:

context: .

Let’s run it!

docker-compose -p my-project up client

We use the -p flag here as well to make sure this project is connected to the same docker network as our API.

That’s it, if everything worked as expected you should now see {“message”:”Hello Docker!”} in your terminal.

Summary

The networks.default.aliases property can be used to give an alias to the container

property can be used to give an alias to the container Use the -p flag with the same name when starting your containers to make sure the containers use the same docker network.

flag with the same name when starting your containers to make sure the containers use the same docker network. Call the API using the alias and port from another service ( my-api.dev:3000 )