spring-boot整合docker插件實現項目部署

1、 部署環境(docker安裝步驟參考之前的博客)

JDK1.8、Docker1.13.1、CentOS7.0

2、docker開啓遠程訪問

[root@xinglinglove ~]# vim /lib/systemd/system/docker.service

紅色部分爲新增內容

[Unit]

Description=Docker Application Container Engine

Documentation=http://docs.docker.com

After=network.target rhel-push-plugin.socket registries.service

Wants=docker-storage-setup.service

Requires=docker-cleanup.timer

[Service]

Type=notify

NotifyAccess=all

EnvironmentFile=-/run/containers/registries.conf

EnvironmentFile=-/etc/sysconfig/docker

EnvironmentFile=-/etc/sysconfig/docker-storage

EnvironmentFile=-/etc/sysconfig/docker-network

Environment=GOTRACEBACK=crash

Environment=DOCKER_HTTP_HOST_COMPAT=1

Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin

ExecStart=/usr/bin/dockerd-current \

--add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \

--default-runtime=docker-runc \

--exec-opt native.cgroupdriver=systemd \

--userland-proxy-path=/usr/libexec/docker/docker-proxy-current \

--seccomp-profile=/etc/docker/seccomp.json \

-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \

$OPTIONS \

$DOCKER_STORAGE_OPTIONS \

$DOCKER_NETWORK_OPTIONS \

$ADD_REGISTRY \

$BLOCK_REGISTRY \

$INSECURE_REGISTRY \

$REGISTRIES

ExecReload=/bin/kill -s HUP $MAINPID

LimitNOFILE=1048576

LimitNPROC=1048576

LimitCORE=infinity

TimeoutStartSec=0

Restart=on-abnormal

MountFlags=slave

KillMode=process

 

[Install]

WantedBy=multi-user.target

3.重啓docker

[root@xinglinglove ~]# systemctl daemon-reload [root@xinglinglove ~]# systemctl restart docker.service

4.測試是否生效

curl http://140.143.0/126:2375/info

返回如下信息代表開啓成功

{"ID":"27LC:PLU7:B5DF:H3KS:QGUH:TABJ:5YJC:DNEV:NEZL:L33Y:BOWB:KOZV","Containers":6,"ContainersRunning":5,"ContainersP aus ed":0,"ContainersStopped":1,"Images":10,"Driver":"overlay2","DriverStatus":[["Backing Filesystem","extfs"],["Supports d_ type","true"],["Native Overlay Diff","true"]],"SystemStatus":null,"Plugins":{"Volume":["local"],"Network":["bridge","hos t","macvlan","null","overlay"],"Authorization":null},"MemoryLimit":true,"SwapLimit":true,"KernelMemory":true,"CpuCfsPeri od":true,"CpuCfsQuota":true,"CPUShares":true,"CPUSet":true,"IPv4Forwarding":true,"BridgeNfIptables":true,"BridgeNfIp6tab les":true,"Debug":false,"NFd":50,"OomKillDisable":true,"NGoroutines":44,"SystemTime":"2019-03-26T16:42:23.723116602+08:0 0","LoggingDriver":"journald","CgroupDriver":"systemd","NEventsListener":0,"KernelVersion":"3.10.0-693.el7.x86_64","PkgV ersion":"<unknown>","OperatingSystem":"CentOS Linux 7 (Core)","OSType":"linux","Architecture":"x86_64","IndexServerAddre ss":"https://index.docker.io/v1/","IndexServerName":"docker.io","RegistryConfig":{"InsecureRegistryCIDRs":["127.0.0.0/8" ],"IndexConfigs":{"docker.io":{"Name":"docker.io","Mirrors":null,"Secure":true,"Official":true}},"Mirrors":[]},"NCPU":1, "MemTotal":1928822784,"DockerRootDir":"/var/lib/docker","HttpProxy":"","HttpsProxy":"","NoProxy":"","Name":"xinglinglove ","Labels":null,"ExperimentalBuild":false,"ServerVersion":"1.13.1","ClusterStore":"","ClusterAdvertise":"","Runtimes":{" docker-runc":{"path":"/usr/libexec/docker/docker-runc-current"},"runc":{"path":"docker-runc"}},"DefaultRuntime":"docker- runc","Swarm":{"NodeID":"","NodeAddr":"","LocalNodeState":"inactive","ControlAvailable":false,"Error":"","RemoteManagers ":null,"Nodes":0,"Managers":0,"Cluster":{"ID":"","Version":{},"CreatedAt":"0001-01-01T00:00:00Z","UpdatedAt":"0001-01-01 T00:00:00Z","Spec":{"Orchestration":{},"Raft":{"ElectionTick":0,"HeartbeatTick":0},"Dispatcher":{},"CAConfig":{},"TaskDe faults":{},"EncryptionConfig":{"AutoLockManagers":false}}}},"LiveRestoreEnabled":false,"Isolation":"","InitBinary":"dock er-init","ContainerdCommit":{"ID":"","Expected":"aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1"},"RuncCommit":{"ID":"N/A","Ex pected":"9df8b306d01f59d3a8029be411de015b7304dd8f"},"InitCommit":{"ID":"N/A","Expected":"949e6facb77383876aeff8a6944dde6 6b3089574"},"SecurityOptions":["name=seccomp,profile=/etc/docker/seccomp.json"],"Registries":[{"Name":"docker.io","Secur e":true}]}

5.在自己的spring-boot項目的pom中添加插件

<plugin>

<groupId>com.spotify</groupId>

<artifactId>

docker-maven-plugin

</artifactId>

<version>0.4.14</version>

<configuration>

<imageName>website</imageName>

<dockerDirectory>src/main/docker</dockerDirectory>

<dockerHost> http://140.143.0.126:2375</dockerHost>

<resources>

<resource>

<targetPath>/</targetPath>

<directory>${project.build.directory} </directory>

<include>${project.build.finalName}.jar</include>

</resource>

</resources>

</configuration>

</plugin>

6.在工程中新建src/main/docker目錄和Dockerfile文件

# 基於哪個鏡像

FROM java:8

# 將本地文件夾掛載到當前容器

VOLUME /tmp

# 拷貝文件到容器,xinglinglove-website-0.0.1-SNAPSHOT.jar這裏是maven打包後的名字

ADD xinglinglove-website-0.0.1-SNAPSHOT.jar app.jar

RUN bash -c 'touch /app.jar'

# 配置容器啓動後執行的命令

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

7.構建鏡像並上傳至docker使用maven運行如下命令

clean package docker:build -DskipTests

8.上傳成功後,在服務器輸入docker imgaes查看鏡像是否上傳成功

[root@xinglinglove nginx]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

website latest 875823024f38 42 minutes ago 715 MB

9.新建容器,啓動項目

[root@xinglinglove nginx]# docker run -t -d -p 8888:8888 website /bin/bash

10.訪問項目地址,驗證成功(登陸頁有點醜,請見諒)

http://140.143.0.126:8888/login

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章