<properties>
<docker.image.prefix>springboot</docker.image.prefix>
<docker.image.suffix>weather</docker.image.suffix>
</properties>
<build>
<finalName>weather-api</finalName>
<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>0.4.13</version>
<configuration>
<imageName>${docker.image.prefix}/${docker.image.suffix}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
FROM adoptopenjdk/openjdk8:alpine-jre
MAINTAINER author
EXPOSE 8081
VOLUME /tmp
ADD weather-api.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
mvn package docker:build
第一次会启动慢一些,如果看到如下信息说明构建镜像成功:
Step 1/8 : FROM adoptopenjdk/openjdk8:alpine-jre
---> 11786ee7c1c9
Step 2/8 : MAINTAINER dinglong
---> Running in d4fdbe133c40
Removing intermediate container d4fdbe133c40
---> 5a374268e8e0
Step 3/8 : EXPOSE 8081
---> Running in 5f859c6259ec
Removing intermediate container 5f859c6259ec
---> a7436e452b6e
Step 4/8 : VOLUME /tmp
---> Running in d4f7fc3757a4
Removing intermediate container d4f7fc3757a4
---> 15ab8031b461
Step 5/8 : ADD weather-api.jar app.jar
---> 4823f70b8604
Step 6/8 : RUN sh -c 'touch /app.jar'
---> Running in 3a0b0c05984c
Removing intermediate container 3a0b0c05984c
---> 93eecba147bb
Step 7/8 : ENV JAVA_OPTS=""
---> Running in 046590ea8b01
Removing intermediate container 046590ea8b01
---> 2e63d89f45ed
Step 8/8 : ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
---> Running in fa76d6bcc705
Removing intermediate container fa76d6bcc705
---> 1cf44c745ee7
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 1cf44c745ee7
Successfully tagged springboot/weather:latest
[INFO] Built springboot/weather
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.720 s
[INFO] Finished at: 2022-02-26T12:35:09+08:00
[INFO] ------------------------------------------------------------------------
author@authordeMacBook-Pro ~ % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot/weather latest 1cf44c745ee7 33 seconds ago 192MB
docker run -p 8081:8081 -t springboot/weather:latest
author@authordeMacBook-Pro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5e9681eafcf springboot/weather:latest "sh -c 'java $JAVA_O…" 21 seconds ago Up 19 seconds 0.0.0.0:8081->8081/tcp vigorous_feistel
npm run build
#dockerfile
#使用 nginx:stable作为基础镜像
FROM nginx:stable
#将当前文件夹的dist文件复制到容器的/usr/share/nginx/html目录
COPY ./dist /usr/share/nginx/html/
#声明运行时容器暴露的端口(容器提供的服务端口)
EXPOSE 3001
#CMD:指定容器启动时要运行的命令
CMD ["nginx", "-g", "daemon off;"]
注:docker中安装nginx:stable,命令:docker pull nginx:stable
docker build -t weather-ui:1.0 .
注:创建镜像的仓库是weather-ui, 标签是1.0
author@authordeMacBook-Pro ~ % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
weather-ui 1.0 1f77fa27d336 4 minutes ago 149MB
springboot/weather latest 1cf44c745ee7 7 minutes ago 192MB
docker run -P -d weather-ui:1.0
author@authordeMacBook-Pro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d85f08d9c28f weather-ui:1.0 "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 0.0.0.0:55001->80/tcp, 0.0.0.0:55000->3001/tcp naughty_moore
c5e9681eafcf springboot/weather:latest "sh -c 'java $JAVA_O…" About a minute ago Up About a minute 0.0.0.0:8081->8081/tcp vigorous_feistel
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d85f08d9c28f weather-ui:1.0 "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 0.0.0.0:55001->80/tcp, 0.0.0.0:55000->3001/tcp naughty_moore
注:55001是前端容器暴露的端口