docker应用容器引擎
1、简介
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
1-1、docker优点
Docker 是一个用于开发,交付和运行应用程序的开放平台。Docker 使您能够将应用程序与基础架构分开,从而可以快速交付软件。借助
Docker,您可以与管理应用程序相同的方式来管理基础架构。通过利用 Docker
的方法来快速交付,测试和部署代码,您可以大大减少编写代码和在生产环境中运行代码之间的延迟。
响应式部署和扩展
Docker 是基于容器的平台,允许高度可移植的工作负载。Docker 容器可以在开发人员的本机上,数据中心的物理或虚拟机上,云服务上或混合环境中运行。
Docker 的可移植性和轻量级的特性,还可以使您轻松地完成动态管理的工作负担,并根据业务需求指示,实时扩展或拆除应用程序和服务。
在同一硬件上运行更多工作负载
Docker轻巧快速。它为基于虚拟机管理程序的虚拟机提供了可行、经济、高效的替代方案,因此您可以利用更多的计算能力来实现业务目标。Docker
非常适合于高密度环境以及中小型部署,而您可以用更少的资源做更多的事情。
2、安装、部署docker
2-1、安装环境
Docker 支持以下的 64 位 CentOS 版本:
- CentOS 7
- CentOS 8
- 更高版本…
2-2、安装步骤
2-2-1、使用官方脚本进行安装
安装命令如下:
[root@localhost ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
也可以使用国内 daocloud 一键安装命令:
[root@localhost ~]# curl -sSL https://get.daocloud.io/docker | sh
2-2-2、手动安装docker
2-2-2-1、卸载旧版本
较旧的 Docker 版本称为 docker 或 docker-engine 。如果已安装这些程序,请卸载它们以及相关的依赖项。
[root@localhost ~]# yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2-2-2-2、安装 Docker Engine-Community
使用 Docker 仓库进行安装
在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,您可以从仓库安装和更新 Docker。
设置仓库
[root@localhost ~]# yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
使用以下命令来替换国内yum源。
阿里云
[root@localhost ~]# yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
清华大学源
[root@localhost ~]# yum-config-manager \
--add-repo \
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
安装 Docker Engine-Community
[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io
2-3、启动docker
[root@localhost ~]# systemctl start docker
2-4、通过运行 hello-world 映像来验证是否正确安装了 Docker Engine-Community 。
运行时提示IPv4转发已禁用、但安装工作正常。
[root@localhost ~]# docker run hello-world
WARNING: IPv4 forwarding is disabled. Networking will not work.
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/
解决方式:
第一步:在宿主机上执行echo “net.ipv4.ip_forward=1” >>/usr/lib/sysctl.d/00-system.conf
第二步:重启network和docker服务
第三步:验证是否成功
[root@localhost ~]# echo "net.ipv4.ip_forward=1" >>/usr/lib/sysctl.d/00-system.conf
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl restart network
[root@localhost ~]# docker run hello-world
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.
问题解决 docker安装完成
3、docker简单应用
3-1、打印 Hello World
Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序。
输出Hello world
[root@localhost ~]# docker run ubuntu /bin/echo "Hello World"
Hello World
各个参数解析:docker run ubuntu /bin/echo “Hello World”
1、docker: Docker 的二进制执行文件。
2、run: 与前面的 docker 组合来运行一个容器。
3、ubuntu 指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
4、/bin/echo "Hello world": 在启动的容器里执行的命令
3-2、运行交互式的容器
我们通过 docker 的两个参数 -i -t,让 docker 运行的容器实现"对话"的能力:
[root@localhost ~]# docker run -i -t ubuntu /bin/bash
root@8f71d04e551e:/#
各个参数解析:docker run -i -t
1、-t: 在新容器内指定一个伪终端或终端。
2、-i: 允许你对容器内的标准输入 (STDIN) 进行交互。
注意第二行 root@8f71d04e551e:/#,此时我们已进入一个 ubuntu系统的容器
查看当前系统的版本信息和当前目录下的文件列表
root@8f71d04e551e:/# ls
bin dev home lib32 libx32 mnt proc run srv tmp var
boot etc lib lib64 media opt root sbin sys usr
可以通过运行 exit 命令或者使用 CTRL+D 来退出容器。
root@8f71d04e551e:/# exit
exit
[root@localhost ~]#
3-3、启动容器(后台模式)
使用以下命令创建一个以进程方式运行的容器
[root@localhost ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
这个长字符串叫做容器 ID,对每个容器来说都是唯一的,我们可以通过容器 ID 来查看对应的容器发生了什么。
首先,我们需要确认容器有在运行,可以通过 docker ps 来查看:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND ...
5917eac21c36 ubuntu:15.10 "/bin/sh -c 'while t…" ...
输出详情介绍:
CONTAINER ID: 容器 ID。
IMAGE: 使用的镜像。
COMMAND: 启动容器时运行的命令。
CREATED: 容器的创建时间。
STATUS: 容器状态。
状态有7种:
1、created(已创建)
2、restarting(重启中)
3、running(运行中)
4、removing(迁移中)
5、paused(暂停)
6、exited(停止)
7、dead(死亡)
3-4、停止容器
使用 docker stop 命令来停止容器:
[root@localhost ~]# docker stop 5917eac21c36
再次查看docker后台运行状态
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
容器已经停止