运行镜像
如果你直接安装会比较慢, 建议参照附录内容配置镜像之后再执行
# 执行命令过程一:下载容器镜像 docker run -d nginx:latest
命令解释 docker run 启动一个容器 -d 把容器镜像中需要执行的命令以daemon(守护进程)的方式运行 nginx 应用容器镜像的名称,通常表示该镜像为某一个软件 latest 表示上述容器镜像的版本,表示最新版本,用户可自定义其标识,例如v1或v2等
显示内容:
latest: Pulling from library/nginx a2abf6c4d29d: Pull complete a9edb18cadd1: Pull complete 589b7251471a: Pull complete 186b1aaa4aa6: Pull complete b4df32aa5a72: Pull complete a0bcbecc962e: Pull complete Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Status: Downloaded newer image for nginx:latest 80948245dada983ce4b4b359793e1e8784cdef14abcd4113b43d0c0e3e3735f4 [root@centos133 ~]# [root@centos133 ~]# [root@centos133 ~]# docker ps可以查看命令 查看docker运行的信息 [root@centos133 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 80948245dada nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp charming_blackwell [root@centos133 ~]#
ps显示的COMMAND列表示: 容器执行的命令, 容器如果没有命令就会停止所以一定要有命令运行
查看启动容器信息
docker inspect 80948245dada # 容器ID CONTAINER ID查看结构化的容器信息如IP等
以下截取部分
"NetworkSettings": {"Bridge": "","SandboxID": "870897ed599108b1232de00b8f4a76778d6a3c5111fe123c520b05b0ce33b5c7","HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"Ports": {"80/tcp": null},"SandboxKey": "/var/run/docker/netns/870897ed5991","SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "e325e8b758be154354e73a612a00f6b6362984b262080c65f29f1be8e678c3a2","Gateway": "172.17.0.1","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "172.17.0.2","IPPrefixLen": 16,"IPv6Gateway": "","MacAddress": "02:42:ac:11:00:02","Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"NetworkID": "86a8f040da173df21bcc63185563c147bcd0020bbb684945ab79b67b802b1080","EndpointID": "e325e8b758be154354e73a612a00f6b6362984b262080c65f29f1be8e678c3a2","Gateway": "172.17.0.1","IPAddress": "172.17.0.2","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"MacAddress": "02:42:ac:11:00:02","DriverOpts": null}}}} ]
访问启动的容器服务
根据上面的IP
curl http://172.17.0.2
查看容器网络
ip a s
显示内容
# 网络桥接,转发到主机之外的昂罗 5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UPlink/ether 02:42:79:27:66:23 brd ff:ff:ff:ff:ff:ffinet 172.17.0.1/16 brd 172.17.255.255 scope global docker0valid_lft forever preferred_lft foreverinet6 fe80::42:79ff:fe27:6623/64 scope linkvalid_lft forever preferred_lft forever# 与容器虚拟网络同在一个网络空间, 目的是把容器的网络连接到主机 7: vethd45ecca@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UPlink/ether f6:12:4f:64:e6:c0 brd ff:ff:ff:ff:ff:ff link-netnsid 0inet6 fe80::f412:4fff:fe64:e6c0/64 scope linkvalid_lft forever preferred_lft forever 9: vethb3335e1@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UPlink/ether a6:24:37:4c:0d:ca brd ff:ff:ff:ff:ff:ff link-netnsid 1inet6 fe80::a424:37ff:fe4c:dca/64 scope linkvalid_lft forever preferred_lft forever