1、开启Docker daemon服务远程访问
修改docker.service配置
[root@localhost docker.registry:5000]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.3 (Maipo)
vim /lib/systemd/system/docker.service
不同系统路径可能会有差别,找到
ExecStart=/usr/bin/dockerd
添加 -H tcp://0.0.0.0:5200 -H unix:///var/run/docker.sock
重启docker
sudo service docker start
验证
[root@localhost ~]# docker -H 172.16.1.146:5200 images
REPOSITORY TAG IMAGE ID CREATED SIZE
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713171831 063b60635335 25 minutes ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713171646 3b8deedff90d 27 minutes ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713091144 aa898d0cbd28 32 minutes ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-2018071308 a91e9e3b0678 About an hour ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713083134 a91e9e3b0678 About an hour ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713080550 db286edbbc18 About an hour ago 727 MB
2、MAVEN配置
此处根据dockerfile生成镜像,
pom.xml中添加插件。
<!-- 生成时间戳 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
</execution>
</executions>
<configuration>
<name>current.time</name>
<pattern>yyyyMMddHHmmss</pattern>
<timeZone>GMT+8</timeZone>
</configuration>
</plugin>
<!-- 打包生成镜像、push镜像到私有镜像中心 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>default</id><!-- 要绑定到的生命周期的阶段 -->
<phase>install</phase><!-- 要绑定到的生命周期的阶段 -->
<goals> <!-- 要绑定的插件的目标 -->
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 私有镜像中心的用户名 -->
<username>registry</username>
<!-- 私有镜像中心的密码 -->
<password>xxxx</password>
<!-- 172.16.1.146:8888:私有镜像中心地址; wondertek/${project.artifactId}:镜像名称-->
<repository>172.16.1.146:8888/wondertek/${project.artifactId}</repository>
<!-- 镜像版本号 -->
<tag>${project.version}-${current.time}</tag>
<buildArgs>
<!-- 参数,提供给dockerfile使用 -->
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
<JAR_FILE>target/docker-test-1.0.0.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
3、dockerfile配置
官网说明:https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
# 拉取基础镜像
FROM hub.c.163.com/library/java:8-jdk
# 镜像的作者
MAINTAINER [email protected]
#挂载目录,通过 VOLUME 指令创建的挂载点,无法指定主机上对应的目录,是自动生成的
VOLUME ["/data1","/data2"]
RUN ["mkdir", "-p", "/app"]
#结合maven插件dockerfile-maven-plugin的打包使用
ARG JAR_FILE
ADD ${JAR_FILE} /app/app.jar
#为后面的 RUN, CMD, ENTRYPOINT, ADD 或 COPY 指令设置镜像中的当前工作目录。
#WORKDIR /usr/local/docker/test
#拷贝当前目录文件到容器/app
#COPY . /app
#与 COPY 类似,从 build context 复制文件到镜像。不同的是,如果 src 是归档文件(tar, zip, tgz, xz 等),文件会被自动解压到 dest。
#ADD src dest
#设置环境变量,环境变量可被后面的指令使用
ENV EVN_SET_TEST "WELCOME TO DOCKERFILE CONTAINER!'
##################
# RUN、CDM、ENTRYPOINT 命令都包含两种格式:Shell 格式和 Exec 格式
# CMD还可以放在ENTRYPOINT后,为其传递参数。
##### shell 格式:######
## 底层会调用 /bin/sh -c <command>
# 在容器中运行指定的命令
RUN echo $EVN_SET_TEST
# 容器启动命令 只有最后一个生效,CMD 可以被 docker run 之后的参数替换。
#只有最后一个生效
CMD echo "CMD Hello world"
#配置容器启动时运行的命令
ENTRYPOINT echo "ENTRYPOINT Hello, $EVN_SET_TEST"
###### Exec 格式: #####
## 当指令执行时,会直接调用 <command>,不会被 shell 解析
# ENTRYPOINT ["/bin/echo", "Hello, $EVN_SET_TEST"]
# 正确写法应该为:
# ENTRYPOINT ["/bin/sh", "-c", "echo Hello, $EVN_SET_TEST"]
# 为Exec 格式的ENTRYPOINT传递参数,结果输出Hello, $EVN_SET_TEST dockerfile world
# CMD ["dockerfile world"]
#只有最后一个生效
ENTRYPOINT ["java","-jar","/app/app.jar"]
#表示哪个端口提供服务的提示,宿主机如果要访问,需要结合-P参数联合使用。
EXPOSE 8080
4、build publish镜像
配置远程docker环境变量
export DOCKER_HOST=tcp://172.16.1.146:5200
windows直接在环境变量中添加。
mvn命令执行
build镜像:
mvn clean package dockerfile:build -DskipTests
发布到镜像中心,注意,该命令不会build镜像。
mvn dockerfile:push
配置中 build 和 publish 都绑定在install周期上。
如果不想触发相关流程,可以添加相关例外参数。
mvn clean install -Ddockerfile.skip
命令 | 说明 |
---|---|
dockerfile.skip | Disables the entire dockerfile plugin; all goals become no-ops. |
dockerfile.build.skip | Disables the build goal; it becomes a no-op. |
dockerfile.tag.skip | Disables the tag goal; it becomes a no-op. |
dockerfile.push.skip | Disables the push goal; it becomes a no-op. |
远程镜像查看:
[root@localhost log]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713171831 063b60635335 2 days ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713171646 3b8deedff90d 2 days ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713091144 aa898d0cbd28 2 days ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-2018071308 a91e9e3b0678 2 days ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713083134 a91e9e3b0678 2 days ago 727 MB
172.16.1.146:8888/wondertek/docker-test 1.0.0-20180713080550 db286edbbc18 2 days ago 727 MB
127.0.0.1:5200/docker-test 1.0.0-20180705042837 1e5b41afb0a1 10 days ago 727 MB
dockerlocal/docker-test 1.0.0 7d1c3e14aede 10 days ago 643 MB
docker.io/postgres 10-alpine e273e6bfba1c 2 weeks ago 39.5 MB
镜像历史层级查看:
–no-trunc 查看完整的命令
[root@localhost log]# docker history 172.16.1.146:8888/wondertek/docker-test:1.0.0-20180716100040
IMAGE CREATED CREATED BY SIZE COMMENT
c4cf1fc7cae8 About a minute ago /bin/sh -c #(nop) EXPOSE 8080/tcp 0 B
539db240ca1f About a minute ago /bin/sh -c #(nop) ENTRYPOINT ["java" "-ja... 0 B
cccad169f4df About a minute ago /bin/sh -c #(nop) ENTRYPOINT ["/bin/sh" "... 0 B
e16f9ebbab1b About a minute ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "ec... 0 B
d06785ad2059 About a minute ago |1 JAR_FILE=target/docker-test-1.0.0.jar /... 0 B
2517b2a49809 About a minute ago /bin/sh -c #(nop) ENV EVN_SET_TEST=WELCOM... 0 B
7daa3e4150f8 About a minute ago /bin/sh -c #(nop) ADD file:be73bf23e734c61... 83.6 MB
d42fa5d86ebf About a minute ago /bin/sh -c #(nop) ARG JAR_FILE 0 B
7b0922396475 About a minute ago /bin/sh -c #(nop) VOLUME [/data1 /data2] 0 B
beb6d7c28971 About a minute ago /bin/sh -c #(nop) MAINTAINER [email protected] 0 B
a001fc27db5a 19 months ago /bin/sh -c /var/lib/dpkg/info/ca-certifica... 418 kB
<missing> 19 months ago /bin/sh -c set -x && apt-get update && a... 352 MB
<missing> 19 months ago /bin/sh -c #(nop) ENV CA_CERTIFICATES_JAV... 0 B
<missing> 19 months ago /bin/sh -c #(nop) ENV JAVA_DEBIAN_VERSION... 0 B
<missing> 19 months ago /bin/sh -c #(nop) ENV JAVA_VERSION=8u111 0 B
<missing> 19 months ago /bin/sh -c #(nop) ENV JAVA_HOME=/usr/lib/... 0 B
<missing> 19 months ago /bin/sh -c { echo '#!/bin/sh'; echo 's... 87 B
<missing> 19 months ago /bin/sh -c #(nop) ENV LANG=C.UTF-8 0 B
<missing> 19 months ago /bin/sh -c echo 'deb http://deb.debian.org... 55 B
<missing> 19 months ago /bin/sh -c apt-get update && apt-get insta... 1.29 MB
<missing> 19 months ago /bin/sh -c apt-get update && apt-get insta... 123 MB
<missing> 19 months ago /bin/sh -c apt-get update && apt-get insta... 44.3 MB
<missing> 19 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 19 months ago /bin/sh -c #(nop) ADD file:1d214d2782eaccc... 123 MB