本文主要描述一下从零开始在CentOS7下,如何用docker部署springboot微服务
docker的安装
安装一些必要的系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加软件源信息:
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新 yum 缓存:
sudo yum makecache fast
安装 Docker-ce:
sudo yum -y install docker-ce
启动 Docker 后台服务
sudo systemctl start docker
测试运行 hello-world
docker run hello-world
Centos7中docker开启远程访问
#vi /usr/lib/systemd/system/docker.service
[Service]
ExecStart=ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://va/run/docker.sock
# systemctl daemon-reload
# systemctl restart docker
#firewall-cmd --zone=public --add-port=2375/tcp --permanent
#systemctl restart firewalld.service
如果是ubuntu系统,则
vi /etc/default/docker
DOCKER_OPTS=”-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375”
service docker restart
springboot项目的打包上传
项目所在windows主机上,配置环境变量,这里的{docker_ip}就是docker所在的linux机器的{docker_ip}
DOCKER_HOST=tcp://{docker_ip}:2375
配置maven的pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed-->
<imageName>fhbean/dockertest</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
maven打包,或在run configuration中goal增加clean package docker:build
mvn clean package docker:build
docker启动容器
[root@s01 ~]# docker run -d -p 8080:8080 -t fhbean/dockertest
这时就可以在浏览器访问了。
参考