Docker概述
1.什么是docker
所谓的docker本质上是一款容器管理工具,是一种打包文件的技术。
因为Linux一切皆文件,容器技术本质上是将一个程序及其依赖的库文件打包管理的技术。
2.docker架构
属于典型的C/S架构,服务端由进程”dockerd”启动,客户端由docker命令进行管理。
对于dockerd而言,能够提供如下典型服务:
– 镜像管理
– 官方站点镜像
– 第三方镜像
– 自制镜像
– 容器管理
– 存储卷管理
– 网络管理
3.docker的相关站点
官方镜像站点:
https://hub.docker.com/
github站点:
https://github.com/docker-archive/docker-ce # old,deprecated
https://github.com/moby/moby # new
二进制Docker环境部署
推荐阅读:
https://docs.docker.com/engine/install/ubuntu/
https://docs.docker.com/engine/install/centos/
https://docs.docker.com/engine/install/binaries/
- 1.下载docker
[root@elk91 ~]# wget https://download.docker.com/linux/static/stable/x86_64/docker-28.0.1.tgz
- 2.解压软件包
[root@elk91 ~]# tar xf docker-28.0.1.tgz
[root@elk91 ~]#
[root@elk91 ~]# tree docker
docker
├── containerd
├── containerd-shim-runc-v2
├── ctr
├── docker
├── dockerd
├── docker-init
├── docker-proxy
└── runc
0 directories, 8 files
[root@elk91 ~]#
- 3.将二进制文件拷贝到环境变量中
[root@elk91 ~]# cp docker/* /usr/local/bin/
[root@elk91 ~]#
[root@elk91 ~]# ll /usr/local/bin/
total 206896
drwxr-xr-x 2 root root 4096 Mar 19 10:30 ./
drwxr-xr-x 17 root root 4096 Mar 18 17:10 ../
-rwxr-xr-x 1 root root 40415384 Mar 19 10:30 containerd*
-rwxr-xr-x 1 root root 13299864 Mar 19 10:30 containerd-shim-runc-v2*
-rwxr-xr-x 1 root root 20394136 Mar 19 10:30 ctr*
-rwxr-xr-x 1 root root 41552248 Mar 19 10:30 docker*
-rwxr-xr-x 1 root root 76659456 Mar 19 10:30 dockerd*
-rwxr-xr-x 1 root root 708448 Mar 19 10:30 docker-init*
-rwxr-xr-x 1 root root 2377328 Mar 19 10:30 docker-proxy*
-rwxr-xr-x 1 root root 16426200 Mar 19 10:30 runc*
[root@elk91 ~]#
- 4.启动docker服务端
[root@elk91 ~]# dockerd
- 5.客户端测试
[root@elk91 ~]# docker version
Client:
Version: 28.0.1
API version: 1.48
Go version: go1.23.6
Git commit: 068a01e
Built: Wed Feb 26 10:40:04 2025
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 28.0.1
API version: 1.48 (minimum version 1.24)
Go version: go1.23.6
Git commit: bbd0a17
Built: Wed Feb 26 10:41:19 2025
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.7.25
GitCommit: bcc810d6b9066471b0b6fa75f557a15a1cbf31bb
runc:
Version: 1.2.5
GitCommit: v1.2.5-0-g59923ef
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@elk91 ~]#
6.由于不可抗因素,目前国内无法访问docker官方镜像仓库,需要借助’梯子’
[root@elk91 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Run 'docker run --help' for more information
[root@elk91 ~]#
卸载二进制Docker环境
- 1.停止docker服务
[root@elk91 ~]# ps -ef | grep dockerd
root 79359 24572 1 10:34 pts/0 00:00:00 dockerd
root 79587 66495 0 10:34 pts/2 00:00:00 grep --color=auto dockerd
[root@elk91 ~]#
[root@elk91 ~]# kill 79359
[root@elk91 ~]#
[root@elk91 ~]# docker version
Client:
Version: 28.0.1
API version: 1.48
Go version: go1.23.6
Git commit: 068a01e
Built: Wed Feb 26 10:40:04 2025
OS/Arch: linux/amd64
Context: default
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@elk91 ~]#
- 2.卸载docker环境
[root@elk91 ~]# ll /usr/local/bin/
total 206908
drwxr-xr-x 2 root root 4096 Mar 19 10:30 ./
drwxr-xr-x 17 root root 4096 Mar 18 17:10 ../
-rwxr-xr-x 1 root root 40415384 Mar 19 10:30 containerd*
-rwxr-xr-x 1 root root 13299864 Mar 19 10:30 containerd-shim-runc-v2*
-rwxr-xr-x 1 root root 20394136 Mar 19 10:30 ctr*
-rwxr-xr-x 1 root root 41552248 Mar 19 10:30 docker*
-rwxr-xr-x 1 root root 76659456 Mar 19 10:30 dockerd*
-rwxr-xr-x 1 root root 708448 Mar 19 10:30 docker-init*
-rwxr-xr-x 1 root root 2377328 Mar 19 10:30 docker-proxy*
-rwxr-xr-x 1 root root 16426200 Mar 19 10:30 runc*
[root@elk91 ~]#
[root@elk91 ~]# rm -f /usr/local/bin/*
[root@elk91 ~]#
[root@elk91 ~]# ll /usr/local/bin/
total 8
drwxr-xr-x 2 root root 4096 Mar 19 10:35 ./
drwxr-xr-x 17 root root 4096 Mar 18 17:10 ../
[root@elk91 ~]#
一键部署docker脚本
vim ./install-docker.sh
#!/bin/bash
# auther: violet
# blog: https://www.vionletarchitect.top/
# 加载操作系统的变量,主要是ID变量。
. /etc/os-release
# DOCKER_VERSION=26.1.1
DOCKER_VERSION=20.10.24
# DOCKER_COMPOSE_VERSION=2.27.0
DOCKER_COMPOSE_VERSION=2.23.0
FILENAME=docker-${DOCKER_VERSION}.tgz
DOCKER_COMPOSE_FILE=docker-compose-v${DOCKER_COMPOSE_VERSION}
URL=https://download.docker.com/linux/static/stable/x86_64
DOCKER_COMPOSE_URL=https://github.com/docker/compose/releases/download/v${DOCKER_COMPOSE_VERSION}/docker-compose-linux-x86_64
DOWNLOAD=./download
BASE_DIR=/violet/softwares
OS_VERSION=$ID
# 判断是否下载了docker-compose
function prepare(){
# 判断是否下载docker-compose文件
if [ ! -f ${DOWNLOAD}/${DOCKER_COMPOSE_FILE} ]; then
wget -T 3 -t 2 ${DOCKER_COMPOSE_URL} -O ${DOWNLOAD}/${DOCKER_COMPOSE_FILE}
fi
if [ $? != 0 ];then
rm -f ${DOWNLOAD}/${DOCKER_COMPOSE_FILE}
echo "不好意思,由于网络波动原因,无法下载${DOCKER_COMPOSE_URL}软件包,程序已退出!请稍后再试......"
exit 100
fi
# 给脚本添加执行权限
chmod +x ${DOWNLOAD}/${DOCKER_COMPOSE_FILE}
}
# 定义安装函数
function InstallDocker(){
if [ $OS_VERSION == "centos" ];then
[ -f /usr/bin/wget ] || yum -y install wget
rpm -qa |grep bash-completion || yum -y install bash-completion
fi
if [ $OS_VERSION == "ubuntu" ];then
[ -f /usr/bin/wget ] || apt -y install wget
fi
# 判断文件是否存在,若不存在则下载软件包
if [ ! -f ${DOWNLOAD}/${FILENAME} ]; then
wget ${URL}/${FILENAME} -O ${DOWNLOAD}/${FILENAME}
fi
# 判断安装路径是否存在
if [ ! -d ${BASE_DIR} ]; then
install -d ${BASE_DIR}
fi
# 解压软件包到安装目录
tar xf ${DOWNLOAD}/${FILENAME} -C ${BASE_DIR}
# 安装docker-compose
prepare
cp $DOWNLOAD/${DOCKER_COMPOSE_FILE} ${BASE_DIR}/docker/docker-compose
# 创建软连接
ln -svf ${BASE_DIR}/docker/* /usr/bin/
# 自动补全功能
cp $DOWNLOAD/docker /usr/share/bash-completion/completions/docker
source /usr/share/bash-completion/completions/docker
# 配置镜像加速
install -d /etc/docker
cp $DOWNLOAD/daemon.json /etc/docker/daemon.json
# 开机自启动脚本
cp download/docker.service /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl enable --now docker
docker version
docker-compose version
tput setaf 3
echo "安装成功,欢迎使用violet二进制docker安装脚本,欢迎下次使用!"
tput setaf 2
}
# 卸载docker
function UninstallDocker(){
# 停止docker服务
systemctl disable --now docker
# 卸载启动脚本
rm -f /usr/lib/systemd/system/docker.service
# 清空程序目录
rm -rf ${BASE_DIR}/docker
# 清空数据目录
rm -rf /var/lib/{docker,containerd}
# 清除符号链接
rm -f /usr/bin/{containerd,containerd-shim,containerd-shim-runc-v2,ctr,docker,dockerd,docker-init,docker-proxy,runc}
# 使得终端变粉色
tput setaf 5
echo "卸载成功,欢迎再次使用尹正杰二进制docker安装脚本哟~"
tput setaf 7
}
# 程序的入口函数
function main(){
# 判断传递的参数
case $1 in
install|i)
InstallDocker
;;
remove|r)
UninstallDocker
;;
*)
echo "Invalid parameter, Usage: $0 install|remove"
;;
esac
}
# 向入口函数传参
main $1
- 2.自行配置代理
[root@elk92 ~]# systemctl cat docker
# /lib/systemd/system/docker.service
[Unit]
Description=oldboyedu linux Docke Engine
Documentation=https://docs.docker.com,https://www.oldboyedu.com
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
# 配置docker代理
Environment="HTTP_PROXY=代理ip"
Environment="HTTPS_PROXY=代理ip"
[Install]
WantedBy=multi-user.target
[root@elk92 ~]#
- 3.测试翻墙成功
root@elk92 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:7e1a4e2d11e2ac7a8c3f768d4166c2defeb09d2a750b010412b6ea13de1efb19
Status: Downloaded newer image for hello-world:latest
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/
[root@elk92 ~]#
- 4.使用docker运行nginx服务
[root@elk92 ~]# docker run -p 81:80 --name myweb -d nginx:1.27.4-alpine
Unable to find image 'nginx:1.27.4-alpine' locally
1.27.4-alpine: Pulling from library/nginx
f18232174bc9: Pull complete
ccc35e35d420: Pull complete
43f2ec460bdf: Pull complete
984583bcf083: Pull complete
8d27c072a58f: Pull complete
ab3286a73463: Pull complete
6d79cc6084d4: Pull complete
0c7e4c092ab7: Pull complete
Digest: sha256:4ff102c5d78d254a6f0da062b3cf39eaf07f01eec0927fd21e219d0af8bc0591
Status: Downloaded newer image for nginx:1.27.4-alpine
b03c97981ceacd43c4977dc176b53da242f61c1601e1cdc25ea7e75af14d8f2d
[root@elk92 ~]#
[root@elk92 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b03c97981cea nginx:1.27.4-alpine "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp myweb
[root@elk92 ~]#
- 5.部署服务测试
[root@elk93 ~]# curl -I http://10.0.0.92:81/
HTTP/1.1 200 OK
Server: nginx/1.27.4
Date: Wed, 19 Mar 2025 03:03:04 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 05 Feb 2025 14:46:11 GMT
Connection: keep-alive
ETag: "67a379b3-267"
Accept-Ranges: bytes
[root@elk93 ~]#
docker环境至此部署完毕!
Categories: