学习目标:
docker学习一、
学习内容:
1、在centos上安装docker
2、docker的初步使用
1、在centos上安装docker
- 使用国内 daocloud 一键安装命令:
curl -sSL https://get.daocloud.io/docker | sh
[root@1 ~]# curl -sSL https://get.daocloud.io/docker | sh
# Executing docker install script, commit: 26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c
+ sh -c 'yum install -y -q yum-utils'
warning: /var/cache/dnf/BaseOS-929b586ef1f72f69/packages/libzstd-1.4.2-2.el8.x86_64.rpm: He
...
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
[root@1 ~]#
- 待docker自动安装好之后,启动docker并检验docker是否安装好
sudo systemctl start docker :启动docker
sudo docker run hello-world hello-world : 映像来验证是否正确安装了 DockerEngine-Community
[root@1 ~]# sudo systemctl start docker
[root@1 ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
Status: Downloaded newer image for hello-world:latest
...
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@1 ~]#
2、docker的初步使用
1、运行hello world
docker run ubuntu:15.10 /bin/echo “Hello world” :Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo “Hello world”,然后输出结果
- docker: Docker 的二进制执行文件
- run: 与前面的 docker 组合来运行一个容器
- ubuntu:15.10:指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库Docker Hub 下载公共镜像
- /bin/echo “Hello world”: 在启动的容器里执行的命令
[root@1 ~]# 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@1 ~]# docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world
2、运行交互式的容器
进入Ubuntu:15.10这个系统容器
- docker 的两个参数 -i -t,让 docker 运行的容器实现"对话"的能力:
-t: 在新容器内指定一个伪终端或终端。
-i: 允许你对容器内的标准输入 (STDIN) 进行交互。
[root@ecs-9da0 ~]# docker run -it ubuntu:15.10 /bin/bash
root@3de39315c277:/# cat /proc/version
Linux version 4.18.0-147.5.1.el8_1.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)) #1 SMP Wed Feb 5 02:00:39 UTC 2020
root@3de39315c277:/#
root@3de39315c277:/# exit
exit
[root@ecs-9da0 ~]#
3、启动容器到关闭容器
- 创建一个以进程方式运行的容器:
[root@ecs-5ccd ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
2a0b740dc630d06b74b0caa8c17c98b4df18dc135316b4fb339c5c52aa5a233e
[root@ecs-5ccd ~]#
[root@ecs-5ccd ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a0b740dc630 ubuntu:15.10 "/bin/sh -c 'while t…" 6 minutes ago Up 6 minutes tender_robinson
- COMMAND: 启动容器时运行的命令
- STATUS: 容器状态
created(已创建)
restarting(重启中)
running 或 Up(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡)
- 使用 docker logs 命令,查看容器内的标准输出:
[root@ecs-5ccd ~]# docker logs 2a0b740dc630
hello world
hello world
hello world
hello world
hello world
...
上面是以ID工作的,下面是以名字工作:
[root@ecs-5ccd ~]# docker logs tender_robinson
hello world
hello world
hello world
4、停止容器
[root@ecs-5ccd ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a0b740dc630 ubuntu:15.10 "/bin/sh -c 'while t…" 19 minutes ago Up 19 minutes tender_robinson
[root@ecs-5ccd ~]# docker stop tender_robinson
tender_robinson
[root@ecs-5ccd ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@ecs-5ccd ~]#