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
2、docker创建一个容器
[root@localhost ~]# docker run -it -v /docker_test:/yufei --name yufei_6 centos
[root@724e7701f0d4 /]#
-i:允许我们对容器内的 (STDIN) 进行交互
-t:在新容器内指定一个伪终端或终端
-v:是挂在宿机目录, /docker_test是宿机目录,/yufei是当前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 yufei_6
f9097691663e centos "/bin/bash" 5 minutes ago Up 5 minutes yufei_5
[root@localhost ~]#
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 yufei_6
f9097691663e centos "/bin/bash" 6 minutes ago Up 6 minutes yufei_5
e59a540fb979 centos "/bin/base" 6 minutes ago Created yufei_4
ff49dfedea4f centos "/bin/bash" 2 hours ago Exited (137) 10 minutes ago yufei_03
d2cc70abb5a5 centos "/bin/bash" 2 hours ago Exited (127) 2 hours ago yufei_02
2d48fc5b7c17 centos "/bin/bash" 2 hours ago Exited (127) 2 hours ago yufei_01
[root@localhost ~]#
docker ps 默认列表是正在启动的容器 -a是显示所有创建的容器
4、启动、停止、重启某个docker 容器
[root@localhost ~]# docker start yufei_01
yufei_01
[root@localhost ~]# docker stop yufei_01
yufei_01
[root@localhost ~]# docker restart yufei_01
yufei_01
[root@localhost ~]#
5、查看指定容器的日志记录
[root@localhost ~]# docker logs -f yufei_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 yufei
[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 yufei
[root@2d48fc5b7c17 /]# cd yufei
[root@2d48fc5b7c17 yufei]# ls
yufei
[root@2d48fc5b7c17 yufei]# cd yufei
[root@2d48fc5b7c17 yufei]# ls
application
[root@2d48fc5b7c17 yufei]# cd ../../
[root@2d48fc5b7c17 /]# rm -rf yufei
[root@2d48fc5b7c17 /]# eixt
bash: eixt: command not found
[root@2d48fc5b7c17 /]# exit
exit
[root@2d48fc5b7c17 /]#
上面红色部分是日志命令部分。
6、删除某个容器,若正在运行,需要先停止
[root@localhost ~]# docker rm yufei_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 yufei_01
yufei_01
[root@localhost ~]# docker rm yufei_01
yufei_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 yufei_6
yufei_6
[root@localhost ~]# docker stop yufei_5
yufei_5
[root@localhost ~]# docker rm $(docker ps -a -q)
724e7701f0d4
f9097691663e
[root@localhost ~]#
查看docker版本信息
#docker version
#docker -v
#docker info
image镜像操作命令
#docker search image_name //检索image
#docker pull image_name //下载镜像
#docker images //列出本地镜像 -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs
//删除一个或者多个镜像; -f, --force=false Force; --no-prune=false Do not delete untagged parents
#docker rmi image_name
//显示一个镜像的历史; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs
#docker history image_name
容器操作
# 在容器中运行"echo"命令,输出"hello word"
$docker run image_name echo "hello word"
# 交互式进入容器中
$docker run -i -t image_name /bin/bash
# 后台启动镜像 并更改镜像名字
$docker run -d --name myImage centos
# 在容器中安装新的程序
$docker run image_name yum install -y app_name
# 列出当前所有正在运行的container
$docker ps
# 列出所有的container
$docker ps -a
# 列出最近一次启动的container
$docker ps -l
# 保存对容器的修改; -a, --author="" Author; -m, --message="" Commit message
$docker commit ID new_image_name
# 删除所有容器
$docker rm `docker ps -a -q`
# 删除单个容器; -f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, --volumes=false Remove the volumes associated to the container
$docker rm Name/ID
# 停止、启动、杀死一个容器
$docker stop Name/ID
$docker start Name/ID
$docker kill Name/ID
# 从一个容器中取日志; -f, --follow=false Follow log output; -t, --timestamps=false Show timestamps
$docker logs Name/ID
# 列出一个容器里面被改变的文件或者目录,list列表会显示出三种事件,A 增加的,D 删除的,C 被改变的
$docker diff Name/ID
# 显示一个运行的容器里面的进程信息
$docker top Name/ID
# 从容器里面拷贝文件/目录到本地一个路径
$docker cp Name:/container_path to_path
$docker cp ID:/container_path to_path
# 重启一个正在运行的容器; -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10
$docker restart Name/ID
# 附加到一个运行的容器上面; --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process
$docker attach ID
#访问另一个容器的命名空间 进入另一个容器
#安装Linux工具包
$ yum install -y util-linux
#获取容器的Pid
$docker inspect --format "{{.State.Pid}}" containerName
#进入容器
$ nsenter --target Pid --mount --uts --ipc --net --pid
#容器网络配置
#随机生成container到host端口映射
$docker run -d -P --name myNginx nginx
#指定特定端口 将container 80到host91端口的映射
$docker run -d -p 91:80 --name myNginx imageName
# -p ip: hostPort:containerPosrt
$docker ps -l
docker数据管理
# -v 绑定挂载一个数据卷 -h 给容器指定一个主机名
$docker run -it --name volume-test1 -h nginx -v /data/ imageName
#或着手动设置映射
$docker run -it --name volume-test1 -h nginx -v /opt:/opt imageName
#挂载另一容器, 另一容器volume-test2(即使容器已经停掉)来做volume-test1的专门的存储
$docker run -it --name volume-test1 -h nginx --volumes-from volume-test2 imageName
#显示数据卷到host主机的映射关系
$docker inspect -f {{.Volumes}} volume-test1
posted on 2018-07-03 10:33
思月行云 阅读(540)
评论(0) 编辑 收藏 引用 所属分类:
Docker\K8s