docker安装与运行web程序


欢迎大家加入CSDN开发云
CSDN开发云
各位朋友,以下是我自学docker部署jar包程序的安装部署步骤。仅供参考
本次是安装在了VMware Workstation Pro虚拟机中,Centos7.5(内核版本3.10.0-957.el7.x86_64),需要提前写好能够验证的java程序

1.自动下载最新版docker命令

curl -s https://get.docker.com | sh

以下为展示安装日志

[root@bogon local]# curl -s https://get.docker.com | sh
Executing docker install script, commit: 3d8fe77c2c46c5b7571f94b42793905e5b3e4                                                                                                                              2e4
+ sh -c 'yum install -y -q yum-utils'
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
警告:/var/cache/yum/x86_64/7/base/packages/libxml2-python-2.9.1-6.el7.5.x86_64.                                                                                                                              rpm:V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
libxml2-python-2.9.1-6.el7.5.x86_64.rpm 的公钥尚未安装
导入 GPG key 0xF4A80EB5:
 用户ID     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org                                                                                                                              >"
 指纹       : 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 软件包     : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
+ sh -c 'yum-config-manager --add-repo https://download.docker.com/linux/centos/                                                                                                                              docker-ce.repo'
已加载插件:fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yu                                                                                                                              m.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
+ '[' stable '!=' stable ']'
+ sh -c 'yum makecache'
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
base                                                     | 3.6 kB     00:00
docker-ce-stable                                         | 3.5 kB     00:00
extras                                                   | 2.9 kB     00:00
updates                                                  | 2.9 kB     00:00
(1/10): docker-ce-stable/7/x86_64/updateinfo               |   55 B   00:00
(2/10): docker-ce-stable/7/x86_64/primary_db               |  55 kB   00:00
(3/10): docker-ce-stable/7/x86_64/other_db                 | 117 kB   00:00
(4/10): docker-ce-stable/7/x86_64/filelists_db             |  24 kB   00:02
(5/10): extras/7/x86_64/other_db                           | 134 kB   00:00
(6/10): updates/7/x86_64/other_db                          | 454 kB   00:01
(7/10): extras/7/x86_64/filelists_db                       | 224 kB   00:03
(8/10): base/7/x86_64/other_db                             | 2.6 MB   00:07
(9/10): updates/7/x86_64/filelists_db                      | 3.4 MB   00:16
(10/10): base/7/x86_64/filelists_db                        | 7.2 MB   00:19
元数据缓存已建立
+ '[' -n '' ']'
+ sh -c 'yum install -y -q docker-ce'
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-20.10.3-3.e                                                                                                                              l7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
docker-ce-20.10.3-3.el7.x86_64.rpm 的公钥尚未安装
导入 GPG key 0x621E9F35:
 用户ID     : "Docker Release (CE rpm) <docker@docker.com>"
 指纹       : 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 来自       : https://download.docker.com/linux/centos/gpg
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker your-user

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemo                                                                                                                              n-attack-surface
         for more information.

2.自动安装完成后执行

docker version

以下为展示日志

Client: Docker Engine - Community
 Version:           20.10.3
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        48d30b5
 Built:             Fri Jan 29 14:34:14 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docke                                                                                                                        r daemon running?

3.展示server命令

service docker start

以下为展示日志

Redirecting to /bin/systemctl start docker.service

4.再确认

docker version

以下为展示日志

Client: Docker Engine - Community
 Version:           20.10.3
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        48d30b5
 Built:             Fri Jan 29 14:34:14 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.3
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       46229ca
  Built:            Fri Jan 29 14:32:37 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

5.验证docker是否安装成功

docker run hello-world

以下为展示日志

0e03bdcc26d7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

以上为安装成功!

6.设置开机自启

systemctl start docker
systemctl enable docker

7.查看启动状态

systemctl status docker

以下为展示日志

● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2021-02-07 11:19:48 CST; 2h 10min ago
     Docs: https://docs.docker.com
 Main PID: 6805 (dockerd)
    Tasks: 10
   Memory: 49.1M
   CGroup: /system.slice/docker.service
           └─6805 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/cont...

2月 07 11:19:47 bogon dockerd[6805]: time="2021-02-07T11:19:47.740356356+0...pc
2月 07 11:19:47 bogon dockerd[6805]: time="2021-02-07T11:19:47.764173992+0...."
2月 07 11:19:48 bogon dockerd[6805]: time="2021-02-07T11:19:48.672975282+0...s"
2月 07 11:19:48 bogon dockerd[6805]: time="2021-02-07T11:19:48.795440720+0...g"
2月 07 11:19:48 bogon dockerd[6805]: time="2021-02-07T11:19:48.921816765+0...."
2月 07 11:19:48 bogon dockerd[6805]: time="2021-02-07T11:19:48.953092771+0....3
2月 07 11:19:48 bogon dockerd[6805]: time="2021-02-07T11:19:48.953380299+0...n"
2月 07 11:19:48 bogon systemd[1]: Started Docker Application Container Engine.
2月 07 11:19:48 bogon dockerd[6805]: time="2021-02-07T11:19:48.988094890+0...k"
2月 07 11:27:30 bogon dockerd[6805]: time="2021-02-07T11:27:30.296721547+0...e"
Hint: Some lines were ellipsized, use -l to show in full.

8.ps -ef|grep docker

以下为展示日志

root      6805     1  0 11:19 ?        00:00:01 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root      7552  6461  0 13:30 pts/0    00:00:00 grep --color=auto docker

9.关闭防火墙或打开某个端口

systemctl stop firewalld

10.设置网关ipv4

vi /usr/lib/sysctl.d/00-system.conf
编辑中增加 net.ipv4.ip_forward=1
编辑后重启网关systemctl restart network
验证修改是否成功sysctl net.ipv4.ip_forward

11.编辑docker设置

新建文件 touch filename

编辑 filename
FROM java:8 (自动下载1.8版本jdk)
VOLUME /tmp     
ADD demo-0.0.1-SNAPSHOT.jar /demo.jar (demo-0.0.1-SNAPSHOT.jar是打好的jar包,demo是自定义命名的镜像名 )
ENTRYPOINT ["java","-Djava.security.egd=file:/usr/local/XXXX(jar包位置)","-jar","/demo.jar"]

12.输入命令安装镜像

docker build -t demo .

注意上方命令中最后有**.**
以下为展示日志

Sending build context to Docker daemon  17.04MB
Step 1/4 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/4 : VOLUME /tmp
 ---> Running in 252764e4bb9c
Removing intermediate container 252764e4bb9c
 ---> 924ebd9618c0
Step 3/4 : ADD demo-0.0.1-SNAPSHOT.jar /demo.jar
 ---> 4adaf794c1e9
Step 4/4 : ENTRYPOINT ["java","-Djava.security.egd=file:/usr/local/software","-j             ar","/demo.jar"]
 ---> Running in 14abd8c750e4
Removing intermediate container 14abd8c750e4
 ---> ba929197535b
Successfully built ba929197535b
Successfully tagged demo:latest

以上显示为成功 无异常

13.重启docker

systemctl restart docker

重启是防止后续运行时报错

14.启动命令

docker run -d -p 端口号:端口号 --name 镜像文件运行名称  镜像文件名称

15.验证是否成功

[root@bogon ]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                    NAMES
9b699d03e15a   demo      "java -Djava.securit…"   5 seconds ago   Up 4 seconds   0.0.0.0:9099->9099/tcp   demo
PORTS不为空即为成功

16.网站验证

服务器IP地址+设置的端口号+/xxx

注意事项

注意:当在虚拟机上进行安装部署时,需要注意关闭虚拟机后再次使用有可能会出现 PORTS 显示Exited (255)的情况,以下为解决方案
1.删掉对应的容器
2.重新使用run命令启动程序

暂时没有更好的解决方案,与开机自启docker没有太大关系,属于关闭宿主机产生的问题。 如果您有更好的解决方案还请不吝赐教!

欢迎大家加入CSDN开发云
CSDN开发云

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值