赞
踩
1.1 配置Docker2375 端口
找到 docker.service 配置文件
编辑:vim /usr/lib/systemd/system/docker.service,在 ExecStart=/usr/bin/dockerd-current 后增加如下配置
-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
- systemctl daemon-reload
-
- systemctl restart docker
1.2 配置 Docker 地址,构建 Docker 镜像并上传
配置 Docker 地址
- <properties>
- <!-- docker配置 -->
- <docker.host>http://192.168.56.101:2375</docker.host>
- <docker.maven.plugin.version>1.2.2</docker.maven.plugin.version>
- <docker.images.prefix>microservice</docker.images.prefix>
- </properties>
配置构建 Docker 镜像信息
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <!-- 引入系统范围内的依赖 -->
- <configuration>
- <includeSystemScope>false</includeSystemScope>
- </configuration>
- </plugin>
- <!-- docker镜像 -->
- <plugin>
- <groupId>com.spotify</groupId>
- <artifactId>docker-maven-plugin</artifactId>
- <version>${docker.maven.plugin.version}</version>
- <executions>
- <execution>
- <id>build-image</id>
- <phase>package</phase>
- <goals>
- <goal>build</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <imageName>${docker.images.prefix}/${project.artifactId}:${project.version}</imageName>
- <dockerHost>${docker.host}</dockerHost>
- <baseImage>java:8</baseImage>
- <entryPoint>["java", "-jar", "-Dspring.profiles.active=test","/${project.build.finalName}.jar"]
- </entryPoint>
- <resources>
- <resource>
- <targetPath>/</targetPath>
- <directory>${project.build.directory}</directory>
- <include>${project.build.finalName}.jar</include>
- </resource>
- </resources>
- </configuration>
- </plugin>
- <!--添加配置跳过测试-->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.22.2</version>
- <configuration>
- <skipTests>true</skipTests>
- </configuration>
- </plugin>
- </plugins>
- </build>
点击 maven 中 package 打包并上传
1.3 查看镜像
docker images
1.4 安装运行容器
docker run -p 8084:8040 --restart=always --name microservice-project -v /etc/localtime:/etc/localtime -v /mydata/app/project/logs:/var/logs -d microservice/cloud-project:0.0.3-SNAPSHOT
2.1 在服务器指定位置创建目录,如:project_docker,在该文件夹下创建 Dockerfile
vim Dockerfile
- # 该镜像需要依赖的基础镜像
- FROM java:8
- # 指定维护者的名字
- LABEL maintainer=lc
- # 定义匿名数据卷
- VOLUME /tmp
- # COPY 复制资源到容器中
- COPY cloud-project-0.0.3-SNAPSHOT.jar /app/cloud-project-0.0.3-SNAPSHOT.jar
- COPY application.properties application.properties
- # 指定docker容器启动时运行jar包和配置文件
- ENTRYPOINT ["java","-Xms256m", "-Xmx256m","-jar","/app/cloud-project-0.0.3-SNAPSHOT.jar","--spring.config.location=/application.properties","-Dfile.encoding=UTF-8"]
2.2 dockerfile常用指令
2.3 上传微服务jar,构建镜像
docker build -t cloud-project:0.0.3 .
2.4 安装运行容器
docker run -p 8848:8848 --restart=always --name cloud-project -v /home/project/jar/project_docker/logs:/logs -d cloud-project:0.0.3
2.5 编写自动化脚本
vim run.sh
- #!/bin/bash
- # 定义应用组名
- group_name='scistor'
- # 定义应用名称
- app_name='cloud-project'
- # 定义应用版本
- app_version='0.0.3-SNAPSHOT'
- # 定义应用环境
- profile_active='qa'
- echo '----copy jar----'
- docker stop ${app_name}
- echo '----stop container----'
- docker rm ${app_name}
- echo '----rm container----'
- docker rmi ${group_name}/${app_name}:${app_version}
- echo '----rm image----'
- # 打包编译docker镜像
- docker build -t ${group_name}/${app_name}:${app_version} .
- echo '----build image----'
- docker run -p 8844:8844 --restart=always --name ${app_name} -v /home/project/jar/project_docker/logs:/logs -d ${group_name}/${app_name}:${app_version}
- echo '----start container----'
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。