docker基础命令
#部署docker
##关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@localhost ~]# grep -v "#" /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted
[root@server1 ~]# vi /etc/resolv.conf
nameserver 8.8.8.8
##安装依赖包
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
##设置阿里云镜像源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
#只有在下载完docker.repo配置文件之后,才可以正常安装Docker-CE(社区版Docker引擎)
##安装Docker-CE(社区版Docker引擎)
[root@localhost ~]# yum -y install docker-ce
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
docker镜像操作
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
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/
1、docker client客户端(在前端)连接到了服务端(在后端)(服务端是以一个守护进程的形式跑在操作系统里面的)典型的C/S架构
2、由docker服务端的守护进程从docker hub(doxker镜像仓库)上下载了镜像
3、服务端创建了一个新的容器,然后从拉去的这个镜像启动了一个容器,容器执行了脚本可执行程序让我们可以查看/使用(客户端告诉服务端动作操作,服务端是真正进行操作角色)
4、docker服务端把这些信息流(传递)返回到客户端并展示出来, (展示在终端上或者其他位置)
docker client可以是多种形式, 比如" docker"命令具所在的终端
查询docker 版本
方式①
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 20.10.5
API version: 1.41
Go version: go1.13.15
Git commit: 55c4c88
Built: Tue Mar 2 20:33:55 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.5
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 363e9a8
Built: Tue Mar 2 20:32:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4
GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc:
Version: 1.0.0-rc93
GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
docker-init:
Version: 0.19.0
GitCommit: de40ad0
方式②
[root@localhost ~]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 1
Server Version: 20.10.5
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
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 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc version: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.683GiB
Name: localhost.localdomain
ID: CDT4:OQ2O:SJK4:YD4Q:66RP:VO5V:WXFY:LGWU:G2H2:PNRR:QPRF:SZ3K
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://kwu8d3is.mirror.aliyuncs.com/
Live Restore Enabled: false
搜索镜像
[root@localhost ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14565 [OK]
下载镜像
docker pull 镜像名称
例如:docker pull nginx
[root@localhost docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
查看镜像列表
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 2 months ago 133MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
获取镜像信息
[root@localhost ~]# docker inspect f6d0b4767a6c #获取镜像信息
[root@localhost ~]# docker tag nginx:latest nginx:lnmp #给镜像添加标签
[root@localhost ~]# docker rmi nginx:lnmp #删除标签
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
nginx lnmp f6d0b4767a6c 2 months ago 133MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
镜像导出和导入
导出
docker save -o 文件名 镜像名
示例:
docker save -o nginx_1 nginx:latest
[root@localhost docker]# docker save -o nginx_1 nginx:latest
[root@localhost docker]# ls
daemon.json key.json nginx nginx_1
导入
[root@localhost docker]# docker rmi nginx:latest
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx lnmp f6d0b4767a6c 2 months ago 133MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
[root@localhost docker]# docker load < nginx_1 #导入镜像
Loaded image: nginx:latest
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
nginx lnmp f6d0b4767a6c 2 months ago 133MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
容器操作
查询容器
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff8c4e2afd0e hello-world "/hello" 23 minutes ago Exited (0) 23 minutes ago wonderful_jennings
创建容器
[root@localhost docker]# docker create -it nginx:latest /bin/bash
7050105cee05bb6055f383491739e2ad60ae25c45b1cda80aa70190586e64a64
# -i:让容器的标准输入保持打开
# -t:分配一个伪终端
# /bin/bash 给予执行环境
启动容器
[root@localhost docker]# docker start 7050105cee05
7050105cee05
启动容器(一次性执行)
[root@localhost docker]# docker run centos:7 /usr/bin/bash -c ls /
停止容器
[root@localhost docker]# docker stop 7050105cee05
7050105cee05
持续后台运行
[root@localhost docker]# docker run -d nginx:lnmp /bin/bash -c "while true;do echo hello;done"
8a61f7204df4eca88c8b1b110aa1f5d6dc70755fb926ea705be189635acd8ef9
进入容器
使用run
[root@docker docker]# docker run -it nginx:latest /bin/bash
exec(容器必须正常运行)
[root@localhost docker]# docker run -it nginx:latest /bin/bash
root@d84f134e223d:/#
容器导出
[root@localhost docker]# docker export 8a61f7204df4 > nginx_2
[root@localhost docker]# ls
daemon.json key.json nginx nginx_1 nginx_2
容器导入(生成镜像)
[root@localhost docker]# cat nginx_2 | docker import - nginx:latest
sha256:7679a6d3f4e87b5366a6ea6e17b057239a1caddc314580527046d884dd7fef22
删除容器
[root@docker docker]# docker rm d84f134e223d #这个命令无法删除正在运行的容器,rm -f 可以强制删除
批量删除
[root@docker docker]# docker ps -a | awk '{print docker rm "$1"}' | bash
批量删除"exit"状态的容器
[root@docker docker]# for i in `docker ps -a | grep -i exit | awk '{print $1}'`; do docker rm -f $i;done #过滤异常退出的容器
批量关闭正在运行的容器
[root@docker ~]# for i in `docker ps -a | grep -i up | awk '{print $1}'`; do docker stop $i;done