文章目录
安装docker
环境准备
- 需要会一点点的Linux基础
- CentOS 7(192.168.119.128 root root)
- 使用MobaXterm远程连接服务器进行操作
环境查看
# 系统内核为 3.10 以上的
[root@master ~]# uname -r
3.10.0-327.el7.x86_64
#系统版本
[root@master ~]# 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/ docker 帮助文档
#1、卸载旧版本
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
#2、需要的安装包
$ sudo yum install -y yum-utils
#3、设置镜像的仓库
$ sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#此处使用了阿里云镜像地址
#更新软件包索引 yum
yum makecache fast
#4、安装docker的相关的依赖 docker-ce社区版 docker-ee企业版
#docker-ce-cli containerd.io
$ sudo yum install docker-ce
#5、启动docker
$ sudo systemctl start docker
#6、检测是否安装成功
$docker version
#7、hello-world
$ sudo 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.
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/
#8、查看下载的hello-world镜像
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 12 months ago 13.3kB
# /var/lib/docker docker的默认工作路径
阿里云镜像加速
- 登录阿里云 找到容器服务
- 找到镜像地址
- 配置使用
$sudo mkdir -p /etc/docker
$sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://********.mirror.aliyuncs.com"]
}
EOF
$sudo systemctl daemon-reload
$sudo systemctl restart docker
回顾HelloWorld流程
run流程
底层原理
Docker是怎么工作的?
Docker是一个Client + Server 结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问
DockerServer接收到DockerClient的指令,就会执行这个命令
Docker为什么比VM快?
- Docker有着比虚拟机更少的抽象层
- docker利用的是宿主机的内核,VM需要Guest OS
所以,新建一个容器的时候,dockers不需要向虚拟机一样重新加载一个内核操作系统,避免引导。虚拟机是加载Guest OS,而docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级启动。
Docker的常用命令
帮助命令
$docker version #docker版本信息
$docker info #显示docker的系统信息 ,包括镜像和容器的数量
$docker 命令 --help #帮助命令
帮助命令地址:
https://docs.docker.com/reference/
镜像命令
docker images 查看镜像
$docker images
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 12 months ago 13.3kB
#解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像大小
#可选项
-a, --all Show all images (default hides intermediate images)
-q, --quiet Only show image IDs
[root@master ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 12 months ago 13.3kB
[root@master ~]# docker images -q
bf756fb1ae65
[root@master ~]# docker images -aq
bf756fb1ae65
docker search 搜索镜像
[root@master ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10380 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3848 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 758 [OK]
percona Percona Server is a fork of the MySQL relati… 519 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 87
#可选项
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
#通过收藏来过滤搜索结果
[root@master ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10380 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3848 [OK]
docker pull 下载镜像
#下载镜像docker pull 镜像名[:tag]
[root@master ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认是latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete #分层下载,docker镜像的核心 联合文件系统
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
69dd1fbf9190: Pull complete
5346a60dcee8: Pull complete
ef28da371fc9: Pull complete
fd04d935b852: Pull complete
050c49742ea2: Pull complete
Digest: sha256:0fd2898dc1c946b34dceaccc3b80d38b1049285c1dab70df7480de62265d6213 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
#以下两条命令等价
$docker pull mysql
$docker pull docker.io/library/mysql:latest
#指定版本下载
[root@master ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists #分层下载的应用,共用前版本,不需要重复下载,极大节省存储
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
7065aaa2655f: Pull complete
b4bc531db40f: Pull complete
8c3e9d7c9815: Pull complete
fadfb9734ed2: Pull complete
Digest: sha256:e08834258fcc0efd01df358222333919df53d4a0d9b2a54da05b204b822e3b7b
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
#可选项
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform
capable
-q, --quiet Suppress verbose output