Objective

We built our first microservice and made a docker image of it in previous episodes. Here we run our docker container.

Technologies

Docker

Create a new network

Out catalog-service needs to be able to connect to mongo. For that they need to be on the same network. So, let’s create a network, to which we will attach our containers later.

docker network create microxs-demo-network

Run mongodb container

Our microservice – catalog-service needs a mongodb. Hence, we will first run mongodb as a container. MongoDB’s docker image is available on docker hub. Pull it and run.

docker run --net=microxs-demo-network --name catalog-db mongo

This should pull the image of mongo from docker hub, create a container named catalog-db from the image, and then run it. It also attaches the container to the network – “microxs-demo-network”. It should give the following output to indicate that it is running and listening on the default mongo port

[thread1] waiting for connections on port 27017

Run catalog-service container

docker run --net=microxs-demo-network --name=catalog-service --env MICROXS_COMMERCE_MONGO_SERVICE_HOST=catalog-db --env MICROXS_COMMERCE_MONGO_SERVICE_PORT=27017 -p 8080:3333 catalog-service

This command pulls the docker image “catalog-service” and runs it.

Let’s dissect this command: –

--net=microxs-demo-network

Join the network “microxs-demo-network”. Host name will be “catalog-service”.

--name=catalog-service

It gives the container the name as “catalog-service”.

--env MICROXS_COMMERCE_MONGO_SERVICE_HOST=catalog-db

Set the environment variable MICROXS_COMMERCE_MONGO_SERVICE_HOST to “catalog-db” which is the hostname of the mongo db server. This environment variable is used in catalog-service to generate connection URL to mongo.

--env MICROXS_COMMERCE_MONGO_SERVICE_PORT=27017

Similarly set the environment variable for mongodb port.

-p 8080:3333

Expose the container’s port 3333 to port 8080 on the host machine.

You can verify this by running the command:

docker ps

Also, invoke the spring-data-REST endpoint that we created in catalog-service.

curl http://localhost:8080/catalog-service/skus

Video

Step 4 Video

Advertisements