SpringBoot + Docker 实现一次构建到处运行
Java技术精选
共 8530字,需浏览 18分钟
·
2023-08-09 13:47
1
2
FROM openjdk:8-jre
MAINTAINER Micromaple <micromaple@qq.com>
RUN mkdir /app
COPY my-project-server-1.0.0-SNAPSHOT.jar /app/app.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar", "--spring.profiles.active=prod,druid-prod"]
EXPOSE 8899
-
FROM:指定基础镜像,项目是使用jdk8开发的项目,所以我们指定的基础镜像为openjdk:8-jre -
MAINTAINER:项目维护人员 -
RUN:执行命令,在根目录下创建app目录 -
COPY:将宿主机当前目录下的my-project-server-1.0.0-SNAPSHOT.jar文件拷贝到app目录下,并重命名为app.jar -
ENTRYPOINT:指定容器启动程序及参数 -
EXPOSE:指定运行时容器提供服务的端口
mkdir -p /usr/local/docker/my-project/docker
![](https://filescdn.proginn.com/0efa3d346e55777b562fdddcf9361e79/d02615b8616529233c3aa42afbbe2a28.webp)
docker build -t my-project-server:v1 .
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-project-server v1 ed30386b06d2 11 seconds ago 334MB
openjdk 8-jre 26ac3f63d29f 9 months ago 273MB
3
mkdir -p /usr/local/docker/registry
cd /usr/local/docker/registry
version: '3.1'
services:
registry:
image: registry
restart: always
container_name: registry
ports:
- 5000:5000
volumes:
- ./data:/var/lib/registry
docker-compose up -d
mkdir -p /usr/local/docker/docker-registry-frontend
cd /usr/local/docker/docker-registry-frontend
version: '3.1'
services:
frontend:
image: konradkleine/docker-registry-frontend:v2
ports:
- 8080:80
volumes:
- ./certs/frontend.crt:/etc/apache2/server.crt:ro
- ./certs/frontend.key:/etc/apache2/server.key:ro
environment:
- ENV_DOCKER_REGISTRY_HOST=192.168.110.158(Docker仓库的IP)
- ENV_DOCKER_REGISTRY_PORT=5000
docker-compose up -d
![](https://filescdn.proginn.com/46fe85398627d7443942a2e39d2189fe/968cfb927ef4053b2343846797ac79a5.webp)
vi /etc/docker/daemon.json
"insecure-registries": [
"Docker仓库的IP:5000"
]
{
"registry-mirrors": [
"https://xxx.mirror.aliyuncs.com"
],
"insecure-registries": [
"192.168.110.158:5000"
]
}
-
registry-mirrors:为镜像加速地址,这里为我自己申请的加速地址。大家可以自己申请一个,也可以在网上找一个。 -
insecure-registries:Docker仓库的IP。
systemctl daemon-reload
systemctl restart docker
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-project-server v1 6af7d633afb7 5 seconds ago 334MB
openjdk 8-jre 26ac3f63d29f 9 months ago 273MB
1)、镜像标记
docker tag my-project-server:v1 192.168.110.158:5000/my-project-server:v1
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.110.158:5000/my-project-server v1 6af7d633afb7 3 minutes ago 334MB
my-project-server v1 6af7d633afb7 3 minutes ago 334MB
openjdk 8-jre 26ac3f63d29f 9 months ago 273MB
$ docker push 192.168.110.158:5000/my-project-server
The push refers to repository [192.168.110.158:5000/my-project-server]
5b9e874b9f9c: Pushed
e87c042d22f8: Pushed
b4cfcb8385a8: Pushed
2b730cf18c09: Pushed
edeaba958753: Pushed
8bf42db0de72: Pushed
31892cc314cb: Pushed
11936051f93b: Pushed
v1: digest: sha256:5c8a0efff409649a389d0bc74dda94ca96e67e87c92673b4c7dad0078657af40 size: 2000
$ curl 192.168.110.158:5000/v2/_catalog
{"repositories":["my-project-server"]}
docker rmi my-project-server:v1 192.168.110.158:5000/my-project-server:v1
docker pull 192.168.110.158:5000/my-project-server:v1
$ docker pull 192.168.110.158:5000/my-project-server:v1
v1: Pulling from my-project-server
0e29546d541c: Already exists
9b829c73b52b: Already exists
cb5b7ae36172: Already exists
99ce012bef04: Already exists
22dc2a72d098: Already exists
9c69a57e10d9: Already exists
776f54050ab5: Pull complete
65a83a9a7871: Pull complete
Digest: sha256:5c8a0efff409649a389d0bc74dda94ca96e67e87c92673b4c7dad0078657af40
Status: Downloaded newer image for 192.168.110.158:5000/my-project-server:v1
192.168.110.158:5000/my-project-server:v1
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.110.158:5000/my-project-server v1 6af7d633afb7 15 minutes ago 334MB
openjdk 8-jre 26ac3f63d29f 9 months ago 273MB
4
mkdir -p /usr/local/docker/my-project
cd /usr/local/docker/my-project
version: '3.1'
services:
my_project_server:
image: 192.168.110.158:5000/my-project-server:v1
container_name: my-project-server
restart: always
ports:
- 8899:8899
volumes:
- ./logs:/logs
environment:
TZ: Asia/Shanghai
docker-compose up -d
docker ps -a
![](https://filescdn.proginn.com/62f70e2c2162cda0dd153b3e2fa062ae/630b069344602efb807c8bf47f254783.webp)
来源:blog.csdn.net/qq_41779565/article/details/127356651
评论