docker
1. 概述
1.1 为什么出现?
开发-上线 两套环境配置!版本更新,环境配置十分麻烦。集群部署麻烦容易出错。费时费力
1.2 官网
基于go语言开发的开源项目
官网:https://www.docker.com/
文档地址:https://www.docs.docker.com/
仓库地址:https://hub.docker.com/
1.3 能干嘛
传统的虚拟机技术
问题:
1 资源占用多
2 冗余步骤多
3 启动很慢
容器化技术
区别:
-
容器内的应用直接运行在宿主机的内核,容器没有自己的内核,也没有虚拟机的硬件
-
每个容器内都有自己的文件系统,互不影响
DevOps (开发运维)
-
应用更快捷的交付和部署
-
更便捷的升级和扩缩容
-
更简单的系统运维
-
更高效的计算资源利用
2. 安装
2.1 基本组成
镜像(image):可以认为是一个模板,可以通过模板创建容器服务
容器(container):独立运行一个或者一组应用
仓库(repository):存放镜像的地方
2.2 安装步骤
环境准备
1 linux服务器
2 远程连接工具,笔者推荐MobaXterm
# 查看系统内核版本 必须3.10 以上
[root@VM-4-5-centos home]# uname -r
3.10.0-1160.71.1.el7.x86_64
# 查看系统版本
[root@VM-4-5-centos home]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
安装
帮助文档:(https://docs.docker.com/engine/install/centos/)
# 1.卸载docker
[root@VM-4-5-centos ~]# yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
# 2.安装基础环境
yum install -y yum-utils
# 3.设置镜像的仓库
# 官方的镜像仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 4. 安装docker
yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 5.启动docker
systemctl start docker
# 查看启动后是否成功 查看版本
docker version
# 6.运行helloworld
docker run hello-world
# 7.查看helloworld进行
docker images
# 8 卸载docker
yum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
3. docker的常用命令
3.1 帮助命令
-
docker version # 版本
-
docker info # 系统信息
-
docker 命令 --help
3.2 镜像命令
- docker images # 查看所有的镜像信息
[root@VM-4-5-centos docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 18 months ago 13.3k
# 解释:
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像id
CREATED 镜像创建时间
SIZE 大小
# docker images 常用参数帮助
[root@VM-4-5-centos docker]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images
-f, --filter filter Filter output based on conditions provided
-q, --quiet Only show image IDs
-
docker search 镜像名
docker search nginx
-
docker pull [镜像名]:[TAG] # 拉取下载镜像
[root@VM-4-5-centos ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx f1f26f570256: Pull complete 84181e80d10e: Pull complete 1ff0f94a8007: Pull complete d776269cad10: Pull complete e9427fcfa864: Pull complete d4ceccbfc269: Pull complete Digest: sha256:617661ae7846a63de069a85333bb4778a822f853df67fe46a688b3f0e4d9cb87 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest
-
docker rmi -f 镜像id #删除指定id的镜像
-
docker rmi -f 镜像id 镜像id #删除多个id的镜像
-
docker rmi -f $(docker images -aq) #删除全部镜像
3.3 容器命令
有了镜像才可以创建容器
# 下载centos镜像
docker pull centos
新建容器并启动进入容器(基础centos版本 一些命令不存在)
docker run -参数 image
# 常用参数
--name="centos01" # 容器名字
-d # 后台启动
-it # 使用交互方式运行进入容器查看内容
-p # 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-P # 随机指定端口 大写P
[root@VM-4-5-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 18 months ago 231MB
# 启动centos镜像并进入
[root@VM-4-5-centos ~]# docker run -it centos /bin/bash
[root@fef04e2ecee5 /]#
[root@fef04e2ecee5 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
列出所有运行的容器 ; docker ps
docker ps
#
-a # 正在运行的容器+历史运行过的容器
-q # 只显示容器的编号
[root@VM-4-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aaaf63031cf8 centos "/bin/bash" 12 seconds ago Up 11 seconds unruffled_leavitt
退出容器
-
exit # 直接停止容器并退出
-
Ctrl + P + Q # 容器不停止并退出
删除容器
docker rm 容器id # 删除指定容器 不能删除运行中的容器
docker rm -f $(docker ps -aq) #删除所有容器
启停容器的操作
docker start 容器id # 启动
docker restart 容器id # 重启
docker stop 容器id # 停止
docker kill 容器id # 关闭
3.4 其他命令
后台启动容器
# 查看镜像
[root@VM-4-5-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 18 months ago 231MB
# 启动镜像
[root@VM-4-5-centos ~]# docker run -d centos
30190016abc1124f8c5f7e067426e65e605b49ba75fe4711b07b07dea9b07f4e
# 查看运行的镜像
[root@VM-4-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# ??? 镜像停止了
# 常见问题:docker容器使用后台运行,就必须有一个前台进程,发现没有应用就会自动停止
查看日志
docker logs
docker logs -f -t --tail 10 [CONTAINER_ID]
查看容器内部的进程
docker top CONTAINER_ID
查看容器镜像元数据
docker inspect CONTAINER_ID\CONTAINER_NAME
进入当前正在运行的容器
docker exec -it 容器id /bin/bash # 开启一个新的终端
docker attach 容器id # 进入正在运行的终端
主机与容器文件复制
# 容器内复制文件到主机 主机到容器使用挂载 -v
docker cp 容器id:/home/container/test.txt /home
4. 使用docker+nginx
# 1 搜索nginx
docker search nginx
# 2 下载nginx 不加版本号默认下载最新版本
docker pull nginx
# 3 查看下载的镜像
[root@VM-4-5-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ac232364af84 16 hours ago 142MB
# 4 启动nginx 并指定映射端口
[root@VM-4-5-centos ~]# docker run -d --name nginx01 -p:13344:80 nginx
# 5 查看启动的镜像
[root@VM-4-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
58d71cbf7990 nginx "/docker-entrypoint.…" 54 seconds ago Up 54 seconds 0.0.0.0:13344->80/tcp, :::13344->80/tcp nginx01
# 6 启动后可以在网页返回13344端口查看nginx页面,
# nginx启动成功并且通过13344端口映射到容器内的80端口
# 如果是云服务器,需要关闭防火墙并设置安全组
# 7 进入容器 并查看配置文件路径
[root@VM-4-5-centos home]# docker exec -it nginx01 /bin/bash
root@58d71cbf7990:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
5. 容器数据卷
容器内的数据不能持久化,需要将容器内的数据与外部文件进行关联,进行持久化!
挂载命令 -v 虚拟机路径:容器路径
docker run -it -v /home/test:/home nginx
# 可以查看docker容器 info中的Mounts