docker学习笔记1(在线离线安装、卸载、镜像加速、常用命令)
Centos7安装docker引擎
官方安装文档
https://docs.docker.com/engine/install/centos/#install-using-the-repository
Docker分两个大版本,一个是CE,一个是EE。
CE为社区版,免费,支持周期为7个月。
EE为企业编,付费,支持周围为24个月。
这边以docker CE版的安装为例。
1.卸载
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
卸载时,镜像、容器、卷、网络都不会自动删除。(/var/lib/docker/)
2.安装方式很多种,有用在线仓库安装,有用离线包安装,我这边结合两种:
2.1使用在线仓库安装
安装yum-utils工具(包含yum-config-manager工具)
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加repo
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
官方的repo包下不来,考虑国内镜像(例如阿里镜像源、清华源、163等)
清华源镜像参考:https://mirror.tuna.tsinghua.edu.cn/help/docker-ce/
阿里云镜像参考:https://developer.aliyun.com/mirror/docker-ce
sudo yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum makecache fast
默认安装最新的版本:
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
docker-ce 是社区版的docker-server,docker引擎
docker-ce-cli 是客户端软件 client
docker-compose-plugin 是compose插件,用来批量启动多个容器,在单台机器上
containerd.io 底层用来启动容器的
docker-buildx-plugin 构建多架构镜像插件
当然你也可以指定版本
yum list docker-ce --showduplicates | sort -r
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-buildx-plugin docker-compose-plugin
趁机捞下包( docker-ce 23.0.1):
sudo mkdir docker-ce-centos7-offline-installer
cd docker-ce-centos7-offline-installer
sudo yum install --downloadonly --downloaddir=./ docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
[root@demo01 docker-ce-centos7-offline-installer]# ls
audit-2.8.5-4.el7.x86_64.rpm
audit-libs-2.8.5-4.el7.x86_64.rpm
audit-libs-python-2.8.5-4.el7.x86_64.rpm
checkpolicy-2.5-8.el7.x86_64.rpm
containerd.io-1.6.18-3.1.el7.x86_64.rpm
container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm
docker-buildx-plugin-0.10.2-1.el7.x86_64.rpm
docker-ce-23.0.1-1.el7.x86_64.rpm
docker-ce-cli-23.0.1-1.el7.x86_64.rpm
docker-ce-rootless-extras-23.0.1-1.el7.x86_64.rpm
docker-compose-plugin-2.16.0-1.el7.x86_64.rpm
docker-scan-plugin-0.23.0-3.el7.x86_64.rpm
fuse3-libs-3.6.1-4.el7.x86_64.rpm
fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm
libcgroup-0.41-21.el7.x86_64.rpm
libsemanage-python-2.5-14.el7.x86_64.rpm
policycoreutils-2.5-34.el7.x86_64.rpm
policycoreutils-python-2.5-34.el7.x86_64.rpm
python-IPy-0.75-6.el7.noarch.rpm
setools-libs-3.3.8-4.el7.x86_64.rpm
slirp4netns-0.4.3-4.el7_8.x86_64.rpm
2.2使用离线包安装
sudo yum install *.rpm -y
服务启动并加入开机自启动
systemctl enable docker.service docker.socket containerd.service
systemctl start docker.service docker.socket containerd.service
systemctl status docker.service docker.socket containerd.service
测试
初学者肯定要来个hello world,以下解释的是,为了打印出hello world,docker做了哪些事情(过程)。
[root@demo01 docker-ce-centos7-offline-installer]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb
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@demo01 docker-ce-centos7-offline-installer]# docker version
Client: Docker Engine - Community
Version: 23.0.1
API version: 1.42
Go version: go1.19.5
Git commit: a5ee5b1
Built: Thu Feb 9 19:51:00 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 23.0.1
API version: 1.42 (minimum version 1.12)
Go version: go1.19.5
Git commit: bc3805a
Built: Thu Feb 9 19:48:42 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.18
GitCommit: 2456e983eb9e37e47538f59ea18f2043c9a73640
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
组件版本:
[root@demo01 docker-ce-centos7-offline-installer]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.10.2
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.16.0
Path: /usr/libexec/docker/cli-plugins/docker-compose
scan: Docker Scan (Docker Inc.)
Version: v0.23.0
Path: /usr/libexec/docker/cli-plugins/docker-scan
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 1
Server Version: 23.0.1
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 2456e983eb9e37e47538f59ea18f2043c9a73640
runc version: v1.1.4-0-g5fd4c4d
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.934GiB
Name: demo01
ID: d0487dac-199e-4557-ac1b-63ae44d156a4
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
删除docker
删除安装包:
yum remove docker-ce
删除镜像、容器、配置文件等内容:
rm -rf /var/lib/docker
镜像加速
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://f5l1gayk.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
docker常用命令
搜索仓库镜像:docker search 镜像名
拉取镜像:docker pull 镜像名
查看正在运行的容器:docker ps
查看所有容器:docker ps -a
删除容器:docker rm container_id
查看镜像:docker images
删除镜像:docker rmi image_id
启动(停止的)容器:docker start 容器ID
停止容器:docker stop 容器ID
重启容器:docker restart 容器ID
启动(新)容器:docker run -it ubuntu /bin/bash
进入容器:docker attach 容器ID或docker exec -it 容器ID /bin/bash,推荐使用后者。
更多命令docker help