Spring Boot and Node Applications in Docker

Spring Boot and Node Applications in Docker

Now we are finally getting close to a production setup.

In microservices we have multiple instance of multiple applications running handling all requests. But how do apps run in multiple instances at production ? Well, let's find out

Spring Boot Application in docker

In the last article, we learned how java apps run inside docker environments. Spring Boot applications are also java apps, so the basics remain the same, except for some minor tweaks.

Create a sample SpringBoot application with a ping pong endpoint.

@RequestMapping("/")
@RestController
public class Dummy {
    @GetMapping("/ping")
    public String test(){
        return "pong";
    }
}        

Run the app and hit https://localhost:8080/ping and the result will be "pong"

Cool, how do we run this in the docker environment ?

We'll need dockerfile, we'll create image out of it and then finally create a container to run the app.

But first let's create the jar of application. Run command, gradlew clean build in windows terminal, this would create a jar of the application in the build/libs directory

Let's use the same docker file, from last article, for now

# Bring in the OS and java setup
# This will bring up a tiny little container with ubuntu and java setup
FROM openjdk:21-jdk-slim

# This will setup location for our next command execution
WORKDIR /app

# Copy the jar with a different name in /app
COPY build/libs/demo-0.0.1-SNAPSHOT.jar app.jar

# run the jar
ENTRYPOINT ["java", "-jar", "app.jar"]        

Now, we let's create the image : docker build -t devdanish.in:springboot-demo .

Run the container : docker run --name springboot-demo devdanish.in:springboot-demo

This will log the standard output, starting springboot application ....

Now as per logs the app is running on port 8080. Let's try to hit it on https://localhost:8080/ping

It won't work !! We'll see why in future articles.

For now, let's update the run command and add an extra argument -p 8080:8080

Stop and terminate the container, we'll start it again with the new command.

docker stop springboot-demo and docker rm springboot-demo

Rerun the application docker run --name springboot-demo -p 8080:8080 devdanish.in:springboot-demo

Now go back to https://localhost:8080/ping and you'll get a response "pong"

So, one take away point : use -p port_number:port_number with webapps for map porting. What is that ?! We'll see in future articles

Node application in Docker

Create a directory and run command npm init -y followed by npm install express

Add a new js file, index.js and paste the code below to create a node application

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/ping', (req, res) => {
  res.send('pong');
});

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});        

Now run command : node index.js

Hit endpoint : https://localhost:3000/ping Result : pong

So our node app is up and running

Let's run it in docker environment. We already know the steps, so let's start with dockerfile

# Bring OS with node installed
FROM node:20-alpine

# Set working directory
WORKDIR /app

# Copy package.json and install dependencies
COPY package.json .
RUN npm install

# Copy the rest of the application code
COPY . .

# Start the application
CMD ["node", "index.js"]
        

Build the image : docker build -t devdanish.in:node-demo .

Run the container : docker run --name node-demo -p 3000:3000 devdanish.in:node-demo

Check on https://localhost:3000/ping , result : "pong"

Great ???? Now we have successfully learned to run webapps in docker.


In the next article, we'll learn to pass environment variables more.

Since I get time to draft these only on the weekends, you can checkout my notes at technotes.devdanish.in/docker

要查看或添加评论,请登录

Danish Javed的更多文章

  • Docker Setup

    Docker Setup

    Finally we are going to setup and run our first container. Excited ?! Alright, lets begin.

  • Docker Perquisite understanding & Setup

    Docker Perquisite understanding & Setup

    This is continuation of my previous article. if you haven't read that already, I recommend reading that first :…

  • Docker Containers and Images

    Docker Containers and Images

    This is continuation of my previous article. if you haven't read that already, I recommend reading that first :…

  • Docker Overview

    Docker Overview

    Before diving in with what is Docker, we first need to understand why is docker and what problem it is trying to solve…

社区洞察