This is the 7th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Running MySQL containers with Docker

In addition to using Docker to run various middleware containers, which is convenient for daily development, we can also run Spring Boot and other Web applications in containers, so that we can keep the same running environment for development, testing and production, and avoid many problems caused by different running environments. This article uses a Spring Boot project to demonstrate how Spring Boot applications run in Docker containers.

Create a Spring Boot application

First, create a Spring Boot application to test. The easiest way to do this is through Spring Initializr by adding the Spring Web module to your project.

Then click the “GENERATE” button to GENERATE the project and download it, and you get an empty Spring Boot project. Next, add a Web interface to the startup class.

@SpringBootApplication
@RestController
public class SpringBootDockerDemoApplication {

    public static void main ( String [] args ) {
        SpringApplication.run( SpringBootDockerDemoApplication.class, args ) ;
    }

    @RequestMapping ( "/" )
    public String greeting (a) {
        return "Hello form Docker"; }}Copy the code

Hello Form Docker: Hello Form Docker: Hello Form Docker: Hello Form Docker: Hello Form Docker: Hello Form Docker

➜ curl localhost:8080
Hello form Docker
Copy the code

Finally, the project is packaged through the MVN Package.

Build the image from Dockerfile

Next, write the Dockerfile.

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Copy the code

With openJDK: 8-JDK-alpine as the base image, set a parameter to point to our packaged JAR package file, copy it into the image, and then execute the JAR file. The above Dockerfile is the simplest implementation.

But to keep the operating system in the container safe, it is best to restrict the permissions of the application when it is running, that is, running our Spring Boot program as a non-root user. We can create a dedicated user group and user in the image to execute the JAR file.

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Copy the code

In the Dockerfile above, the RUN directive is used to create the Spring USER group and USER, and then the USER directive is used to make subsequent commands RUN as this USER.

Put the Dockerfile in the root directory of the Spring Boot project so that the image can be built.

docker build -t spring-boot-docker-demo .
Copy the code

Once successful, you can use Docker Images to see the image you just created.

REPOSITORY                                                    TAG       IMAGE ID       CREATED          SIZE
spring-boot-docker-demo                                       latest    05303192f7e7   26 seconds ago   122MB
Copy the code

Run the Spring Boot application through the container

Run a container based on the image you just created.

docker run -p 8080:8080 spring-boot-docker-demo
Copy the code

Here we use -p to bind port 8080 in the container to port 8080 on the host. Since -d is not used, we can see the logs directly on the console. You can use curl to test whether the database is running successfully.