1. Docker入门
1.1 容器的定义
Docker是什么?
官方定义docker是以Docker容器为资源分割和调度的基本单位,封装了整个软件运行时的环境,为开发者和系统管理员设计的,用于构建,发布,和运行分布式应用的平台。
docker使用GO语言开发
- 跨平台
- 可移植
- 简单易用
1.2 容器带来的好处
- 持续部署与测试
- 跨平台支持
- 环境标准化与版本控制
- 高资源利用率和隔离
- 容器跨平台性与镜像
- 易于理解且易用
- 应用镜像仓库
1.3 与虚拟机的比较
传统方式:再硬件层面进行的虚拟化操作,额外增加了虚拟机管理程序和虚拟机操作系统
docker方式:在操作系统层面实现的虚拟化技术,直接复用了宿主机的操作系统,更加轻量化
2. Docker核心概念及安装
2.1 Docker组成
镜像(image):
镜像可以理解为一个模板,可以通过这个模板创建容器服务;
类似于虚拟机的镜像,当把镜像运行起来以后,就是一个容器
镜像由多层组成,每层叠加以后都就是一个独立的对象
容器(container):
docker容器类似于一个轻量级的沙箱,docker利用容器来运行和隔离应用
容器就是镜像运行起来的实例,可以把他启动,停止,删除,而这些容器相互隔离,互不可见
目前可以把他理解成一个简易的Linux系统
docker仓库
仓库就是存放镜像的地方,仓库分为私有仓库和公有仓库
dickerHub 国外的
2.2 centos安装、卸载
1. 卸载旧版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2. 安装yum-utils
sudo yum install -y yum-utils
3. 设置镜像的仓库
#国外的 比较慢
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo #国外的地址
#国内的 阿里云镜像仓库
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4. 更新 yum软件索引
yum makecache fast
5. 安装docker
docker-ce 是社区版,docker-ee 企业版
#安装最新版
sudo yum install docker-ce docker-ce-cli containerd.io
#安装指定版本
#列出可用版本
yum list docker-ce --showduplicates | sort -r
#安装
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
6. 启动docker
#启动
sudo systemctl start docker
#查看当前版本,是否启动成功
docker version
#设置自动开机
systemctl enable docker
7. 进行测试 运行hello-world 验证docker是否安装合适
sudo docker run hello-world
8 .卸载
#卸载 Docker Engine、CLI 和 Containerd 包:
sudo yum remove docker-ce docker-ce-cli containerd.io
#主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有镜像、容器和卷:
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
2.3 配置阿里云镜像加速
1. 进入阿里云官网,搜索容器镜像服务
2. 执行命令即可
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://nm111qru.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2.4 docker运行流程
3. docker常用命令
1. 基础命令
#查看版本
docker version
#查看docker系统信息
docker info
#docker 帮助命令
docker 【命令】 --help
#例如:
docker images --help
docker ps --help
docker network --help
2. 容器命令
2.1 查询镜像命令
#查询镜像
docker search mysql
#搜索收藏数大于3000的镜像
docker search mysql --filter=STARS=3000
2.2 拉取镜像
#拉取镜像
docker pull mysql
[root@itLiu ~]# docker pull mysql
Using default tag: latest # 不指定版本 下载时最新的
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #进行分层下载 docker核心 -联合文件系统
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
#下载来源的真实地址 #docker pull mysql等价于docker pull docker.io/library/mysql:latest
2.3 查看所有镜像
#查看本机所有镜像
docker images
[root@itLiu ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wordpress latest b6306a6a7068 4 days ago 616MB
mysql 5.7 738e7101490b 2 weeks ago 448MB
nginx latest f652ca386ed1 2 weeks ago 141MB
# REPOSITORY: 镜像的仓库源
# TAG: 镜像的标签 版本
# IMAGE ID: 镜像的id
# CREATED: 镜像的创建时间
# SIZE: 镜像的大小
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
# -a 列出所有镜像
# -f 过滤查询
# -q 只查询列出镜像id
2.4 查看所有启动的容器
#查看启动容器
docker ps # 列出当前正在运行的容器
-a # 列出所有容器的运行记录
-n=? # 显示最近创建的n个容器
-q # 只显示容器的编号
2.5 删除镜像
#删除镜像
docker rmi [镜像id]
#例如
docker rmi 3218b38490ce
-f #强制删除镜像
#删除所有镜像
docker rmi $(docker images -qa)
2.6 删除容器
#删除容器
docker rm [容器id]
#删除所有容器
docker rm $(docker ps -qa)
2.7 启动,停止
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前运行的容器
docker kill 容器id #强制停止当前容器
2.8 查看日志命令
#日志的查看
docker logs [容器id]
docker logs --help
#常用:
docker logs -tf 容器id
#num为要显示的日志条数
docker logs --tail number 容器id
2. 9 查看容器中进程信息
docker top [容器id]
[root@itLiu ~]# docker top 951d2e32242e
UID PID PPID C STIME TTY TIME CMD
root 5475 5456 0 09:39 ? 00:00:00 nginx: master process nginx -g daemon off;
101 5528 5475 0 09:39 ? 00:00:00 nginx: worker process
101 5529 5475 0 09:39 ? 00:00:00 nginx: worker process
2.10 查看容器的信息
docker inspect [容器id]
2.11 进入容器命令
# 1.进入容器命令
docker exec -it [容器id] /bin/bash
# 2.进入容器命令
docker attach [容器id]
#docker exec 进入容器后开启一个新的终端,可以在里面操作
#docker attach 进入容器正在执行的终端,不会启动新的进程
#退出容器
exit
2.12 拷贝命令
# 容器内文件拷贝到容器外
docker cp 容器id:[容器内路径] 目的主机路径
#例如
[root@itLiu ~]# docker cp 951d2e32242e:/etc/nginx/nginx.conf /home/
[root@itLiu ~]# cd /home/
[root@itLiu home]# ll
total 32
drwxr-xr-x 4 root root 4096 Dec 20 15:15 compose
drwxr-xr-x 5 root root 4096 Dec 20 09:28 dockerfile
drwxr-xr-x 2 root root 4096 Dec 16 17:02 dockerSave
drwxr-xr-x 2 root root 4096 Dec 9 15:42 docker-test-volume
drwxr-xr-x 4 root root 4096 Dec 8 17:56 mysql
-rw-r--r-- 1 root root 648 Nov 2 23:01 nginx.conf
drwxr-xr-x 2 root root 4096 Dec 20 09:14 packmage
drwxr--r-- 2 root root 4096 Dec 20 10:24 sdklog
[root@itLiu home]#
# 容器外文件拷贝到容器内
docker cp 宿主机路径 容器id:[容器内路径]
3.容器命令图解
4. 常用容器部署
4.1 MySQL部署
1. 下载并运行
#拉取并运行容器
docker run -d --name mysql-5.7 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
#参数说明
-d 后台启动
-p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 默认账号root 用户的密码。
2. 进入容器测试连接
[root@itLiu home]# docker exec -it 924e17aba14e /bin/bash
root@924e17aba14e:/# ls
bin dev entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint-initdb.d etc lib media opt root sbin sys usr
root@924e17aba14e:/# mysql -h localhost -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
4.2 nginx部署
1. 拉取镜像并启动
docker run -d --name nginx01 -p 80:80 nginx
2. 测试访问nginx
curl http://127.0.0.1:80
[root@itLiu home]# curl http://127.0.0.1:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@itLiu home]#
# 阿里云服务器浏览器访问注意开放安全组
http://ip+端口
4.3 redis部署
1.拉取镜像并启动
docker run -d --name redis01 -p 6379:6379 redis --requirepass "123456"
--requirepass: 设置密码
2. 测试连接
[root@itLiu home]# docker exec -it 48f8e0c45909 /bin/bash
root@48f8e0c45909:/data# redis-cli
127.0.0.1:6379> exit
4.4 tomcat部署
1. 拉取镜像并启动
docker run -d --name tomcat01 -p 8080:8080 tomcat
2. 测试访问tomcat
curl http://127.0.0.1:8080