使用docker部署java服务

将jar包放入某一路径下,在同级目录下编写Dockerfile

[root@VM-16-10-centos images]# vim Dockerfile 
[root@VM-16-10-centos images]# ls
app.1.0.jar  Dockerfile

填写Dockerfile内容

将app.1.0.jar换成你需要的jar即可

FROM  openjdk:8u181-jdk
ADD ./app.1.0.jar /app.jar
ENV JAVA_OPTS=""
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ /etc/timezone
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /app.jar"]

生成镜像

[root@VM-16-10-centos images]# docker build -t app.20221110 .
Sending build context to Docker daemon  16.57MB
Step 1/6 : FROM  openjdk:8u181-jdk
 ---> 82942d9df443
Step 2/6 : ADD ./app.1.0.jar /app.jar
 ---> 891442f71881
Step 3/6 : ENV JAVA_OPTS=""
 ---> Running in 26b6063d7d33
Removing intermediate container 26b6063d7d33
 ---> 859eaa73bbe5
Step 4/6 : ENV TZ=Asia/Shanghai
 ---> Running in 1f77b268d2c2
Removing intermediate container 1f77b268d2c2
 ---> bd69352c500d
Step 5/6 : RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ /etc/timezone
 ---> Running in b525f5c5ca6e
Asia/Shanghai /etc/timezone
Removing intermediate container b525f5c5ca6e
 ---> b09cec0f986c
Step 6/6 : ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /app.jar"]
 ---> Running in 89f63c5cc74d
Removing intermediate container 89f63c5cc74d
 ---> eb540140192e
Successfully built eb540140192e
Successfully tagged app.20221110:latest

查看镜像列表

[root@VM-16-10-centos images]# docker images
REPOSITORY        TAG              IMAGE ID       CREATED          SIZE
app.20221110      latest           eb540140192e   2 minutes ago    640MB

启动容器

[root@VM-16-10-centos images]# docker run --name app -d -p 8080:8088 app.20221110
2b8dcbda2cc22401ee710d78ebbe3323a4109fd002fb0a664f4a9b111ab902c9

查看运行列表

[root@VM-16-10-centos images]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                       NAMES
2b8dcbda2cc2   app.20221110   "sh -c 'java $JAVA_O…"   41 seconds ago   Up 40 seconds   0.0.0.0:8080->8088/tcp, :::8080->8088/tcp   app

停止容器

[root@VM-16-10-centos images]# docker stop app
app
[root@VM-16-10-centos images]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-16-10-centos images]#

删除容器

docker rm app

删除镜像

[root@VM-16-10-centos images]# docker images
REPOSITORY        TAG              IMAGE ID       CREATED          SIZE
app.20221110      latest           eb540140192e   16 minutes ago   640MB
[root@VM-16-10-centos images]# docker rmi eb540140192e
Untagged: app.20221110:latest
Deleted: sha256:eb540140192e63b1c90f651d66b707e9e3266dabc1abbf38fc231b5ee14ea8de
Deleted: sha256:b09cec0f986cc5304413a7f77956fc8a14e524e950cfd6c6176700c589b54129
Deleted: sha256:41e1e05bd983623f606964101b93e75c137408f378da4325ea2158b6d891af79
Deleted: sha256:bd69352c500dc8cfe58f775103f588bd97dd98791d44a71de6f4f089f2a70665
Deleted: sha256:859eaa73bbe5b258e3dbc2e96dc7c221b0168a7bffe6844e0234474edd1d500d
Deleted: sha256:891442f71881c1eaa25861e65172a2379ddc18d5b25a55badfce67863c44b376
Deleted: sha256:c9bba00cd4ce9e756722884d0dfa330ac43cf6fc7900669c6800704b7fc67158
[root@VM-16-10-centos images]# docker images
REPOSITORY        TAG              IMAGE ID       CREATED          SIZE

启动mysql镜像

docker run -itd --name mysql-cillian -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

安装docker-compose

[root@VM-16-10-centos images]# sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   423  100   423    0     0    467      0 --:--:-- --:--:-- --:--:--   466
100 16.2M  100 16.2M    0     0  10.6M      0  0:00:01  0:00:01 --:--:-- 10.6M
[root@VM-16-10-centos images]# sudo chmod +x /usr/local/bin/docker-compose
[root@VM-16-10-centos images]# docker-compose --version
docker-compose version 1.25.1, build a82fef07

编写docker-compose文件

version: '2'
services:
        app:
                image: app
                container_name: appV1
                ports:            ###将端口映射到宿主
                        - "8888:8080"

启动

[root@VM-16-10-centos images]# docker-compose up -d app
Creating network "images_default" with the default driver
Creating images_app_1 ... done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值