Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy an application in a docker container

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to deploy applications in docker containers". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A simple Hello World application.

1.1 Maven dependence

Org.springframework.bootspring-boot-starter-parent1.5.4.RELEASE

Introduce spring-boot-starter-web to create a Web application.

Org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starter-web

1.2 main application

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

1.3 Service Interface

RESTful service interface:

@ RestControllerpublic class HelloController {@ RequestMapping ("/ hello") public String hello () {return "Hello World";}}

2 install Docker. After installation, the virtual IP of Docker environment is 192.168.99.100.

3 deploy applications

3.1Packaging Spring Boot applications

Execute the mvn packaging command under the Spring Boot application project directory:

Mvn package spring-boot:repackage

A jar package firtSpringBoot-0.0.1-SNAPSHOT.jar is generated in the target directory of the project. Upload the jar package to firtSpringBoot-1.0.jar to the Docker environment.

3.2 Dockerfile Fil

Dockfile is in principle a batch file that contains commands to build the image. Putting these commands in a file is not absolutely necessary because we can pass them to the command line, but it is easier to use the file.

Now create a Dockerfile:

FROM alpine:edgeMAINTAINER peterwanghao.comRUN apk add-- no-cache openjdk8VOLUME / tmpADD firtSpringBoot-1.0.jar firtSpringBoot.jarEXPOSE 8080ENTRYPOINT ["java", "- jar", "/ firtSpringBoot.jar"]

3.3 create a mirror

Run the docker build command from the Dockerfile file directory, which downloads the base image and runs the entries in the Dockerfile file one by one. The name of the created image is first, label 1. 0. -rm=true, delete the container of the intermediate link after the whole construction process is successful.

Docker build-t first:1.0-- rm=true.

Use the docker images command to list all the images and their details.

Docker@default:~/first-spring-boot$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEfirst 1.0 02f67d8c308d 17 hours ago 119MBhello-world latest e38bc07ac18e 6 weeks ago 1.85kBalpine edge 5c4fa780951b 4 months ago 4.15MB

3.4 run the container

Next, we will run the container. Use the following command to load and run the container.

Docker run-name=first-p 8080R 8080-t first:1.0

Use the docker ps command to list running containers

This is the end of docker@default:~/first-spring-boot$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9fc3aef6e852 first:1.0 "java-jar / firtSpri 5 hours ago Up 5 hours 0.0.0.0 hours ago Up 8080-> 8080/tcp first" how to deploy applications in docker containers. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report