玖叶教程网

前端编程开发入门

Docker常用命令及常见问题解决方法


?1.docker账号

用户名:username

密码:pwd

2.docker的目标

构建:制作一个docker镜像

运输:docker pull

运行:启动一个容器

每一个容器都有自己的文件系统rootfs。

docker镜像:dockers images,每一个镜像都可能依赖一个或多个下层的镜像组成的另一个镜像,AUFS文件系统。

docker仓库:docker registry,集中存放镜像的地方。

docker容器:docker containers,镜像运行后的进行。


3.toomanyrequests问题

toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit?

解决办法:

#docker login

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.

Username: username
Password: pwd


4.使用非root用户启用或连接docker时报错

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/json: dial unix /var/run/docker.sock: connect: permission denied

解决办法:

#添加docker用户组

sudo groupadd docker

#检测当前用户是否已经在docker用户组中,其中XXX为用户名

sudo gpasswd -a $XXX docker

#将当前用户添加至docker用户组

sudo gpasswd -a $USER docker

#更新docker用户组

newgrp docker


5.启动docker服务

#systemctl start docker


6.重启docker服务

#systemctl restart docker


7.docker开机自启动

#systemctl enable docker


8.镜像加速器

科大镜像:https://docker.mirrors.ustc.edu.cn/?

网易:https://hub-mirror.c.163.com/?

阿里云:https://eo9szgzj.mirror.aliyuncs.com?

七牛云加速器:https://reg-mirror.qiniu.com?

8.1.通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

{ "registry-mirrors": ["https://eo9szgzj.mirror.aliyuncs.com"] }

8.2.重新启动服务

#systemctl daemon-reload
#systemctl restart docker


9.docker基本命令

Docker版本信息:

#docker version

Docker系统信息:

#docker info


10.镜像管理

查看所有镜像:#docker images
搜索镜像:#docker search nginx
拉取下载镜像:#docker pull nginx
导出:#docker save redis > redis.tar
导入:#docker load < redis.tar
删除:#docker rmi nginx
更改镜像名:#docker tag nginx:latest nginx:test
查看镜像创建历史:#docker history nginx


11.容器管理

使用docker镜像nginx:latest以后台模式启动一个容器,并将容器命名为mynginx:

#docker run --name=mynginx -d nginx:latest

运行容器(因为busybox一运行就会停止,所以可以ping114让容器一直运行)

#docker run --name=mybusybox -d busybox ping 114.114.114.114

查看正在运行的容器:

#docker ps

查看所有的容器,包括未运行的:

#docker ps -a

查看容器中运行的进程:

#docker top 容器id/names

查看资源占用情况:

#docker stats 容器id/names

启动/重启/关闭容器:

#docker start/restart/stop/kill 容器id/names

暂停容器:

#docker pause/unpause 容器id/names

强制删除容器:

#docker rm -f 容器id/names

在运行的容器中执行命令:

#docker exec -it 容器id/names ls(执行一个ls命令)
#docker exec -it 容器id/names /bin/bash(通过exec对指定的容器执行bash)

查看容器日志:

#docker logs -f 容器id/names

查看容器/镜像的元信息:

#docker inspect 容器id/names

查看容器内文件结构:

#docker diff 容器id/names

复制文件:

#docker cp busybox:/etc/hosts .(将busybox容器下的hosts文件复制到当前目录下)


12.删除镜像时报错

Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 9cc090805514 is using its referenced image feb5d9fea6a5

解决办法:

先删除正在使用镜像的容器id:

#docker rm -f 9cc090805514

然后就可以删除镜像了:

#docker rmi hello-world


13.docker进入容器报错

#docker exec -it mybusybox /bin/bash

报错:OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown

解决办法:

使用替换命令:

#docker exec -it 容器id/names /bin/sh

或者:

#docker exec -it 容器id/names sh
#ls
#cd /etc

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言