Create docker image for micro service

Docker, Docker- compose, Docker hub, Images and Containers

Nikitha Gullapalli
6 min readJun 19, 2023

GitHub: https://github.com/nikitha2/Currency-Exchange-Conversion-Microservices-Backend.git

Linkedin: https://www.linkedin.com/in/nikitha-gullapalli/

In the microservices series so far we looked at:

Now that we have a few micro services working in our local. Lets looks at how to deploy them. Lets use docker for deployment.

What is docker? Why use it? How to use it?

Major advantages of a micro service is flexibility to innovate. I can use different technology for different micro service. E.g MS1 can be written in java and use mongoDB, MS2 can be written in Kotlin and use MySql DB, M3 can be written in Go and use H2 DB.

So does this mean I have different ways of deploying each of these micrservices? This is where docker comes in. Docker simplifies the deployment process. Irrespective of the technology used, deployment process is the same. More on Docker here.

Docker says:

  • Create an image for the micro service. This image has everything needed to run the micro service. From application code, application runtime (like JDK, Python), dependencies, etc.
  • Then run the container (running image is called container) in any interface (local machine, corporate DB, cloud like google cloud or AWS).

Furthur advantage is that it is light weight. With docker, I do not need different virtual machines to deploy each micro service. I can simply share my OS and have containers above it. Images are independent of machine OS.

Download docker

You can download docker from here.

Create an image

  1. Add dependency in pom.xml file for each micro service and save file.
<!--
YourRepositioryName is the repositiory name in docker.
You will need to replace it with your.
To find your repository name https://www.docker.com, signin and goto profile.
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>YourRepositioryName/mmv3-${project.artifactId}:${project.version}</name>
</image>
<pullPolicy>IF_NOT_PRESENT</pullPolicy>
</configuration>
</plugin>
</plugins>
</build>

To find your repository name, goto https://www.docker.com -> sign-in -> profile. You should see something like below. The blacked out part is your repository name.

2. Right click on the micro service you want to create the image for and click Run as -> Maven build.

3. Enter goal and run to create image.

goal = spring-boot:build-image -DskipTests

// DskipTests skips the test. In production you do not skip test
// --X debug logs
/** can also run this from command line. cd into the service folder and run command
nvm spring-boot:build-image -DskipTests --X
*/

4. It will take a while to run. Be patient. Once build runs you should have your image. Here my image name is <YourRepositioryName>/mmv3-currency-conversion-service:0.0.1-SNAPSHOT-1

Repeat the process for all the services you need to run.

Docker-Compose to run the micro service Image

Now that we all images of all the micro services and supporting services. Lets see how to run them.

Running image is called a container

You can either run each image using the command shown here or use docker — compose.

docker run -p <container port>:<local port> -d <repositoryName:tag>
e.g: docker run -p 5000:5000 -d <YourRepositioryName>/mmv3-currency-conversion-service:0.0.1-SNAPSHOT-1

Docker compose allows up to run/deploy all images together. We can simply add all configurations in the file and run it.

  1. Create a docker-compose file.It should be a yaml file. Name it docker-compose.yaml

2. Add details of each micro service image as shown here.

#Note: configurations in docker-compose file will overrite the configurations
# in resources.properties.

# As the file has eureka server named "naming-server".
# We want to pass the traffic via that naming server.
# So we reconfigure and asking each image to use this configuration instead
# of whats defined in application.properties file which is "http://localhost:8761/eureka"
EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://naming-server:8761/eureka

# As the file has zipkin server named "zipkin-server".
# We want to log the traffic to that zipkin-server.
# So we reconfigure and asking each image to use this configuration instead
# of whats defined in application.properties file which is "http://localhost:9411/api/v2/spans"
MANAGEMENT.ZIPKIN.TRACING.ENDPOINT: http://zipkin-server:9411/api/v2/spans

# For services that are dependent on eureka we add a dependency.
# This way the image will start after eureka is up
depends_on:
- naming-server

#Port to run on <docker port not accessable to user: port accessable to user >
ports:
- "8000:8000"

# Max memory limit service can utilize
mem_limit: 700m

# name of image
image: nikithagullapalli/mmv3-currency-conversion-service:0.0.1-SNAPSHOT-1

#Network to run the service on. Create one network to run all your services.
networks:
- currency-network

3. Great! now we can simply run the docker-compose file to start the services.

  • Goto the location where docker compose is. Right click -> new terminal tab at Folder. This will open the terminal at the folder level
  • Run command docker-compose up
# Note: ctrl + c -> cancels the operation running

# Note: Search for "Registered instance" in the terminal to see if
# service instance is up in naming server.
  • Now if you open eureka in bowser, you should see all the services up.

You can also goto zipkin on browser and see the trace.

Push images to Docker Hub

Great! now that we have everything working. Lets push our images to the cloud. That allows us to access our images from anywhere. Here is an article that explains in detail on how to push and pull images from the docker hub.

1. Login to your docker hub from terminal. Open terminal -> login

docker login --username=<docker username or emailID>

Youll be prompted to enter passowrd. Enter docker hub account password and click enter. Login should succeed.

2. Check all the images in your local with below command

docker images
Output for command “docker images”

3. Tag the image you want to push

docker tag <imageID> <yourhubusername>/<Imagename>:<tag>
E.g: docker tag d915328822fd <yourhubusername>/mmv3-gateway:latest

4. push image to docker hub

docker push <yourhubusername>/<Imagename>:<tag>

Hope this is helpful. Excited to hear feedback and learn from your inputs.

References:

Udemy course by in28mins

Docker-commands-to-keep-handy

Pushing and pulling to and from docker Hub

--

--

No responses yet