跳转至

docker安装

一. docker的概念

1. 容器是什么

在生活中,容器就是用来盛放东西的

"容器": 计算机容器中放的是各种服务,应用程序等

2. docker的特点

一次构建,到处运行

只要构建成功一次,需要运行该容器时,只需要将容器移动到相对应的位置/机器。

3. 容器和虚拟机的区别

虚拟机 容器
体积 大 小
占用空间 大 小
启动速度 分钟级 秒级
内核 独立内核 共用宿主机内核
隔离性 完全隔离 使用namespace隔离

二. docker安装

1. 卸载已安装的Docker

[root@4 ~]# dnf -y remove docker-ce\
> docker-clinet \
> docker-clinet-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-selinux \
> docker-engine-selinux \
> docker-engine \
> docker \
> docker-ce-cli

2. 安装一些必要的系统工具

[root@4 ~]# dnf -y install yum-utils

3. 添加软件源信息

rpm -ivh  https://mirrors.aliyun.com/epel/epel-release-latest-9.noarch.rpm
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4. 更新并安装Docker-CE

[root@4 ~]# dnf clean all && dnf makecache
[root@4 ~]# dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

附: 安装Docker-CE的历史版本

yum-config-manager --enable docker-ce-test
yum list docker-ce.x86_64 --showduplicates | sort -r
yum -y install docker-ce-版本号

5. 开启Dokcer服务

[root@4 ~]# systemctl start docker
[root@4 ~]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

三. 配置Docker加速器

[root@4 ~]# mkdir -p /etc/docker
[root@4 ~]# cat > /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://docker.1panel.live","https://docker.m.daocloud.io","https://p8k3kz6h.mirror.aliyuncs.com"]
}
EOF
[root@4 ~]# systemctl daemon-reload
[root@4 ~]# systemctl restart docker
[root@4 ~]# systemctl enable docker

四. 验证Dokcer是否安装成功

1. 查看镜像

[root@4 ~]# docker images
                                               i Info →   U  In Use
IMAGE          ID             DISK USAGE   CONTENT SIZE   EXTRA
httpd:latest   393435ee1a31        175MB         47.6MB    U   

2. 创建并运行容器然后真机访问

[root@4 ~]# docker run -d -p 80:80  httpd
786231a93a999e69dac49fabbee7bbd047ef9827a7e4e3bb62ce5e9678c2730c
docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint interesting_rubin (1b2c499f8af8df371ff0b73e00ae6d4bd66c44db97e39ae7c759ed54ce6ab88e): Bind for 0.0.0.0:80 failed: port is already allocated

Run 'docker run --help' for more information
[root@4 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                                 NAMES
ee4a429a71eb   httpd     "httpd-foreground"   31 seconds ago   Up 31 seconds   0.0.0.0:80->80/tcp, [::]:80->80/tcp   focused_hopper
# 访问web界面
[root@4 ~]# curl localhost
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>It works! Apache httpd</title>
</head>
<body>
<p>It works!</p>
</body>
</html>

五. 镜像相关操作

1. 查询相关镜像

[root@4 ~]# docker search nginx

2. 拉取镜像

格式: docker pull 镜像名称[:tag]

说明:如果不写[:tag]默认为lastest(最新版)

[root@4 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1645c1e06f46: Pull complete 
acf093e7a04f: Pull complete 
df68ee7e7a00: Pull complete 
1b30016634d5: Pull complete 
cd9307c9ecd8: Pull complete 
fcb6fd84b2a0: Pull complete 
1cf7d051b485: Download complete 
e2c07e54e55a: Download complete 
Digest: sha256:ec4ed8b5299e5e90694af7750eb6dffd2627317d30544d056b0371f8082f7bce
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@4 ~]# docker images ls
                                                                                                      i Info →   U  In Use
IMAGE   ID             DISK USAGE   CONTENT SIZE   EXTRA
No images found matching "ls": did you mean "docker image ls"?

说明:

REPOSITORY 镜像名称

TAG 镜像版本

IMAGE ID 镜像的ID号

CREATED 当前时间距离镜像创建的时间

SIZE 镜像的大小

3. 将拉取的镜像打包备份

[root@4 ~]# docker save -o /opt/busybox_web_images.tar nginx:latest
[root@4 ~]# ls /opt
busybox_web_images.tar  containerd

4. 删除系统指定的docker镜像

[root@4 ~]# docker rmi nginx:latest
Untagged: nginx:latest
Deleted: sha256:ec4ed8b5299e5e90694af7750eb6dffd2627317d30544d056b0371f8082f7bce
# 或
docker image rm httpd:latest
# 或