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

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

Jun 02, 2023 am 10:37 AM
redis docker springboot

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
Copy after login
<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>
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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:~#
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

Make environment variables effective

$ source /etc/profile
Copy after login

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)
Copy after login

2. Install Maven

$ apt-get install maven
Copy after login

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"
Copy after login

3. Install Git

$ apt-get install git
Copy after login

Verify Maven

root@souyunku:~# git --version
git version 2.11.0
Copy after login

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
Copy after login

Use maven to compile the project

$ cd other-projects/docker-spring-boot-demo/
Copy after login

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 after login

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/
Copy after login

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
Copy after login

5. Write the Dockerfile

Write Dockerfile based on java:8 mirror

$ cd /opt/
$ touch Dockerfile
$ vi Dockerfile
Copy after login

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"]
Copy after login

6. Release the project

1. Compile the image

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

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop &lt;container_name&gt; Command Use docker kill &lt;container_name&gt; command in the host terminal (force exit)

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] &lt;Container Path&gt; &lt;Host Path&gt;. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop &lt;container_id&gt;); start the container (docker start &lt;container_id&gt;); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

See all articles