Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /webData/hua/www.ascwh.com/var/Typecho/Feed.php on line 221
ASCWH - Docker https://ascwh.com/tag/Docker/ Docker 基本命令 https://ascwh.com/137.html 2018-07-18T16:37:00+08:00 1、docker的启动、停止、重启[root@localhost ~]# service docker restart Redirecting to /bin/systemctl restart docker.service [root@localhost ~]# service docker stop Redirecting to /bin/systemctl stop docker.service [root@localhost ~]# service docker start Redirecting to /bin/systemctl start docker.service +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 搜索可用镜像 # docker search imagesName 删除镜像 # docker rmi [-f] imageID/imagesName -f 为强制删除 拉取镜像 # docker pull imagesName:tag 保存镜像到本地 =>>>>保存单个镜像 # docker save imagesName/imageID > /path/image.tar 保存多个镜像 # docker save imagesName1/imageID1 imagesName2/imageID2 [imagesNameN/imageIDn] > /path/to/image_Name.tar 当需要把一台计算机上的镜像迁移到另一台计算机上时,需要保存镜像到本地。然后使用winscp将image.tar从机器a拷到机器b上,最后加载镜像。 加载本地镜像 # docker load -i /path/image.tar 或 # docker load < /path/image.tar 推送镜像到镜像仓库registry # docker push imagesName:tag/imageID 从镜像运行一个容器,并在容器中运行一条命令 # docker run -d imagesName:tag [cmd] 从镜像运行一个容器,并在容器中运行交互式命令 # docker run -it imagesName:tag [cmd] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2、docker创建一个容器[root@localhost ~]# docker run -it -v /docker_test:/asi --name asi_1 centos [root@724e7701f0d4 /]# -i:允许我们对容器内的 (STDIN) 进行交互 -t:在新容器内指定一个伪终端或终端 -v:是挂在宿机目录, /docker_test是宿机目录,/asi是当前docker容器的目录,宿机目录必须是绝对的。 --name:是给容器起一个名字,可省略,省略的话docker会随机产生一个名字3、docker启动的容器列表[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 724e7701f0d4 centos "/bin/bash" 4 minutes ago Up 4 minutes asi_1 f9097691663e centos "/bin/bash" 5 minutes ago Up 5 minutes asi_1 [root@localhost ~]# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 查看正在运行的容器 # docker ps // 列出最近一个运行过的容器 # docker ps -l // 查看所有容器,包括已经停止的 # docker ps -a // 只展示正在运行的容器ID # docker ps -q +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3、查看docker创建的所有容器[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 724e7701f0d4 centos "/bin/bash" 5 minutes ago Up 5 minutes asi_6 f9097691663e centos "/bin/bash" 6 minutes ago Up 6 minutes asi_5 e59a540fb979 centos "/bin/base" 6 minutes ago Created asi_4 ff49dfedea4f centos "/bin/bash" 2 hours ago Exited (137) 10 minutes ago asi_03 d2cc70abb5a5 centos "/bin/bash" 2 hours ago Exited (127) 2 hours ago asi_02 2d48fc5b7c17 centos "/bin/bash" 2 hours ago Exited (127) 2 hours ago asi_01 [root@localhost ~]# docker ps 默认列表是正在启动的容器 -a是显示所有创建的容器4、启动、停止、重启某个docker 容器[root@localhost ~]# docker start asi_01 asi_01 [root@localhost ~]# docker stop asi_01 asi_01 [root@localhost ~]# docker restart asi_01 asi_01 [root@localhost ~]# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # 发送一个信号,等待容器的停止 # docker stop containerID/names # 启动停止的容器 # docker start [-i] containerID/names # 重启运行中的容器 # docker restart containerID/names # 杀死一个容器 # docker kill containerID/names +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5、查看指定容器的日志记录[root@localhost ~]# docker logs -f asi_01 [root@2d48fc5b7c17 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@2d48fc5b7c17 /]# exit exit [root@2d48fc5b7c17 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# cd / [root@2d48fc5b7c17 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@2d48fc5b7c17 /]# mkdir asi [root@2d48fc5b7c17 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var asi [root@2d48fc5b7c17 /]# cd asi [root@2d48fc5b7c17 asi]# ls asi [root@2d48fc5b7c17 asi]# cd asi [root@2d48fc5b7c17 asi]# ls application [root@2d48fc5b7c17 asi]# cd ../../ [root@2d48fc5b7c17 /]# rm -rf asi [root@2d48fc5b7c17 /]# eixt bash: eixt: command not found [root@2d48fc5b7c17 /]# exit +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # docker logs [-f] [-t] [--tail 数字] containerID/names 没加参数,表示查看全部日志;-f –follows=true 默认为false, 表示一直更新,ctrl+c退出;-t –timestamps=true | false 加上时间戳 ; –tail 数字,指定查看最新数目的日志) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6、删除某个容器,若正在运行,需要先停止[root@localhost ~]# docker rm asi_01 Error response from daemon: You cannot remove a running container 2d48fc5b7c17b01e6247cbc012013306faf1e54f24651d5e16d6db4e15f92d33. Stop the container before attempting removal or use -f [root@localhost ~]# docker stop asi_01 asi_01 [root@localhost ~]# docker rm asi_01 asi_01 [root@localhost ~]# 7、删除所有容器[root@localhost ~]# docker rm $(docker ps -a -q) Error response from daemon: You cannot remove a running container 724e7701f0d4a830167e21f75b470235a0e408fd6cc2913403426e96f69cba11. Stop the container before attempting removal or use -f Error response from daemon: You cannot remove a running container f9097691663ee36f9d2ee56afbdcca0eeb8b63e5590ddf18c0c42954c93b9f06. Stop the container before attempting removal or use -f [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# docker stop asi_6 asi_6 [root@localhost ~]# docker stop asi_5 asi_5 [root@localhost ~]# docker rm $(docker ps -a -q) 724e7701f0d4 f9097691663e [root@localhost ~]#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #守护进程相关 开启、停止、重启docker守护进程 # service start/stop/restart docker 显示Docker运行环境信息 # docker info 查看docker守护进程状态 # ps -ef | grep docker +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++