Docker Review - docker 容器 常用命令(上)

简介: Docker Review - docker 容器 常用命令(上)
+关注继续查看

31ba6d1820c748a98dcc2d94bfddcbbc.png


容器相关的命令


先有个认知: 有镜像才能创建容器


下载一个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 ~]#



6e6860605ccf49be9caeb1b4898387ad.gif


新建容器并启动 docker run

https://docs.docker.com/engine/reference/commandline/run/


3a3813068b9d4cd895ec61de406b2511.png

操作说明

b4fbc0fba5ba4861955801ec780c4c03.png


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 ~]#



131f09df25604b159e712b4e4324b394.png

查看当前有哪些容器正在运行 docker ps

https://docs.docker.com/engine/reference/commandline/ps/

253bed3ebf004e8d9a96a70eb536ffc5.png

# 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/


6fa1fb495eeb4661b9c53643197362ea.png


停止容器

https://docs.docker.com/engine/reference/commandline/stop/

docker stop container_name/container_id       # 停止当前正在运行的容器


1ebd4fea477944a5b604e5d70ae4b948.png

重启容器


docker restart container_name/container_id   # 重启容器


https://docs.docker.com/engine/reference/commandline/restart/


46cf62af1cb0419c827bacfd98e016b2.png

强制停止

docker kill container_name/container_id         # 强制停止当前容器


https://docs.docker.com/engine/reference/commandline/kill/


59b61ba06b164fec880323d598ae44a4.png


相关文章
|
17天前
|
缓存 Ubuntu Linux
docker的安装和常用命令
最近学习了docker做了一些笔记,跟朋友们分享一下
37 0
|
23天前
|
Linux Shell Docker
Linux——安装docker以及docker常用命令
Linux——安装docker以及docker常用命令
|
24天前
|
Docker 容器
【Docker的常用命令】
【Docker的常用命令】
30 0
|
28天前
|
Linux Docker 容器
Docker的常用命令
Docker的常用命令
114 1
|
30天前
|
安全 Linux 虚拟化
【Docker】Docker中network的概要、常用命令、网络模式以及底层ip和容器映射变化的详细讲解
【Docker】Docker中network的概要、常用命令、网络模式以及底层ip和容器映射变化的详细讲解
42 0
|
1月前
|
jenkins 持续交付 网络安全
|
1月前
|
存储 jenkins Shell
|
1月前
|
Java 应用服务中间件 Docker
Spring Boot入门(二十七) 之 Docker常用命令
Spring Boot入门(二十七) 之 Docker常用命令
|
1月前
|
关系型数据库 MySQL Linux
Docker 常用命令
Docker 常用命令
23 1
|
1月前
|
Docker 容器
Docker常用命令
Docker常用命令
63 0
推荐文章
更多