[root@VM-0-11-centos ~]# sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec 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/
[root@VM-0-11-centos ~]#
四、Docker实战开始
1、Docker Hello World
Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序
1 2 3 4 5 6 7 8 9 10 11
[root@VM-0-11-centos ~]# docker run ubuntu:15.10 /bin/echo "Hello world" Unable to find image 'ubuntu:15.10' locally 15.10: Pulling from library/ubuntu 7dcf5a444392: Pull complete 759aa75f3cee: Pull complete 3fa871dc8a2b: Pull complete 224c42ae46e7: Pull complete Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3 Status: Downloaded newer image for ubuntu:15.10 Hello world [root@VM-0-11-centos ~]#
[root@VM-0-11-centos ~]# docker run -i -t ubuntu:15.10 /bin/bash root@752fdf5d7764:/# cat /proc/version Linux version 3.10.0-1127.19.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Aug 25 17:23:54 UTC 2020 root@752fdf5d7764:/# exit exit [root@VM-0-11-centos ~]#
各个参数解析:
-t: 在新容器内指定一个伪终端或终端。
-i: 允许你对容器内的标准输入 (STDIN) 进行交互。
第二行我们已经进入了一个Ubuntu15.10的容器
第五行可以通过exit或CTRL + D来退出容器
3、后台运行容器
1、后台运行
1 2 3 4 5 6 7 8
[root@VM-0-11-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@VM-0-11-centos ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done" 292152f399f3c2fa1fbac80edaffe2721f6948eba8c0821c652508805b82f2b3 [root@VM-0-11-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 292152f399f3 ubuntu:15.10 "/bin/sh -c 'while t…" About a minute ago Up About a minute blissful_lumiere [root@VM-0-11-centos ~]#
输出详情介绍:
CONTAINER ID: 容器 ID。
IMAGE: 使用的镜像。
COMMAND: 启动容器时运行的命令。
CREATED: 容器的创建时间。
STATUS: 容器状态。
状态有7种:
created(已创建)
restarting(重启中)
running 或 Up(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡)
PORTS: 容器的端口信息和使用的连接类型(tcp\udp)。
NAMES: 自动分配的容器名称。
2、查看容器的标准输出以及停止容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[root@VM-0-11-centos ~]# docker logs 292152f399f3 hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world [root@VM-0-11-centos ~]# docker stop 292152f399f3 292152f399f3 [root@VM-0-11-centos ~]#
[root@VM-0-11-centos ~]# firewall-cmd --list-ports //查看端口列表 You're performing an operation over default zone ('public'), but your connections/interfaces are in zone 'docker' (see --get-active-zones) You most likely need to use --zone=docker option.
[root@VM-0-11-centos ~]#
开放8080端口
1 2 3 4 5 6 7 8 9 10 11 12 13
[root@VM-0-11-centos ~]# firewall-cmd --add-port=8080/tcp --permanent //开放8081端口 You're performing an operation over default zone ('public'), but your connections/interfaces are in zone 'docker' (see --get-active-zones) You most likely need to use --zone=docker option. success [root@VM-0-11-centos ~]# firewall-cmd --reload //重新加载防火墙 success [root@VM-0-11-centos ~]# firewall-cmd --list-ports//查看端口列表 You're performing an operation over default zone ('public'), but your connections/interfaces are in zone 'docker' (see --get-active-zones) You most likely need to use --zone=docker option. 8080/tcp //开放的端口 [root@VM-0-11-centos ~]#
这时我们可以通过浏览器访问WEB应用
5、Docker镜像使用
1、列出镜像列表
1 2 3 4 5 6 7 8
[root@VM-0-11-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest f643c72bc252 3 weeks ago 72.9MB httpd latest 0a30f4c29d25 4 weeks ago 138MB hello-world latest bf756fb1ae65 11 months ago 13.3kB ubuntu 15.10 9b9cb95443b5 4 years ago 137MB training/webapp latest 6fae60ef3446 5 years ago 349MB [root@VM-0-11-centos ~]#
各个选项说明:
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
2、获取镜像
1
docker pull + 镜像名字
3、查找镜像
1
docker search + 镜像名字
4、删除镜像
1 2 3 4 5
/***docker rmi + 镜像名字***/
[root@VM-0-11-centos ~]# docker rmi hello-world Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 75d905f3f65d is using its referenced image bf756fb1ae65 [root@VM-0-11-centos ~]#