Docker打包镜像 并且推送到阿里云
一 以插件形式
将SpringBoot项目打包成docker镜像并推送到阿里云然后拉取并部署运行完全的操作过程
Step1:环境配置
-
先把docker加速镜像设成阿里云的docker仓库
-
然后再阿里云注册一个阿里云docker仓库
-
然后在本地的/.m2/ 的配置目录下 建立settings.xml
</servers> <server> <!-- 配置于~/.m2/setting.xml 的server的id--> <id>hub-my</id> # id将在后面插件中用到 <username>你的私服的username</username> <password>你私服的密码</password> <configuration> <email>随便填一个email</email> </configuration> </server> </servers>
-
然后在项目的pom.xml的新增以下内容
<properties> <start-class>你自己的Application的 com.rong.fs.FSApplication</start-class> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <docker.repository>registry.cn-hangzhou.aliyuncs.com</docker.repository> </properties> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.1.1</version> </plugin> </plugins> </pluginManagement> </build> <profiles> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <imageName> registry.cn-hangzhou.aliyuncs.com/xurong365/${project.artifactId}:latest </imageName> <registryUrl>${docker.repository}</registryUrl> <workdir>/work</workdir> <rm>true</rm> <baseImage>freemanliu/openjre</baseImage> <entryPoint>["/sbin/tini","java","-Dspring.profiles.active=prod","-Dspring.cloud.config.profile=prod","-XX:+UseG1GC","-XX:+AggressiveOpts","-XX:+UseFastAccessorMethods","-XX:+UseStringDeduplication","-XX:+UseCompressedOops","-XX:+OptimizeStringConcat","-XX:CICompilerCount=8", "-jar", "${project.build.finalName}.jar"]</entryPoint> <pushImage>true</pushImage> <resources> <resource> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!-- 配置于~/.m2/setting.xml 的server的id--> <serverId>hub-my</serverId> <!-- 你的私服地址 --> <registryUrl>${docker.repository}</registryUrl> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
Step2: 运行打包命令
目标将Spring Boot 项目打包成docker镜像 并上送到阿里云 然后从阿里云pull该docker镜像并运行
在控制台中运行插件打包上送阿里云 mvn clean package -Pprod 登陆阿里云并拉取镜像 docker login --username=你的阿里云用户名 registry.cn-hangzhou.aliyuncs.com docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/实际镜像地址:[镜像版本号] docker push registry.cn-hangzhou.aliyuncs.com/实际镜像地址:[镜像版本号] 拉取docker镜像 docker pull registry.cn-hangzhou.aliyuncs.com/实际镜像地址:[镜像版本号] 启动docker镜像 docker run -d -p 本地端口:对外端口 镜像名
二 以Dockerfile的形式
目标有一个Vue的Web项目需要与nginx一起打包成镜像 并推送到阿里云并且pull 下来并运行
Step1: 配置环境
-
在Vue的工程目录中新增Dockerfile这个文件
FROM nginx COPY ./dist /var/html/ ADD ./nginx.conf /etc/nginx/nginx.conf CMD ["nginx","-g","daemon off;"]
-
在Vue的工程项目中在新增nginx.conf文件
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 10240; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; client_max_body_size 100m; client_body_buffer_size 256k; gzip on; gzip_min_length 10k; gzip_buffers 32 32k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types application/json application/javascript text/plain application/x-javascript text/css application/xml; gzip_vary on; # ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) server { listen 80 default; server_name localhost; index index.html; root /var/html; location / { try_files $uri $uri/ /index.html; } } }
Step2: 运行打包命令
在控制台中运行命令 打包并上送到阿里云服务器并pull下来运行
在控制台中打包镜像
docker build -t 自定义的镜像名字 .
登陆阿里云docker镜像仓库
docker login --username=你的用户名 registry.cn-hangzhou.aliyuncs.com
给该镜像先打一个tag
docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/实际镜像名字:[镜像版本号]
docker push registry.cn-hangzhou.aliyuncs.com/实际镜像名字:[镜像版本号]
拉取docker镜像
docker pull registry.cn-hangzhou.aliyuncs.com/实际镜像地址:[镜像版本号]
启动docker镜像
docker run -d -p 本地端口:对外端口 镜像名