容器相关的命令
先有个认知: 有镜像才能创建容器
下载一个centos的基础镜像
我们来看个例子 : 下载一个centos的基础镜像
[root@VM-0-7-centos ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 2 weeks ago 231MB [root@VM-0-7-centos ~]#
新建容器并启动 docker run
https://docs.docker.com/engine/reference/commandline/run/
操作说明
Option 太多了,看官文吧 ,这里挑几个常用的
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
# 参数说明 --name="Name" 容器名字 ,任意指定,用来区分容器 -d 后台方式运行 -it 使用交互方式运行,进入容器查看内容 -p 指定容器端口 -p 8080:8080 -p ip:主机端口:容器端口 -p 主机端口:容器端口 (常用) -p 容器端口 容器端口 -P 随机指定端口
启动并进入容器
[root@VM-0-7-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 2 weeks ago 231MB [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# # 启动并进入容器 [root@VM-0-7-centos ~]# docker run -it centos /bin/bash [root@23e62436c0c5 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@23e62436c0c5 /]# pwd / # 从容器中退回主机 [root@23e62436c0c5 /]# exit exit [root@VM-0-7-centos ~]# pwd /root [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]#
查看当前有哪些容器正在运行 docker ps
https://docs.docker.com/engine/reference/commandline/ps/
# docker ps 显示正常运行的容器 -a # 显示当前正在运行的容器 + 历史运行过的容器 -n=? # 显示最近创建的容器 -q # 只显示容器的编号
[root@VM-0-7-centos ~]# 显示正常运行的容器 [root@VM-0-7-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# 显示当前正在运行的容器 + 历史运行过的容器 [root@VM-0-7-centos ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 23e62436c0c5 centos "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago admiring_austin 95a5e684ea82 feb5d9fea6a5 "/hello" 44 hours ago Exited (0) 44 hours ago dazzling_davinci [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# 查看帮助 [root@VM-0-7-centos ~]# docker ps --help Usage: docker ps [OPTIONS] List containers Options: -a, --all Show all containers (default shows just running) -f, --filter filter Filter output based on conditions provided --format string Pretty-print containers using a Go template -n, --last int Show n last created containers (includes all states) (default -1) -l, --latest Show the latest created container (includes all states) --no-trunc Don't truncate output -q, --quiet Only display container IDs -s, --size Display total file sizes [root@VM-0-7-centos ~]# docker ps -n=1 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 23e62436c0c5 centos "/bin/bash" 4 minutes ago Exited (0) 4 minutes ago admiring_austin [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# [root@VM-0-7-centos ~]# docker ps -aq 23e62436c0c5 95a5e684ea82 [root@VM-0-7-centos ~]#
启动容器
docker start container_name/container_id # 启动容器
https://docs.docker.com/engine/reference/commandline/start/
停止容器
https://docs.docker.com/engine/reference/commandline/stop/
docker stop container_name/container_id # 停止当前正在运行的容器
重启容器
docker restart container_name/container_id # 重启容器
https://docs.docker.com/engine/reference/commandline/restart/
强制停止
docker kill container_name/container_id # 强制停止当前容器
https://docs.docker.com/engine/reference/commandline/kill/