docker安装与常用的命令详解

在下编不完 1年前 ⋅ 1423 阅读

docker安装与常用的命令详解

centos7 的安装方式

参考安装的手册
https://docs.docker-cn.com/engine/installation/linux/docker-ce/centos/#prerequisites

1、安装必要的一些系统工具

sudo yum clean all && yum update && yum makecache

sudo yum install -y yum-utils device-mapper-persistent-data lvm2 

2、添加软件源信息

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 

3、更新并安装 Docker-CE

sudo yum makecache fast

sudo yum -y install docker-ce

4、开启Docker服务

sudo service docker start 

镜像加速 针对Docker客户端版本大于 1.10.0 的用户 您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://smpgb2bd.mirror.aliyuncs.com"]}EOF sudo systemctl daemon-reload sudo systemctl restart docker 

centos6 的安装方式

yum install -y epel-release yum install -y docker-io 

安装后的配置文件:

/etc/sysconfig/docker 

启动docker:

service docker start 

windows安装docker需要注意的事项:

一.需要安装Docker Toolbox工具;

二.设置镜像加速:

  • 1、运行docker-machine ssh default连接虚拟机
  • 2、修改
"https://registry.docker-cn.com" ] }EOF 
  • 3、退出ssh,重启docker-machine restart default

其他的命令:

  • 运行docker-machine stop default,停止运行中的虚拟机
  • 运行docker-machine rm default,删除默认的虚拟机

其他的安装方式

  • 1、 更新软件包和系统内核(等待更新完,需要一定的时间)
yum update 
  • 2、清空历史
sudo yum remove docker docker-client docker-client-latest docker- common docker-latest docker-latest-logrotate docker-logrotate docker- selinux docker-engine-selinux docker-engine 
  • 3、安装必要的包
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 
  • 4、添加yum源
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker- ce/linux/centos/docker-ce.repo
  • 5、更新 yum 缓存
sudo yum makecache fast 
  • 6、查询可用的docker版本
yum list docker-ce --showduplicates | sort -r 
  • 7、安装docker
sudo yum install docker-ce-版本号 (as: sudo yum install docker-ce- 18.06.3.ce-3.el7) 

安装可能存在的问题

a、问题一

failure: repodata/repomd.xml from mirrors.aliyun.com_docker-: [Errno 256] No more mirrors to try.
http://mirrors.aliyun.com/docker-/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found

1、进入目录

cd /etc/yum.repos.d

2、执行rm -rf删除所有

rm -rf *

3、更新yum

yum update

4、再按照步骤安装docker

b、启动时,出现 Failed to start docker.service: Unit not found

[root@anjingsi ~]# sudo service docker start
Failed to start docker.service: Unit not found. 

1、这是因为缺少 rhel-push-plugin.socket 单元,该单元是rhel-push-plugin软件包的一部分。所以执行以下指令就可以成功解决:

curl -sSL https://get.docker.com/ | sh

常用的命令

查看docker版本

docker version

启动docker

service docker start 或者 systemctl start docker

设置开机自启

systemctl enable docker

重启docker

service docker restart 或者 systemctl restart docker

停止docker

systemctl stop docker

检索镜像

docker search xxx(镜像名)

下载镜像

docker pull xxx(镜像名)

带版本号下载镜像

docker pull xxx(镜像名):[tag版本号]

列出镜像列表

docker images

删除镜像

docker rmi xxx(镜像名)

列出当前所有正在运行的container

docker ps

列出所有的container

docker ps -a

删除容器

docker rm 容器ID或者容器名

停止容器

docker stop 容器ID或者容器名

重启容器

docker restart 容器ID或者容器名

关闭docker

systemctl stop docker

进入容器内部

docker exec -it 容器id /bin/bash

删除所有镜像信息

docker rmi $(docker images -q)

删除所有镜像信息

docker rm $(docker ps -aq)

停止所有容器信息

docker stop $(docker ps -aq)

获取容器ip

docker inspect redis_s | grep IPAddress

想要删除untagged images,也就是那些id为的image

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

强制停止容器

docker kill 容器ID或者容器名

从一个容器中取日志

docker logs Name/ID

读取docker容器日志的后一千行信息的命令

docker logs -f --tail=1000 容器ID或者容器名

查看容器的日志
docker logs -f -t --tail 容器ID
#  -t 是加入时间戳  --tail 数字 显示最后多少条 -f 跟随最新的日志打印
将已有镜像重新打包

docker commit -m=“提交的描述信息” -a=“作者” 容器ID 要创建的目标镜像名:[标签名]

docker命令参数总结

命令 英文注释 含义
attach Attach to a running container 当前 shell 下 attach 连接指定运行镜像
build Build an image from a Dockerfile 通过 Dockerfile 定制镜像
commit Create a new image from a container changes 提交当前容器为新的镜像
cp Copy files/folders from the containers filesystem to the host path 从容器中拷贝指定文件或者目录到宿主机中
create Create a new container 创建一个新的容器,同 run,但不启动容器
diff Inspect changes on a container's filesystem 查看 docker 容器变化
events Get real time events from the server 从 docker 服务获取容器实时事件
exec Run a command in an existing container 在已存在的容器上运行命令
export Stream the contents of a container as a tar archive 导出容器的内容流作为一个 tar 归档文件[对应 import ]
history Show the history of an image 展示一个镜像形成历史
images List images 列出系统当前镜像
import Create a new filesystem image from the contents of a tarball 从tar包中的内容创建一个新的文件系统映像[对应export]
info Display system-wide information 显示系统相关信息
inspect Return low-level information on a container 查看容器详细信息
kill Kill a running container 停止指定 docker 容器
load Load an image from a tar archive 从一个 tar 包中加载一个镜像[对应 save]
login Register or Login to the docker registry server 注册或者登陆一个 docker 源服务器
logout Log out from a Docker registry server 从当前 Docker registry 退出
logs Fetch the logs of a container 输出当前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT 查看映射端口对应的容器内部源端口
pause Pause all processes within a container 暂停容器
ps List containers 列出容器列表
pull Pull an image or a repository from the docker registry server 从docker镜像源服务器拉取指定镜像或者库镜像
push Push an image or a repository to the docker registry server 推送指定镜像或者库镜像至docker源服务器
restart Restart a running container 重启运行的容器
rm Remove one or more containers 移除一个或者多个容器
rmi Remove one or more images 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run Run a command in a new container 创建一个新的容器并运行一个命令
save Save an image to a tar archive 保存一个镜像为一个 tar 包[对应 load]
search Search for an image on the Docker Hub 在 docker hub 中搜索镜像
start Start a stopped containers 启动容器
stop Stop a running containers 停止容器
tag Tag an image into a repository 给源中镜像打标签
top Lookup the running processes of a container 查看容器中运行的进程信息
unpause Unpause a paused container 取消暂停容器
version Show the docker version information 查看 docker 版本号
wait Block until a container stops, then print its exit code 截取容器停止时的退出状态值

全部评论: 0

    我有话说: