Rapid Construction mirror Docker + SpringBoot2.0

Bowen link

Docker is an open source application container engine that lets developers can package their applications and dependencies into a portable mirror, and then publish to any of the popular Linux or Windows machine can be virtualized. The container is full use of the sandbox mechanism will not have any interface with each other. (Quoted official concept paper focusing docker + springboot2.0 quickly build image, more detailed knowledge of the docker do not go into details)

Docker

As used herein, is CentOs7

docker installation:

yum install docker
systemctl start docker 开启docker

docker: Modify pull mirroring address, docker use the default public warehouses in the country will be relatively slow download

Recommended ustc pull
edit the host machine file: vi /etc/docker/daemon.json
Without this file, please create one.
{
"Registry-Mirrors": [ "https://docker.mirrors.ustc.edu.cn"]
}
Last: Restart dokcer container systemctl restart docker

docker: commonly used commands

docker images listed Mirror
docker rmi Mirror Mirror delete
docker rm container id remove container
docker run -d --name container name -p port mirroring
docker start / stop container id start / stop container
docker rm 'docker ps -a -q' Remove all containers
docker ps to see running container
docker ps -a view the history of run-off containers
docker ps -l to view the most recent run-off container

Private warehouse installation

Execute the following command will start a registry container is used to provide services to the private warehouse
is not first create a mount directory
docker run --name docker-registry -d -p 5000:5000 -v /usr/local/work/registry:/tmp/registry registry

** Use curl -X GET http://192.168.15.129:5000/v2/_catalog view mirror private warehouse information **
If you did not create, or an empty
{ "repositories": []}

So docker support http push mirror
disposed in /etc/docker/daemon.json following properties, if multiple separated by commas.
{"insecure-registries":["192.168.15.129:5000","192.168.15.129"]}
In the configuration /etc/containers/registries.conf
[registries.search] registries = ['docker.io','registry.centos.org','registry.access.redhat.com','192.168.15.129:5000','192.168.15.129']

[registries.insecure] registries =["192.168.15.129:5000","192.168.15.129"]
PW warehouse address is configured docker build their own domain name and address of the
restart Docker
systemctl restart docker.
Download an image from hub.docker.com in Tomcat.
See docker images using image information, comprising IMAGE ID.

Add ip with a private warehouse to the mirror Tag
Docker Tag Tomcat image id 192.168.15.129:5000/tomcat

View docker images mirror, more than a dozen of the tag tomcat image

Push mirroring
docker push 192.168.15.129:5000/tomcat

Delete from the pull hub.docker.com in the mirror

Start a private warehouse mirror
docker run --name tomcatpri -d -p 8080: 8080 192.168.15.129:5000/tomcat

Turn off the firewall, visit Tomcat, success!

Use docker deployment SpringBoot

SpringBoot build a simple project with Docker technology integration, use maven to build docker mirror and pushed to local docker registry, for the last time does not want to deploy only need to have a mirrored boot container according to correspondence.

Environment to build

JDK1.8
SpringBoot2.0.6.RELEASE
Maven 3.6.0
Docker

Creating a project SpringBoot
QQ picture 20191102233115.png

Introducing dependent configuration

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
</parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <docker.registry>192.168.15.129:5000</docker.registry>
  </properties>

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

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- Docker maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <!-- docker私服的地址 -->
                    <dockerHost>http://192.168.15.129:2375</dockerHost>
                    <!--镜像名称以及版本号-->
                    <imageName>${docker.registry}/${project.artifactId}:${project.version}</imageName>
                    <!--依赖的基础镜像-->
                    <baseImage>java</baseImage>
                    <!--Dockerfile的位置 -->
                 <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
                    <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <!-- Docker maven plugin -->
        </plugins>
    </build>

Start writing class
QQ picture 20191102233115.png

Write control type, for receiving a request for http
QQ picture 20191102234012.png

Docker need to create a directory in src / main below, and create a new file Dockerfile

  FROM java:8
    
    MAINTAINER lvy [email protected]
    
    VOLUME /tmp
    
    ADD docker_registry-0.0.1-RELEASE.jar docker_registry.jar
    
    RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' >/etc/timezone
    
    EXPOSE 9000
    
    ENTRYPOINT ["java", "-jar", "docker_registry.jar"]

The contents of Dockerfile inside to do a brief introduction:

1.FROM:指明当前镜像继承的基础镜像,编译当前镜像时候会自动下载基镜像
2.MAINTAINER:当前镜像的作者和邮箱,使用空格隔开
3.VOLUME:挂载目录
4.ADD:从当前工作目录复制文件到镜像目录中并重新命名
5.RUN:在当前镜像上执行Linux命令,,第二个run指令是为了解决容器和宿主机时间不一致的问题
6.EXPOSE:监听的端口号
7.ENTRYPOINT:让容器像一个可执行程序一样运行

Construction of the project

Use docker build the project package to build
mvn clean install -DskipTests docker:build

Then will report an error
Question 1: If you add in the pom file inside the <dockerHost>node and configure the address is PW address we build it, it will be reported 404 NOT PAGE, if not configured node will report a link rejected abnormal (Connection refused)

解决方案:添加一个``<dockerHost>``节点,值为docker所在机器ip:2375,至于为什么要配置2375,后面会介绍。

问题2:这时在进行 ``mvn clean install -DskipTests docker:build``命令会报出同样的错误,链接被拒绝。
导致原因:
在默认情况下Docker守护进程``Unit socket(/var/run/docker.sock)``来进行本地进程通信,而不会监听任何端口,因此只能在本地使用docker客户端或者使用docker api进行操作。如果想在其他主机上操作docker主机,就需要让docker守护进程打开一个http socket,这样才能实现远程通信,我们是在windows上打包构建的,所以需要让centos7的docker服务开启远程访问。
解决方案:
开启docker远程访问,进入docker私服所在的服务器,编辑配置文件
``vim /usr/lib/systemd/system/docker.service``
在``docker.service``的``[Service]``下增加如下内容
```shell
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
```
注意,该端口不是docker私服所在的服务端口,是新开的远程访问端口。进行远程通信的。

2 based on the same basis, we would also like to resume two flexible connections, otherwise execution will not find the quote command

 ln -s /usr/libexec/docker/docker-proxy-current /usr/bin/docker-proxy
 
 cd /usr/libexec/docker/
 ln -s docker-runc-current docker-runc

start up

Re-read configuration files and start docker

systemctl daemon-reload
systemctl restart docker

Build again

mvn clean install -DskipTests docker: build
that actually succeeded. Correct! It's that simple.
QQ picture 20191102234012.png

View mirror, running

Use docker images to view mirror, you will find just the service has been packaged into a mirror.
Use docker run command can start a container service it!
Need docker name + version number of the mirror name at startup. Otherwise it might not be found (on my machine so)
QQ picture 20191102234012.png

Access Services Success

QQ picture 20191102234012.png

Add that: maven built-in variables Description

$ {basedir} project root
$ {project.build.directory} build directory defaults to target
$ {} project.build.outputDirectory build process output directory, defaults to target/classes
$ {project.build.finalName} Output name thereof , defaults to $ {project.artifactId} - $ {project.version } current version
$ {project.packaging} package type, default jar

Guess you like

Origin www.cnblogs.com/aliases/p/11785402.html