Home  >  Article  >  Database  >  How Docker deploys SpringBoot projects and integrates Redis images for access counting

How Docker deploys SpringBoot projects and integrates Redis images for access counting

WBOY
WBOYforward
2023-06-02 10:37:14734browse

The final effect is as follows

How Docker deploys SpringBoot projects and integrates Redis images for access counting

Just a few steps

1. Install Docker CE 2. Run Redis image 3. Java environment preparation 4. Project Preparation 5. Write Dockerfile 6. Release project 7. Test service

Environment preparation

  • System: Ubuntu 17.04 x64

  • Docker 17.12.0-ce

  • IP:45.32.31.101

1. Install Docker CE

It is not recommended to use: "Script for installation" in China. The download and installation will be very slow. Use step 1 to install. See the link below: Conventional installation method

1. Conventional Installation method

Ubuntu 17.04 x64 Install Docker CE

2. Script for installation

It is not recommended to use scripts for domestic installation:

Install Docker CE

$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
<output>
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
 sudo usermod -aG docker your-user
Remember that you will have to log out and back in for this to take effect!
WARNING: Adding a user to the "docker" group will grant the ability to run
 containers which can be used to obtain root privileges on the
 docker host.
 Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
 for more information.</output>

Verify Docker CE

Verify that Docker CE is installed correctly by running the hello-world image

$ sudo docker run hello-world
root@souyunku:~# sudo docker images
REPOSITORY TAG  IMAGE ID CREATED SIZE
hello-world latest f2a91732366c 7 weeks ago 1.85kB

2. Run Redis mirror

1. Run the mirror

$ docker run --name redis-6379 -p 6379:6379 -d redis
Unable to find image 'redis:latest' locally
latest: Pulling from library/redis
c4bb02b17bb4: Pull complete 
58638acf67c5: Pull complete 
f98d108cc38b: Pull complete 
83be14fccb07: Pull complete 
5d5f41793421: Pull complete 
ed89ff0d9eb2: Pull complete 
Digest: sha256:0e773022cd6572a5153e5013afced0f7191652d3cdf9b1c6785eb13f6b2974b1
Status: Downloaded newer image for redis:latest
2f1f20f672e386a61644e1c08232ea34bdfd6a0c244b55fa833fcfd6dd207288

2. Check the mirror

View the mirror

root@souyunku:~# docker images redis
REPOSITORY TAG  IMAGE ID CREATED SIZE
redis latest 1e70071f4af4 4 weeks ago 107MB

View the mirror process

root@souyunku:~# docker ps
CONTAINER ID IMAGE COMMAND  CREATED STATUS PORTS  NAMES
2f1f20f672e3 redis "docker-entrypoint.s…" 14 seconds ago Up 14 seconds 0.0.0.0:6379->6379/tcp redis-6379

View the container process

root@souyunku:~# docker container ps
CONTAINER ID IMAGE COMMAND  CREATED STATUS PORTS  NAMES
2f1f20f672e3 redis "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 0.0.0.0:6379->6379/tcp redis-6379

3. Test the Redis service

Connect the Redis service through redis-cli and test the stored data

root@souyunku:~# docker run -it --link redis-6379:redis --rm redis redis-cli -h redis -p 6379
redis:6379> set count 1
OK
redis:6379> get count
"1"
redis:6379> exit
root@souyunku:~#

3. Java Environment preparation

Note: Read the following steps! ! !

1. The Java environment is for compiling the Github Maven SpringBoot sample project and preparing

2. Or you can compile it locally and upload it. Then install the following Java environment: Jdk, Maven, Git, you don’t need to configure it

1. Install Jdk

Download JDK method: 1

Download jdk1.8 under Linux environment, please go to (official website) to download jdk Installation file

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Download JDK method: 2

My link in Baidu Cloud Disk: http://pan.baidu.com/s/1jIFZF9s Password: u4n4

Download JDK Method: 3

Use wget to download,

$ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz

Note that if you use: Download JDK method: 3

then in the following steps, replace jdk1.8.0_144 with jdk1.8.0_141

Start the installation

Upload in the /opt directory

Unzip

$ cd /opt
$ tar zxvf jdk-8u144-linux-x64.tar.gz
$ mv jdk1.8.0_144/ /lib/jvm

Configure environment variables

$ vi /etc/profile
#jdk
export JAVA_HOME=/lib/jvm
export JRE_HOME=${JAVA_HOME}/jre 
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib 
export PATH=${JAVA_HOME}/bin:$PATH

Make environment variables effective

$ source /etc/profile

Verify

root@souyunku:~# java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)

2. Install Maven

$ apt-get install maven

Verify Maven

root@souyunku:~# mvn -v
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_141, vendor: Oracle Corporation
Java home: /lib/jvm/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.10.0-35-generic", arch: "amd64", family: "unix"

3. Install Git

$ apt-get install git

Verify Maven

root@souyunku:~# git --version
git version 2.11.0

4. Project preparation

1. Compile the project

1. Compile the project on the server Maven yourself

Use git to clone the project

$ git clone https://github.com/souyunku/other-projects.git

Use maven to compile the project

$ cd other-projects/docker-spring-boot-demo/

2. Modify project

Modify the Redis server address spring.redis.host=45.32.44.217 to the local IP. When running the Redis image, Redis has been made into an external network service 0.0.0.0:6379-> 6379/tcp

$ vi src/main/resources/application.properties
# Redis服务器地址
spring.redis.host=45.32.44.217
$ mvn package

Copy the docker-spring-boot-demo-0.0.1-SNAPSHOT.jar project in the target/ directory to the /opt directory for later use

$ cp target/docker-spring-boot-demo-0.0.1-SNAPSHOT.jar /opt/

2. Compile the project in local Maven, and then upload it to the /opt directory. You will use it later

Modify the Redis server address of application.properties

other-projects/docker-spring-boot-demo/src/main/resources/application.properties
# Redis服务器地址
spring.redis.host=45.32.44.217

5. Write the Dockerfile

Write Dockerfile based on java:8 mirror

$ cd /opt/
$ touch Dockerfile
$ vi Dockerfile

Edit the content as follows

# 基于哪个镜像
FROM java:8
# 将本地文件夹挂载到当前容器
VOLUME /tmp
# 拷贝文件到容器,也可以直接写成ADD docker-spring-boot-demo-0.0.1-SNAPSHOT.jar /souyunku-app.jar
ADD docker-spring-boot-demo-0.0.1-SNAPSHOT.jar souyunku-app.jar
RUN bash -c 'touch /souyunku-app.jar'
# 开放80端口
EXPOSE 80
# 配置容器启动后执行的命令
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/souyunku-app.jar"]

6. Release the project

1. Compile the image

$ cd /opt/
$ docker build -t souyunku-app:v1 .

Seeing the following information proves that your Dockerfile is written correctly and the image is compiled successfully

Sending build context to Docker daemon 18.72MB
Step 1/6 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete 
fce5728aad85: Pull complete 
76610ec20bf5: Pull complete 
60170fec2151: Pull complete 
e98f73de8f0d: Pull complete 
11f7af24ed9c: Pull complete 
49e2d6393f32: Pull complete 
bb9cdec9c7f3: Pull complete 
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/6 : VOLUME /tmp
 ---> Running in 0559a62b0cd5
Removing intermediate container 0559a62b0cd5
 ---> b1f3846913a4
Step 3/6 : ADD docker-spring-boot-demo-0.0.1-SNAPSHOT.jar souyunku-app.jar
 ---> 9f60dad5d2ac
Step 4/6 : RUN bash -c 'touch /souyunku-app.jar'
 ---> Running in 39d5c09ab614
Removing intermediate container 39d5c09ab614
 ---> 2b691adf7922
Step 5/6 : EXPOSE 80
 ---> Running in 11a577437a23
Removing intermediate container 11a577437a23
 ---> 78815d6fe6b2
Step 6/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/souyunku-app.jar"]
 ---> Running in eca10fed3d02
Removing intermediate container eca10fed3d02
 ---> 8ec4e85a0f05
Successfully built 8ec4e85a0f05
Successfully tagged souyunku-app:v1

2. Check the image

root@souyunku:/opt# docker images souyunku-app
REPOSITORY TAG  IMAGE ID CREATED SIZE
souyunku-app v1  8ec4e85a0f05 2 minutes ago 681MB

3. Run the image

Run the background daemon, and then map the container port to the external network port 80

root@souyunku:/opt# docker run --name MySpringBoot -d -p 80:80 souyunku-app:v1
e68d438603619e363883d4eae65d3918e1c3e00f867731207bccf06f5690dc64

4. View the process

View the container process, you can see that redis is in Port 6379, MySpringBoot project is at port 80

root@souyunku:/opt# docker container ps
CONTAINER ID IMAGE COMMAND  CREATED STATUS PORTS  NAMES
e68d43860361 souyunku-app:v1 "java -Djava.securit…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp MySpringBoot
0f9646171edd redis "docker-entrypoint.s…" 39 minutes ago Up 39 minutes 0.0.0.0:6379->6379/tcp redis-6379

7. Test service

Browser access: http://127.0.0.1/, of course I did not enter 127.0. 0.1 I did it directly on the server, using the public IP

Docker Compose

Docker Compose is one of Docker’s official orchestration projects, responsible for fast Deploy distributed applications in a cluster.

An application using Docker containers is usually composed of multiple containers. With Docker Compose , you no longer need to use shell scripts to start containers. In the configuration file, all containers are defined by services, and then use docker-compose scripts to start, stop and restart the application, the services in the application and all containers that depend on the service.

The above is the detailed content of How Docker deploys SpringBoot projects and integrates Redis images for access counting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete