3.docker容器的常用命令
作者:互联网
1.docker run -d(后台运行)-p(端口映射) 【创建并启动容器,本地如果没有镜像的话会在线拉取镜像】
[root@docker03 ~/docker]# docker run -d -p 80:80 nginx:latest nginx -g 'deamon off;'
28aa819caa8823d34b1b6ff3a365bf5e7fed37619a5121bfd6552d5d45e809eb
2.docker ps -a 查看所有的容器
root@docker03 ~/docker]# docker ps -a
CONAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc18cfe46896 nginx:latest "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp wonderful_joliot
3.docker ps 查看正在运行的容器
[root@docker03 ~/docker]# docker ps
4.docker run -it centos(镜像名称)【进入容器】
[root@docker03 ~/docker]# docker run -it centos
[root@4a52d871171c /]#
5.docker create(创建容器) --name(命名镜像名称)
[root@docker03 ~/docker]# docker create kod:v1
[root@docker03 ~/docker]# docker create --name oldshuai kod:v1
[root@docker03 ~/docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31b70cbdb48c kod:v1 "/bin/bash" 2 seconds ago Created oldshuai
6.docker start oldshuai (启动创建的容器)
[root@docker03 ~/docker]# docker start oldshuai
oldshuai
7.docker stop (停止正在运行的容器)
[root@docker03 ~/docker]# docker stop oldshuai
oldshuai
8.docker kill(强制停止正在运行的容器)
[root@docker03 ~/docker]# docker kill 02f13815bbc4
02f13815bbc4
9.docker restart (重启容器)
[root@docker03 ~/docker]# docker restart 3744637195dd
3744637195dd
10.docker rm (删除容器,不能删除正在运行的)
[root@docker03 ~/docker]# docker rm oldshuai
oldshuai
[root@docker03 ~/docker]# docker rm -f oldshuai (删除正在运行的容器)
[root@docker03 ~/docker]#[root@docker03 ~/docker]# docker rm -f `docker ps -a -q`(删除所有容器)
11.docker exec -it (进入正在运行的容器)
[root@docker03 ~/docker]# docker exec -it 3744637195d /bin/bash 初始命令/bin/bash或/bin/sh
root@3744637195dd:/#
注:容器的初始命令必须夯住,否则容器会退出
12.docker更改时区(重启容器生效)
[root@22732407347e /]# /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
13.指定容器默认启动目录
[root@docker03 /dockerfile/centos6.9]# docker run -it --workdir /opt centos:6.9 /bin/bash
[root@7beca86cb54f opt]#
14.从容器中拷贝文件出来
[root@docker03 ~]# docker run -d kod:v1
3a5ef1de432348245e81ea2a0a988c42fcc430eec46adc8ec38ad77bd549dc22
[root@docker03 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a5ef1de4323 kod:v1 "/bin/bash" 29 seconds ago Exited (0) 28 seconds ago relaxed_swartz
[root@docker03 ~]# docker cp relaxed_swartz:/etc/php-fpm.d/www.conf .
[root@docker03 ~]# ls
anaconda-ks.cfg docker www.conf
15.查看镜像构建的历史
[root@docker03 /dockerfile/ssh]# docker history ssh_centos7:v1
IMAGE CREATED CREATED BY SIZE COMMENT
5d3ed4ecb74c 13 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/usr… 0B
b671cb873a41 13 minutes ago /bin/sh -c echo '123456'|passwd --stdin root 514B
9ad687ac33b9 13 minutes ago /bin/sh -c /usr/sbin/sshd-keygen 2.92kB
e4c58b902735 13 minutes ago /bin/sh -c yum install openssh-server init… 146MB
c941d6ab860f 14 minutes ago /bin/sh -c curl -o /etc/yum.repos.d/CentOS-B… 2.52kB
bec3e6d53d07 14 minutes ago /bin/sh -c curl -o /etc/yum.repos.d/epel.rep… 664B
5e35e350aded 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ADD file:45a381049c52b5664… 203MB
标签:bin,容器,ago,常用命令,docker03,docker,root 来源: https://www.cnblogs.com/zhangfushuai/p/16370519.html