The final effect is as follows
There are roughly 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
Environmental preparation
1. Install Docker CE
Do not recommend using it in China: "Scripts for installation", it will be downloaded and installed very slowly. Use Step 1 to install it, see the following link: General installation method
1. Regular installation method
Ubuntu 17.04 x64 Install Docker CE
http://www.ymq.io/2018/01/11/Docker-Install-docker-ce
2. Install the script
Scripts are not recommended for domestic installation:
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 considering 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
Verify that Docker CE is installed correctly by running hello-world image
$ sudo docker run hello-worldroot@souyunku:~# sudo docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEhello-world latest f2a91732366c 7 weeks ago 1.85kB
2. Run Redis image
1. Run the mirror
$ 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 mirror
View the mirror
root@souyunku:~# docker images redisREPOSITORY TAG IMAGE ID CREATED SIZEredis latest 1e70071f4af4 4 weeks ago 107MB
View the mirror 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:6379->6379/tcp redis-6379
3. Test Redis Service
Connect to Redis service through redis-cli to test store data
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:~#
3. Java environment preparation
Note: Read the following steps! ! !
1. The Java environment is to compile the Github Maven SpringBoot sample project and prepare
2. Or you can compile and upload locally by yourself. Then install the following Java environment: Jdk, Maven, Git, and you can do without configuration.
1. Install Jdk
Download JDK method: 1
Download jdk1.8 in 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, if using: Download JDK method: 3
Then, in the following steps, jdk1.8.0_144 is replaced by jdk1.8.0_141
Start the installation
Upload in /opt directory
Decompression
$ cd /opt$ tar zxvf jdk-8u144-linux-x64.tar.gz$ mv jdk1.8.0_144/ /lib/jvm
Configure environment variables
$ vi /etc/profile#jdkeexport JAVA_HOME=/lib/jvmexport JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATHMake environment variables effective
$ 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 on the server Maven by 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 the project
Modify the Redis server address spring.redis.host=45.32.44.217 to be 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 server address 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 and will be used later
$ cp target/docker-spring-boot-demo-0.0.1-SNAPSHOT.jar /opt/
2. Compile the project in local Maven and upload it to the /opt directory and will be used later
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. Write Dockerfile
Writing Dockerfile based on java:8 image
$ 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, or write it directly as 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 80 port EXPOSE 80# Configure the command ENTRYPOINT after the container is started ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/souyunku-app.jar"]
VI. Publish the project
1. Compile the image
$ cd /opt/$ docker build -t souyunku-app:v1 .
Seeing the following information proves that your Dockerfile is not bad, 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 the mirror
root@souyunku:/opt# docker images souyunku-appREPOSITORY TAG IMAGE ID CREATED SIZEsouyunku-app v1 8ec4e85a0f05 2 minutes ago 681MB
3. Run the mirror
The background daemon runs, and then maps the container port to the external network port 80
root@souyunku:/opt# docker run --name MySpringBoot -d -p 80:80 souyunku-app:v1e68d438603619e363883d4eae65d3918e1c3e00f867731207bccf06f5690dc64
4. View the process
Check the container process and you can see that redis is on port 6379 and the MySpringBoot project 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
7. Testing 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 and used the public IP
Docker Compose
Docker Compose is one of the official Docker orchestration projects, responsible for quickly deploying distributed applications in clusters.
An application that uses a Docker container usually consists 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 the docker-compose script to start, stop and restart the application, and the services in the application and all containers that depend on services.
Summarize
The above is the example code for the Docker deployment SpringBoot project that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!