◉◡◉ 您好,欢迎到访伊成个人站!

Dockerfile构建SpringBoot镜像并推送到docker公共镜像仓库Dockerhub

写在前面

前期准备工作主要有:准备好必要的环境,确保安装了docker,以及有一个Spring boot项目。

1
tips:本文所有操作均在宿主机上的 VMware (centos 7)中进行.

使用Dockerfile构建SpringBoot镜像

1.新建一个文件夹

1
mkdir /yicheng/passwordGenerator/

2.把Spring boot项目打包成jar放在此目录下,并且新建文件Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
# 使用Java8
FROM java:8
# 作者
MAINTAINER yicheng
# VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名
ADD passwordGenerator-0.0.1-SNAPSHOT.jar pwdGenerator_docker.jar
# 运行jar包
RUN bash -c 'touch /pwdGenerator_docker.jar'
ENTRYPOINT ["java","-jar","/pwdGenerator_docker.jar"]
#暴露8889端口作为微服务
EXPOSE 8999
1
2
[root@localhost passwordGenerator]# ls
Dockerfile passwordGenerator-0.0.1-SNAPSHOT.jar

3.开始构建镜像

1
docker build -t pwdgenerator_docker:1.0 .

主要镜像名不能大写!

4.运行镜像

1
docker run -d -p 8080:8080 【容器id】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost passwordGenerator]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 7d229f7ebdd4 33 minutes ago 680MB
pwdgenerator_docker 1.0 e6c6bbfe9344 33 minutes ago 680MB
neosmemo/memos latest 322df040d768 11 days ago 36MB
tomcat latest 6dd589e60602 6 months ago 474MB
dataengine_docker 1.0 3080d43f20c2 8 months ago 853MB
mysql 5.7 3147495b3a5c 9 months ago 431MB
nacos/nacos-server 1.4.2 938169b118c7 24 months ago 956MB
portainer/portainer latest 980323c8eb3f 2 years ago 196MB
redis 5.0.9-alpine 4e5490070cce 2 years ago 29.2MB
redis 6.0.8 16ecd2772934 2 years ago 104MB
java 8 d23bdf5b1b1b 6 years ago 643MB
[root@localhost passwordGenerator]#
[root@localhost passwordGenerator]# docker run -d -p 8999:8999 7d229f7ebdd4

伊成博客

到此我们完成了第一个小目标,使用Dockerfile构建SpringBoot镜像并且成功的运行了。

发布镜像到Docker-hub

1.访问 https://hub.docker.com 注册账号,这里的用户名和密码一定要记住了,后续需要。

2.登录docker-hub,输入用户名和密码

1
2
3
4
5
6
7
8
9
[root@localhost passwordGenerator]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 输入你的用户名
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

3.提交镜像至Docker-hub
命令:docker push / :

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost passwordGenerator]# docker push yicheng2023/pwdgenerator:1.0
The push refers to repository [docker.io/yicheng2023/pwdgenerator]
f6462cee0153: Pushed
dd456a172e38: Pushed
35c20f26d188: Mounted from library/java
c3fe59dd9556: Mounted from library/java
6ed1a81ba5b6: Mounted from library/java
a3483ce177ce: Mounted from library/java
ce6c8756685b: Mounted from library/java
30339f20ced0: Mounted from library/java
0eb22bfb707d: Mounted from library/java
a2ae92ffcd29: Mounted from library/java
1.0: digest: sha256:273386989c0f4cf97842dc0cf3187ab2788564528cd642cbd11159c6693636fc size: 2424

伊成博客

到此就完成了将镜像推送到Docker-hub公共仓库的所有步骤。


The end.

支付宝打赏 微信打赏