**
Docker学习
**
docker常用命令
dockers version // 查看docker的版本***
docker info // 显示docker的系统信息,包括镜像和容器的数量***
若出现一下错误:
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
解决办法:
键入vi /etc/sysctl.conf
然后键入i添加以下内容:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
然后esc退出输入模式,键入 :wq进行保存
最后再执行sysctl -p,问题解决
docker --help // 可以看到所有的docker命令***
镜像命令
docker images // 查看多有本地主机上的镜像*** **
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
docker images -q // 只显示镜像的id***
docker images -a // 列出所有镜像***
docker search // 搜索镜像***
# 可选项,通过搜索来过滤
–filter==STARS=3000表示搜索出来的镜像的stars大于3000
docker pull xx // 下载镜像***
例如下载mysql镜像:
docker pull mysql
下载镜像的指定版本
例如:
docker pull mysql:5.7
docker rmi // 删除镜像***
docker rmi -f 镜像id # 删除指定的镜像
docker rmi -f 镜像id 镜像id 镜像id // 删除多个镜像
docker rmi -f $(docker images -aq) // 删除所有镜像
容器命令
我们有了镜像才可以创建容器,首先下载一个centos镜像来学习
docker pull centos
若出现以下错误:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
解决方法,键入以下内容:
systemctl daemon-reload
systemctl restart docker.service
之后再执行docker pull centos,此时下载成功
新建容器并启动
docker run [可选参数] image
// 参数说明
–name=“Name” 容器名字
-d 后台方式运行
-it 使用交互方式运行,了进入容器查看内容
例如:
[root@bogon yang]# docker run -it centos /bin/bash // 启动并进入容器
[root@230ad9574460 /]# ls // 查看容器内的centos
退出容器
[root@230ad9574460 /]# exit // 从容器中退回到主机,直接容器停止并退出
快捷键ctrl +P+Q // 容器不停止并退出
-p 指定容器的端口 -p 8080:8080
-p有以下三种使用方式:
-p 主机端口:容器端口 (常用)
-p 容器端口
-p ip:主机端口:容器端口
-P 随机指定端口
列出所有运行的容器
docker ps // 列出当前正在运行的容器
docker ps -a // 列出当前正在运行的容器+历史运行过的容器
docker ps -n=? // 显示最近创建的容器
例如:
docker ps -n=2 // 显示最近创建过的2个容器
docker ps -aq // 只显示容器的编号
删除容器
docker rm 容器id // 删除指定的容器,但不能删除正在运行的容器
docker rm -f $(docker ps -aq) // 删除所有的容器
启动和停止容器
docker start 容器id // 启动容器
docker restart 容器id // 重启容器
docker stop 容器id // 停止容器
docker kill 容器id // 强制停止当前容器
后台启动容器
docker run -d 镜像名
docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止nginx,容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了
查看日志
docker logs -tf --tail 10 容器id // 10指的是只显示10条日志
[root@localhost yang]# docker logs -tf --tail 10 3036acf64dab
2022-08-05T02:58:26.368975533Z xx
2022-08-05T02:58:27.372336484Z xx
2022-08-05T02:58:28.375562810Z xx
2022-08-05T02:58:29.378723084Z xx
2022-08-05T02:58:30.382876026Z xx
2022-08-05T02:58:31.385733916Z xx
以下的就不粘上来了
docker logs -tf 容器id // 显示所有日志
docker top 容器id // 查看容器中的进程
docker inspect 容器id // 查看镜像的元数据
进入容器
方式一:docker exec -it 容器id,例如
docker exec -it 3036acf64dab /bin/bash
方式二:docker attach 容器id
二者的区别:
方式一: 进入容器后开启一个新的终端,可以在里面操作(常用)
方式二:进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机
[yang@bogon ~]$ su
密码:
[root@bogon yang]# cd /home
// 查看当前主机目录
[root@bogon home]# ls
yang
[root@bogon home]# cd yang
[root@bogon yang]# cd ..
[root@bogon home]# touch xx.go
[root@bogon home]# ls
xx.go yang
[root@bogon home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcf2226eeb6d centos "/bin/bash" 3 hours ago Up 3 hours elegant_taussig
134d2c961572 centos "/bin/bash" 4 hours ago Up 4 hours focused_buck
// 进入docker容器内部
[root@bogon home]# docker attach fcf2226eeb6d
[root@fcf2226eeb6d /]# cd /home
[root@fcf2226eeb6d home]# ls
// 在容器内新建一个文件
[root@fcf2226eeb6d home]# touch test.go
[root@fcf2226eeb6d home]# exit
exit
[root@bogon home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
134d2c961572 centos "/bin/bash" 4 hours ago Up 4 hours focused_buck
// 将容器内的文件拷贝到主机上
[root@bogon home]# docker cp fcf2226eeb6d:/home/test.go /home
[root@bogon home]# ls
test.go xx.go yang