Docker deploys the SpringBoot project to integrate the Redis image for access counting sample code, dockerspringboot

Source: Internet
Author: User
Tags using git docker compose redis server

Docker deploys the SpringBoot project to integrate the Redis image for access counting sample code, dockerspringboot

The final result is as follows:

About 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.04x64
  • Docker 17.12.0-ce
  • IP: 45.32.31.101

1. Install Docker CE

It is not recommended in China: "script for installation", it will download and install slowly, use step 1 for installation, see the following link: Regular Installation Method

1. General Installation Method

Install Docker CE on Ubuntu 17.04x64

Http://www.ymq.io/2018/01/11/Docker-Install-docker-ce

2. Install the script

Script is not recommended for installation in China:

Install Docker CE

$ curl -fsSL get.docker.com -o get-docker.sh$ sudo sh get-docker.sh
<output truncated>If you would like to use Docker as a non-root user, you should now consideradding your user to the "docker" group with something like: sudo usermod -aG docker your-userRemember 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.

Verify Docker CE

Run the hello-world image to verify that Docker CE is correctly installed.

$ sudo docker run hello-worldroot@souyunku:~# sudo docker imagesREPOSITORY TAG  IMAGE ID CREATED SIZEhello-world latest f2a91732366c 7 weeks ago 1.85kB

Ii. Run the Redis Image

1. Run the image

$ docker run --name redis-6379 -p 6379:6379 -d redisUnable to find image 'redis:latest' locallylatest: Pulling from library/redisc4bb02b17bb4: Pull complete 58638acf67c5: Pull complete f98d108cc38b: Pull complete 83be14fccb07: Pull complete 5d5f41793421: Pull complete ed89ff0d9eb2: Pull complete Digest: sha256:0e773022cd6572a5153e5013afced0f7191652d3cdf9b1c6785eb13f6b2974b1Status: Downloaded newer image for redis:latest2f1f20f672e386a61644e1c08232ea34bdfd6a0c244b55fa833fcfd6dd207288

2. Check the image

View images

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

View Image Process

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

View container Process

root@souyunku:~# docker container psCONTAINER ID IMAGE COMMAND  CREATED STATUS PORTS  NAMES2f1f20f672e3 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 to the redis service through Redis-cli to test data storage

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

Iii. Java environment preparation

Note: follow these steps !!!

1. the Java environment is used to compile the Github Maven SpringBoot sample project and prepare

2. You can compile and upload the files locally. In this case, install Jdk, Maven, and Git in the following Java environment.

1. Install Jdk

Download JDK: 1

To download jdk in Linux, go to the (Official Website) to download the jdk Installation File.

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

JDK download method: 2

I am in Baidu cloud disk under the link: http://pan.baidu.com/s/1jIFZF9s password: u4n4

JDK download 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: If you use: Download JDK: 3

In the following steps, replace jdk1.8.0 _ 144 With jdk1.8.0 _ 141.

Start Installation

Upload to the/opt directory

Extract

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

Configure Environment Variables

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

Make environment variables take effect

$ source /etc/profile

Verify

root@souyunku:~# java -versionjava 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 -vApache Maven 3.3.9Maven home: /usr/share/mavenJava version: 1.8.0_141, vendor: Oracle CorporationJava home: /lib/jvm/jreDefault locale: en_US, platform encoding: UTF-8OS name: "linux", version: "4.10.0-35-generic", arch: "amd64", family: "unix"

3. Install Git

$ apt-get install git

Verify Maven

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

4. Project Preparation

1. compile the project

1. compile the project in Maven on the server.

Clone a project using git

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

Use maven to compile a project

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

2. Modify the project

Modify the Redis server address spring. redis. host = 45.32.44.217, which is a local IP address. when running the Redis image, you have made Redis an Internet service named 0.0.0.0: 6379-> 6379/tcp.

$ Vi src/main/resources/application. properties # Redis server address spring. redis. host = 45.32.44.217 $ mvn package

Copy the docker-spring-boot-demo-0.0.1-SNAPSHOT.jar project under the target/directory to the/opt directory which will be used later

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

2. compile the project locally using Maven, and then upload it to the/opt directory for later use.

Modify the Redis server address of application. properties

Other-projects/docker-spring-boot-demo/src/main/resources/application. properties # Redis server address spring. redis. host = 45.32.44.217

5. Compile Dockerfile

Write Dockerfile Based on java: 8 Images

$ cd /opt/$ touch Dockerfile$ vi Dockerfile

The edited content is as follows:

# Based on which image FROM java: 8 # mount the local folder to the current Container VOLUME/tmp # copy the file to the container, you can also directly write the ADD docker-spring-boot-demo-0.0.1-SNAPSHOT.jar/souyunku-app.jarADD docker-spring-boot-demo-0.0.1-SNAPSHOT.jar souyunku-app.jarRUN bash-c 'touch/souyunku-app.jar '# Open Port 80 EXPOSE 80 # configure the ENTRYPOINT Command executed after the container is started ["java ", "-Djava. security. egd = file:/dev /. /urandom ","-jar ","/souyunku-app.jar "]

6. Release a project

1. Compile the image

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

The following information proves that your Dockerfile has been written correctly and the image has been compiled successfully.

Sending build context to Docker daemon 18.72MBStep 1/6 : FROM java:88: Pulling from library/java5040bd298390: 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:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9dStatus: Downloaded newer image for java:8 ---> d23bdf5b1b1bStep 2/6 : VOLUME /tmp ---> Running in 0559a62b0cd5Removing intermediate container 0559a62b0cd5 ---> b1f3846913a4Step 3/6 : ADD docker-spring-boot-demo-0.0.1-SNAPSHOT.jar souyunku-app.jar ---> 9f60dad5d2acStep 4/6 : RUN bash -c 'touch /souyunku-app.jar' ---> Running in 39d5c09ab614Removing intermediate container 39d5c09ab614 ---> 2b691adf7922Step 5/6 : EXPOSE 80 ---> Running in 11a577437a23Removing intermediate container 11a577437a23 ---> 78815d6fe6b2Step 6/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/souyunku-app.jar"] ---> Running in eca10fed3d02Removing intermediate container eca10fed3d02 ---> 8ec4e85a0f05Successfully built 8ec4e85a0f05Successfully tagged souyunku-app:v1

2. view images

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

3. Run the image

The background daemon runs and maps the container port to the Internet port 80.

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

4. View Processes

View the container process. You can see that redis is on port 6379 and MySpringBoot is on port 80.

root@souyunku:/opt# docker container psCONTAINER ID IMAGE COMMAND  CREATED STATUS PORTS  NAMESe68d43860361 souyunku-app:v1 "java -Djava.securit…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp MySpringBoot0f9646171edd redis "docker-entrypoint.s…" 39 minutes ago Up 39 minutes 0.0.0.0:6379->6379/tcp redis-6379

VII. Test services

Browser access: http: // 127.0.0.1/

Docker Compose

Docker Compose is one of the official Docker Orchestration projects and is responsible for quickly deploying distributed applications in clusters.

An application that uses Docker containers usually consists of multiple containers. UseDocker ComposeYou do not need to use the shell script to start the container. In the configuration file, all containers are defined by services, and then useDocker-composeScripts to start, stop, and restart applications, and the services in the application and all containers dependent on services.

Summary

The above is the sample code for deploying spring boot in Docker by using an integrated Redis image for access counting. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.