1. docker安装镜像仓库registry:2
# 拉取镜像
docker pull registry:2
#查看镜像
docker images
#运行镜像
docker run -itd -p 5000:5000 --restart=always --privileged=true --name registry2 registry:2
2. Docker开启远程API(可以远程操作配置此项的docker程序)
2.1 此项网上一些方法很容易出现重启失败报错
我是centos7 后面的改为 -H unix://var/run/docker.sock 就会重启失败,不知道为啥(欢迎评论指导)
2.2 想要开启远程访问,就需要修改下面这个配置文件
/usr/lib/systemd/system/docker.service
查看文件默认配置
cat /usr/lib/systemd/system/docker.service
#内容前后省略
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
添加远程访问参数
#插入ExecStart=/usr/bin/dockerd后面
-H tcp://0.0.0.0:2375
修改后文件
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
[Install]
WantedBy=multi-user.target
2.3 刷新配置重启docker
systemctl daemon-reload
systemctl restart docker
2.4 开启防火墙的Docker构建端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload
2.5 测试
docker -H docker配置远程api的服务器ip地址:2375 images
#出现远程docker镜像列表
3. 让Docker支持http上传镜像(在上传的服务器docker配置中配置,当然镜像仓库也可配置上传镜像)
3.1 修改配置
#修改docker 配置位置,没有此文件即为新建
echo '{ "insecure-registries":["镜像仓库ip:5000"] }' > /etc/docker/daemon.json
#或者
vim /etc/docker/daemon.json
#在打开的json文件下添加此项(保证符合json格式)
"insecure-registries":["镜像仓库ip:5000"]
#再刷新重启
systemctl daemon-reload
systemctl restart docker
3.2 修改后文件/etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"],
"insecure-registries":["镜像仓库ip地址:5000"]
}
3.3 开启防火墙的访问镜像仓库信息端口
firewall-cmd --zone=public --add-port=5000/tcp --permanent
firewall-cmd --reload
3.4 测试
web测试查看仓库镜像信息:
http://镜像仓库ip:5000/v2/_catalog
同局域网的配置此项的服务器做镜像上传:
docker pull hello-world:latest
docker iamges
docker tag hello-world:latest 镜像仓库ip:5000/hello:v1
docker -H 镜像仓库ip:2375 images (或者进入镜像仓库查看)
4. 使用maven-docker插件将jar包打包成镜像上传至仓库(示例:IDEA测试;eclipse需要收到安装插件)
4.1 在应用的pom.xml文件中添加docker-maven-plugin的依赖
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>镜像名前缀/${project.artifactId}:${project.version}</imageName>
<dockerHost>http://镜像仓库ip:2375</dockerHost>
<baseImage>java:8</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>
相关配置说明:
executions.execution.phase:此处配置了在maven打包应用时构建docker镜像;
imageName:用于指定镜像名称,mall-tiny是仓库名称,${project.artifactId}为镜像名称,${project.version}为仓库名称;
dockerHost:打包后上传到的docker服务器地址;
baseImage:该应用所依赖的基础镜像,此处为java;
entryPoint:docker容器启动时执行的命令;
resources.resource.targetPath:将打包后的资源文件复制到该目录;
resources.resource.directory:需要复制的文件所在目录,maven打包的应用jar包保存在target目录下面;
resources.resource.include:需要复制的文件,打包好的应用jar包。
4.2 修改application.yml,将localhost改为db(docker中容器之间访问需要进行)
(mysql redis和项目镜像运行在一个dockers中)可以把docker中的容器看作独立的虚拟机,项目中访问localhost自然会访问不到mysql,docker容器之间可以通过指定好的服务名称db进行访问,至于db这个名称可以在运行maven项目镜像时run容器的时候指定。
4.3 使用IDEA打包项目并构建镜像
注意:依赖的基础镜像需要先行下载,否则会出现构建镜像超时的情况,比如我本地并没有java8的镜像,就需要先把镜像pull下来,再用maven插件进行构建。
执行maven的package命令: