Docker 部署SpringBoot項目整合 Redis 鏡像作訪問計數Demohtml
大概就幾個步驟java
國內不建議使用:「腳本進行安裝」,會下載安裝很慢,使用步驟 1 安裝,看下面的連接:常規安裝方式linux
Ubuntu 17.04 x64 安裝 Docker CEgit
www.ymq.io/2018/01/11/…github
國內安裝不建議使用腳本:redis
安裝Docker CEspring
$ 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 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.
複製代碼
驗證Docker CEdocker
經過運行hello-world
鏡像 驗證是否正確安裝了Docker CE
shell
$ 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
複製代碼
$ 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
複製代碼
查看鏡像瀏覽器
root@souyunku:~# docker images redis
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 1e70071f4af4 4 weeks ago 107MB
複製代碼
查看鏡像進程
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
複製代碼
查看容器進程
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
複製代碼
經過 redis-cli
鏈接Redis
服務,測試存儲數據
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:~#
複製代碼
注意:閱讀如下步驟!!!
1.Java 環境是爲了,編譯Github Maven SpringBoot
示例項目,準備
2.或者本身在本地編譯好上傳也行,那麼如下**Java
環境,安裝:Jdk
,Maven
,Git
,就能夠不用配置**
下載JDK 方式:1
下載Linux環境下的jdk1.8,請去(官網)中下載jdk的安裝文件
下載JDK 方式:2
我在百度雲盤分下的連接:pan.baidu.com/s/1jIFZF9s 密碼:u4n4
下載JDK 方式:3
使用wget下載,
$ 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"
複製代碼
注意,若是使用:下載JDK 方式:3
那麼如下步驟的,jdk1.8.0_144
替換成jdk1.8.0_141
開始安裝
上傳在 /opt
目錄
解壓
$ cd /opt
$ tar zxvf jdk-8u144-linux-x64.tar.gz
$ mv jdk1.8.0_144/ /lib/jvm
複製代碼
配置環境變量
$ 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
複製代碼
使環境變量生效
$ source /etc/profile
複製代碼
驗證
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)
複製代碼
$ apt-get install maven
複製代碼
驗證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"
複製代碼
$ apt-get install git
複製代碼
驗證Maven
root@souyunku:~# git --version
git version 2.11.0
複製代碼
1.本身在服務器Maven編譯項目
使用 git 克隆項目
$ git clone https://github.com/souyunku/other-projects.git
複製代碼
使用 maven 編譯項目
$ cd other-projects/docker-spring-boot-demo/
複製代碼
修改 Redis
服務器地址spring.redis.host=45.32.44.217
,爲本地IP
, 在運行 Redis
鏡像時候 已經把 Redis
作成外網服務了0.0.0.0:6379->6379/tcp
$ vi src/main/resources/application.properties
複製代碼
# Redis服務器地址
spring.redis.host=45.32.44.217
複製代碼
$ mvn package
複製代碼
複製target/
目錄下的docker-spring-boot-demo-0.0.1-SNAPSHOT.jar
項目到/opt
目錄稍後會用到
$ cp target/docker-spring-boot-demo-0.0.1-SNAPSHOT.jar /opt/
複製代碼
2.本身在本地Maven
編譯項目,而後上傳到/opt
目錄稍後會用到
修改application.properties
的 Redis
服務器地址
other-projects/docker-spring-boot-demo/src/main/resources/application.properties
# Redis服務器地址
spring.redis.host=45.32.44.217
複製代碼
編寫 Dockerfile
基於java:8
鏡像爲基礎
$ cd /opt/
$ touch Dockerfile
$ vi Dockerfile
複製代碼
編輯內容個以下
# 基於哪一個鏡像
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"]
複製代碼
$ cd /opt/
$ docker build -t souyunku-app:v1 .
複製代碼
看到以下信息,就證實你的Dockerfile
寫的沒毛病,並且鏡像也編譯成功了
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
複製代碼
root@souyunku:/opt# docker images souyunku-app
REPOSITORY TAG IMAGE ID CREATED SIZE
souyunku-app v1 8ec4e85a0f05 2 minutes ago 681MB
複製代碼
後臺守護進程運行,而後把容器端口映射到,外網端口80
root@souyunku:/opt# docker run --name MySpringBoot -d -p 80:80 souyunku-app:v1
e68d438603619e363883d4eae65d3918e1c3e00f867731207bccf06f5690dc64
複製代碼
查看容器進程,能夠看到redis 在 6379
端口,MySpringBoot
項目在 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
複製代碼
瀏覽器訪問:http://127.0.0.1/,固然我沒有輸入127.0.0.1
我是在服務器上直接作的,用的公網IP
Docker Compose
是 Docker
官方編排(Orchestration
)項目之一,負責快速在集羣中部署分佈式應用。
一個使用Docker
容器的應用,一般由多個容器組成。使用Docker Compose
,再也不須要使用shell
腳原本啓動容器。在配置文件中,全部的容器經過services
來定義,而後使用docker-compose
腳原本啓動,中止和重啓應用,和應用中的服務以及全部依賴服務的容器
Docker Compose 的搭建,實戰用例正在整理中,會在下篇文章體現,關注公衆號:"搜雲庫" 我會在微信公衆號首發
GitHub :docker-spring-boot-demo