虚拟机上启动docker容器,个人的话有点强迫症,不是很原因在新的电脑上装虚拟机的软件,突然想起来自己还有一个笔记本,于是安装了虚拟机

奇怪的是我需要从另一台电脑连接上这台虚拟机,于是我需要通过一个局域网来连接上当前的这台虚拟机 于是需要像这样来实现

首先:更改虚拟机的网络网络适配器,将原本NTA 模式的连接换到桥连模式

ifconfig查看虚拟机的地址

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:8bff:fea2:8719  prefixlen 64  scopeid 0x20<link>
        ether 02:42:8b:a2:87:19  txqueuelen 0  (Ethernet)
        RX packets 132  bytes 71503 (69.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 162  bytes 15191 (14.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.102  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::8b76:af99:4f90:7e57  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:89:1c:07  txqueuelen 1000  (Ethernet)
        RX packets 318704  bytes 364897092 (347.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 168588  bytes 18499531 (17.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 68  bytes 5912 (5.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5912 (5.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethb91c37a: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::6cd6:56ff:fe0e:c5a2  prefixlen 64  scopeid 0x20<link>
        ether 6e:d6:56:0e:c5:a2  txqueuelen 0  (Ethernet)
        RX packets 81  bytes 68963 (67.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 101  bytes 10364 (10.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以很明显的看到我当前的这台虚拟机的地址是 192.168.1.102使用Xshell工具连接即可

那么下面就是重点了,如何在当前的虚拟机上运行 tomcat 容器,在保证你的虚拟机环境正常的情况下,安装docker。简单的东西直接百度了,不着重介绍

咳咳,敲黑板,重点拿笔记!

下载镜像

$ docker pull tomcat

查看镜像

$ docker images
  REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  tomcat              latest              d5eef28cf41d        41 hours ago        647MB

启动镜像 (后台运行)

$ docker run -p 8080:8080 -d tomcat
a580ba060ccd9df5cfd3c692159858bab7d4b1d8a6c888d61e3465295d3b2b25

如果在这一步不报错那就说明启动成功了

查看正在运行的镜像

$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
a580ba060ccd        tomcat              "catalina.sh run"   16 minutes ago      Up 16 minutes       0.0.0.0:8080->8080/tcp   epic_murdock
// 运行过的镜像可以通过 $ docker ps -a 查看

访问 http://192.168.1.102:8080/ 可以看到tomcat猫的界面

还有一种情况就是这个镜像访问404

解决办法:

$ docker exec -it 容器id /bin/bash`* 

当前的容器id是我刚才启动的tomcatid,可以通过docker ps查看

$ docker exec -it a580ba060ccd /bin/bash
root@a580ba060ccd:/usr/local/tomcat# 

确定 webapps下没有文件

$ rm -rf webapps
$ mv webapps.dist webapps

此时刷新页面就能看到tomcat猫的