Docker: 运行Java应用

更方便的运行方式

在以前只能在Linux或者Windows系统中一一搭建所有应用程序的开发环境,包括MySQL,Redis,RabbitMQ等.但自从Docker出现之后,改变了这一切.开发环境和运行环境全都可以直接放到Docker容器中运行,最主要的是,搭建方便.基本只需一条命令就可以安装好.

关于Spring Boot的运行

完整源码: Gitee仓库

其实Spring Boot一般有两种运行方式:

  1. 放在Tomcat中,以war包的方式运行.
  2. 在Java命令行中,以jar包的方式运行.

Docker中,推荐以第二种方式更好,更方便一些.

image-2881
docker

推送方式

把Spring Boot打包之后的jar包,推送到Docker有两种方式:

  1. dockerfile Maven插件
  2. jib Maven插件

这里只介绍第一种用的比较多的dockerfile方式(但是,源码里面提供了两种方式的示例: Gitee仓库),源码如下:

pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>xyz.suancaiyu</groupId>
	<artifactId>custom-spring-boot-docker-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>custom-spring-boot-docker-demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<docker.image.prefix>192.168.37.130</docker.image.prefix>
		<java.version>12</java.version>
	</properties>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- tag::plugin[] -->
			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>dockerfile-maven-plugin</artifactId>
				<version>1.4.9</version>
				<configuration>
					<repository>${docker.image.prefix}/${project.artifactId}</repository>
				</configuration>
			</plugin>
			<!-- end::plugin[] -->

			<!-- tag::unpack[] -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>unpack</id>
						<phase>package</phase>
						<goals>
							<goal>unpack</goal>
						</goals>
						<configuration>
							<artifactItems>
								<artifactItem>
									<groupId>${project.groupId}</groupId>
									<artifactId>${project.artifactId}</artifactId>
									<version>${project.version}</version>
								</artifactItem>
							</artifactItems>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>12</source>
					<target>12</target>
				</configuration>
			</plugin>
			<!-- end::unpack[] -->
		</plugins>
	</build>



	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>



</project>

需要注意: docker.image.prefix 这个值是Docker服务端的地址,需要修改为自己的.

java标志
image-2882

Dockerfile 文件:

定义程序的依赖,运行方式:

FROM openjdk:12-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT [“java”,”-cp”,”app:app/lib/*”,”xyz.suancaiyu.customspringbootdockerdemo.CustomSpringBootDockerDemoApplication”]

编译并运行应用程序:

1. (Windows)在本地机器上配置环境变量:

DOCKER_HOST=192.168.37.130

2. (Windows)在当前目录执行下面的语句编译(推荐从菜单栏单独打开Power Shell,或者cmd.):

mvn install dockerfile:build

3. (Linux)在192.168.37.130的机器上运行(启动应用):

docker run -p 8080:8080 -t 192.168.37.130/custom-spring-boot-docker-demo

4. (Windows)在浏览器中访问: http://192.168.37.130:8080/

更多可以参考: Spring官方示例

完整源码: Gitee仓库

Fedora 31:运行Docker遇到的问题

image-2822
docker

错误详情

docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused “process_linux.go:297: applying cgroup configuration for process caused \”open /sys/fs/cgroup/docker/cpuset.cpus.effective: no such file or directory\””: unknown.

fedora31发布
image-2823

问题导致

在Fedora 31中默认开启了cgroups V2,Docker还没有支持这个版本.但对cgroups V2的支持正在开发中,估计后续版本会支持.

解决办法

在Fedora 31中禁用cgroups V2即可.

执行下面的语句会重启系统,请注意提前保存数据及相关工作进度!

sudo grubby --update-kernel=ALL --args="systemd.unified_cgroup_hierarchy=0"
sudo reboot

解决办法参考自: https://github.com/docker/cli/issues/297#issuecomment-547022631

Docker:Centos 7 中的Docker提供远程服务

Docker开启远程

文件:

/usr/lib/systemd/system/docker.service

修改为下面内容:

# vim /usr/lib/systemd/system/docker.service  
[Service]  
ExecStart=  
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

docker重新读取配置文件,重新启动docker服务

systemctl daemon-reload
systemctl restart docker

会显示正在监听2375端口:

ps -ef|grep docker  
image-2735
docker

禁用centos 7 防火墙(正式环境慎用)

systemctl start firewalld         # 启动,
systemctl enable firewalld        # 开机启动
systemctl stop firewalld          # 关闭
systemctl disable firewalld       # 取消开机启动
service firewalld restart 重启
firewall-cmd --state # 查看状态
firewall-cmd –list-all # 查看防火墙规则

添加自定义端口:

firewall-cmd --permanent --add-port=9527/tcp
  1. firewall-cmd:是Linux提供的操作firewall的一个工具;
  2. –permanent:表示设置为持久;
  3. –add-port:标识添加的端口;

测试是否开放

直接浏览器打开:http://192.168.2.100:2375/version

其中192.168.2.100为主机IP.

CentOS 7:安装Docker

版本信息

  1. centos 7
  2. Docker version 18.09.2, build 6247962

docker LOGO

安装docker

移除旧的版本:

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

安装一些必要的系统工具:

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

CentOS 7配置阿里镜像加速服务

sudo cp -n /lib/systemd/system/docker.service /etc/systemd/system/docker.service
sudo sed -i "s|ExecStart=/usr/bin/docker daemon|ExecStart=/usr/bin/docker daemon --registry-mirror=|g" /etc/systemd/system/docker.service
sudo sed -i "s|ExecStart=/usr/bin/dockerd|ExecStart=/usr/bin/dockerd --registry-mirror=|g" /etc/systemd/system/docker.service
sudo systemctl daemon-reload
sudo service docker restart

文中的加速器地址,请到阿里云容器Hub服务控制台查看。

登录docker hub

docker login

输入https://hub.docker.com/的用户名和密码,

登录成功会提示:

Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

进行测试:

docker pull hello-world

成功则提示:

Status: Downloaded newer image for hello-world:latest