Docker常用命令实例

Posted on 2022-11-24 21:20  梦中千秋  阅读(351)  评论(0编辑  收藏  举报

Docker常用命令

官方文档

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

version

# 查看版本
[root@izuf6567rc0o5t7oi23kmqz ~]# docker -v
Docker version 20.10.12, build e91ed57
# 查看版本更多信息
[root@izuf6567rc0o5t7oi23kmqz ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:45:41 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.12
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.12
  Git commit:       459d0df
  Built:            Mon Dec 13 11:44:05 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

info

# 查看系统信息
[root@izuf6567rc0o5t7oi23kmqz ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.7.1-docker)
  scan: Docker Scan (Docker Inc., v0.12.0)

Server:
 Containers: 2
  Running: 0
  Paused: 0
  Stopped: 2
 Images: 1
 Server Version: 20.10.12
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc version: v1.0.2-0-g52b36a2
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-862.9.1.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.695GiB
 Name: izuf6567rc0o5t7oi23kmqz
 ID: 7OJ2:JNXX:LNBV:4J5D:27JQ:6YL6:MJNN:YXDZ:A66Q:QT2S:6LGL:EL65
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

–help

# 查看全部帮助
[root@izuf6567rc0o5t7oi23kmqz ~]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default
                           context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.7.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.12.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
# 查看某个命令的帮助信息
[root@izuf6567rc0o5t7oi23kmqz ~]# docker start --help

Usage:  docker start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers

Options:
  -a, --attach               Attach STDOUT/STDERR and forward signals
      --detach-keys string   Override the key sequence for detaching a container
  -i, --interactive          Attach container's STDIN

images

# 查看当前所有镜像
[root@izuf6567rc0o5t7oi23kmqz ~]# docker images
#仓库源       #标签       #id           #创建时间      #大小
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
# 查看所有镜像
[root@izuf6567rc0o5t7oi23kmqz ~]#  docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
# 只显示镜像id
[root@izuf6567rc0o5t7oi23kmqz ~]# docker images -q
feb5d9fea6a5

search

# 搜索镜像 
[root@izuf6567rc0o5t7oi23kmqz ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11840     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4512      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   887                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   92                   
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   90                   
centurylink/mysql                 Image containing mysql. Optimized to be link…   59                   [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         54                   
prom/mysqld-exporter                                                              46                   [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                   [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35                   
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   34                   
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   31                   [OK]
mysql/mysql-router                MySQL Router provides transparent routing be…   23                   
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   21                   
arey/mysql-client                 Run a MySQL client from a docker container      20                   [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   18                   [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                    
cytopia/mysql-5.5                 MySQL 5.5 on CentOS 7                           4                    [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   3                    
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                3                    [OK]
centos/mysql-80-centos7           MySQL 8.0 SQL database server                   2                    
jelastic/mysql                    An image of the MySQL database server mainta…   2                    
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   1                    [OK]
vitess/mysqlctld                  vitess/mysqlctld                                1                    [OK]
monasca/mysql-init                A minimal decoupled init container for mysql    0                   
# 条件查询  star数5000+的
[root@izuf6567rc0o5t7oi23kmqz ~]# docker search mysql --filter=stars=5000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11840     [OK]    

pull

# 下载CentOS 容器
[root@izuf6567rc0o5t7oi23kmqz ~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
# 下载镜像 默认下载最新版[latest]
[root@izuf6567rc0o5t7oi23kmqz ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
# 分层下载
ffbb094f4f9e: Pull complete 
df186527fc46: Pull complete 
fa362a6aa7bd: Pull complete 
5af7cb1a200e: Pull complete 
949da226cc6d: Pull complete 
bce007079ee9: Pull complete 
eab9f076e5a3: Pull complete 
8a57a7529e8d: Pull complete 
b1ccc6ed6fc7: Pull complete 
b4af75e64169: Pull complete 
3aed6a9cd681: Pull complete 
23390142f76f: Pull complete 
Digest: sha256:ff9a288d1ecf4397967989b5d1ec269f7d9042a46fc8bc2c3ae35458c1a26727
Status: Downloaded newer image for mysql:latest
# 下载地址
docker.io/library/mysql:latest
# 指定版本下载
[root@izuf6567rc0o5t7oi23kmqz ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
# 复用已下载之前的版本,绝
ffbb094f4f9e: Already exists 
df186527fc46: Already exists 
fa362a6aa7bd: Already exists 
5af7cb1a200e: Already exists 
949da226cc6d: Already exists 
bce007079ee9: Already exists 
eab9f076e5a3: Already exists 
c7b24c3f27af: Pull complete 
6fc26ff6705a: Pull complete 
bec5cdb5e7f7: Pull complete 
6c1cb25f7525: Pull complete 
Digest: sha256:d1cc87a3bd5dc07defc837bc9084f748a130606ff41923f46dec1986e0dc828d
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@izuf6567rc0o5t7oi23kmqz ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       738e7101490b   2 weeks ago    448MB
mysql         latest    bbf6571db497   2 weeks ago    516MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
# 通过镜像id删除镜像
[root@izuf6567rc0o5t7oi23kmqz ~]# docker rmi 738e7101490b
Untagged: mysql:5.7
Untagged: mysql@sha256:d1cc87a3bd5dc07defc837bc9084f748a130606ff41923f46dec1986e0dc828d
Deleted: sha256:738e7101490b45decf606211a5437ed87aa6a82f1ff03c354564bf9375ce20f9
Deleted: sha256:addad8cfeac97b96eb6652a576269346ac96def9a6709ed2388e24fff4345837
Deleted: sha256:e288c3439a7e2f423f50bf22979a759371c51a70bbbaa450993c336978460b1a
Deleted: sha256:33ece15accaa3bb20e3dee84e2e4501469b917c3abba3d5475cd1fec8bb3e82c
Deleted: sha256:6b15390bceeca8424d82e75f5c9aca5eb4693f96849d6382168a99747877693d
[root@izuf6567rc0o5t7oi23kmqz ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    bbf6571db497   2 weeks ago    516MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
# 删除多个镜像 , 可根据镜像名或镜像id指定
[root@izuf6567rc0o5t7oi23kmqz ~]# docker rmi -f hello-world bbf6571db497
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Untagged: mysql:latest
Untagged: mysql@sha256:ff9a288d1ecf4397967989b5d1ec269f7d9042a46fc8bc2c3ae35458c1a26727
Deleted: sha256:bbf6571db4977fe13c3f4e6289c1409fc6f98c2899eabad39bfe07cad8f64f67
Deleted: sha256:a72da99dce60d6f8d4c4cffa4173153c990537fcdfaa27c35324c3348d55dd5c
Deleted: sha256:8b535d432ef2fbd45d93958347b2587c5cbe334f07d6909ad9d2d480ebbafb65
Deleted: sha256:14d13a3b33fc76839f156cd24b4636dab121e6d3d026cefa2985a4b89e9d4df8
Deleted: sha256:77c21a5a897a1ba752f3d742d6c94ee7c6b0e373fd0aeecc4bf88b9a3982007e
Deleted: sha256:189162becec8bb4588c54fb4ea7e62d20121812e68aeb0291fb4bb5df9ec0985
Deleted: sha256:34980dadfd6a5bb9d7f9e8d4e408000e0a8f4840cc7d3092dc94357ebe7a89b6
Deleted: sha256:15b2beb64a91785c8f3709ecd2410d13577b3174faad164524434ce6a7633506
Deleted: sha256:e38dd14d47b61171927ea4b928f7296123b65a81ad1cfde8f5d00cadf1e81bbb
Deleted: sha256:865abdfd8444741f581ce582e4ac5746c4a00c282febf65aa808a235ec7abf78
Deleted: sha256:b1e35233e1ac953bd06fc8fa83afb3a88c39c1aeae0c89a46cb1b652d6821b38
Deleted: sha256:3bcfdf6641227ff63e3ddf9e38e45cf317b178a50a664e45c6ae596107d5bc46
Deleted: sha256:f11bbd657c82c45cc25b0533ce72f193880b630352cc763ed0c045c808ff9ae1
[root@izuf6567rc0o5t7oi23kmqz ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
# 删除全部
[root@izuf6567rc0o5t7oi23kmqz ~]# docker rmi $(docker images -a)
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase

run

[root@izuf6567rc0o5t7oi23kmqz ~]# docker run --help

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cgroupns string                Cgroup namespace to use (host|private)
                                       'host':    Run the container in the Docker host's cgroup namespace
                                       'private': Run the container in its own private cgroup namespace
                                       '':        Use the cgroup namespace as configured by the
                                                  default-cgroupns-mode option on the daemon (default)
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)
  -d, --detach                         Run container in background and print container ID
      --detach-keys string             Override the key sequence for detaching a container
      --device list                    Add a host device to the container
      --device-cgroup-rule list        Add a rule to the cgroup allowed devices list
      --device-read-bps list           Limit read rate (bytes per second) from a device (default [])
      --device-read-iops list          Limit read rate (IO per second) from a device (default [])
      --device-write-bps list          Limit write rate (bytes per second) to a device (default [])
      --device-write-iops list         Limit write rate (IO per second) to a device (default [])
      --disable-content-trust          Skip image verification (default true)
      --dns list                       Set custom DNS servers
      --dns-option list                Set DNS options
      --dns-search list                Set custom DNS search domains
      --domainname string              Container NIS domain name
      --entrypoint string              Overwrite the default ENTRYPOINT of the image
  -e, --env list                       Set environment variables
      --env-file list                  Read in a file of environment variables
      --expose list                    Expose a port or a range of ports
      --gpus gpu-request               GPU devices to add to the container ('all' to pass all GPUs)
      --group-add list                 Add additional groups to join
      --health-cmd string              Command to run to check health
      --health-interval duration       Time between running the check (ms|s|m|h) (default 0s)
      --health-retries int             Consecutive failures needed to report unhealthy
      --health-start-period duration   Start period for the container to initialize before starting health-retries
                                       countdown (ms|s|m|h) (default 0s)
      --health-timeout duration        Maximum time to allow one check to run (ms|s|m|h) (default 0s)
      --help                           Print usage
  -h, --hostname string                Container host name
      --init                           Run an init inside the container that forwards signals and reaps processes
  -i, --interactive                    Keep STDIN open even if not attached
      --ip string                      IPv4 address (e.g., 172.30.100.104)
      --ip6 string                     IPv6 address (e.g., 2001:db8::33)
      --ipc string                     IPC mode to use
      --isolation string               Container isolation technology
      --kernel-memory bytes            Kernel memory limit
  -l, --label list                     Set meta data on a container
      --label-file list                Read in a line delimited file of labels
      --link list                      Add link to another container
      --link-local-ip list             Container IPv4/IPv6 link-local addresses
      --log-driver string              Logging driver for the container
      --log-opt list                   Log driver options
      --mac-address string             Container MAC address (e.g., 92:d0:c6:0a:29:33)
  -m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)
      --mount mount                    Attach a filesystem mount to the container
      --name string                    Assign a name to the container
      --network network                Connect a container to a network
      --network-alias list             Add network-scoped alias for the container
      --no-healthcheck                 Disable any container-specified HEALTHCHECK
      --oom-kill-disable               Disable OOM Killer
      --oom-score-adj int              Tune host's OOM preferences (-1000 to 1000)
      --pid string                     PID namespace to use
      --pids-limit int                 Tune container pids limit (set -1 for unlimited)
      --platform string                Set platform if server is multi-platform capable
      --privileged                     Give extended privileges to this container
  -p, --publish list                   Publish a container's port(s) to the host
  -P, --publish-all                    Publish all exposed ports to random ports
      --pull string                    Pull image before running ("always"|"missing"|"never") (default "missing")
      --read-only                      Mount the container's root filesystem as read only
      --restart string                 Restart policy to apply when a container exits (default "no")
      --rm                             Automatically remove the container when it exits
      --runtime string                 Runtime to use for this container
      --security-opt list              Security Options
      --shm-size bytes                 Size of /dev/shm
      --sig-proxy                      Proxy received signals to the process (default true)
      --stop-signal string             Signal to stop a container (default "SIGTERM")
      --stop-timeout int               Timeout (in seconds) to stop a container
      --storage-opt list               Storage driver options for the container
      --sysctl map                     Sysctl options (default map[])
      --tmpfs list                     Mount a tmpfs directory
  -t, --tty                            Allocate a pseudo-TTY
      --ulimit ulimit                  Ulimit options (default [])
  -u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                  User namespace to use
      --uts string                     UTS namespace to use
  -v, --volume list                    Bind mount a volume
      --volume-driver string           Optional volume driver for the container
      --volumes-from list              Mount volumes from the specified container(s)
  -w, --workdir string                 Working directory inside the container

进入/退出容器

# 不加版本号默认指定运行最新的
[root@izuf6567rc0o5t7oi23kmqz ~]# docker run -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
# 注意! e706a15f4f07 已经变化,已经进入容器
[root@e706a15f4f07 /]# uname -v
#1 SMP Mon Jul 16 16:29:36 UTC 2018
[root@e706a15f4f07 /]# uname -r
3.10.0-862.9.1.el7.x86_64
[root@e706a15f4f07 /]# exit
exit
[root@izuf6567rc0o5t7oi23kmqz ~]# uname -r
3.10.0-862.9.1.el7.x86_64
[root@izuf6567rc0o5t7oi23kmqz ~]# docker run -it centos:7 /bin/bash
[root@a59f6d77934a /]# uname -r
3.10.0-862.9.1.el7.x86_64
[root@a59f6d77934a /]# exit
exit
[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
a59f6d77934a   centos:7       "/bin/bash"   31 seconds ago   Exited (0) 11 seconds ago             sad_bhaskara
e706a15f4f07   centos         "/bin/bash"   11 minutes ago   Exited (0) 6 minutes ago              laughing_bhabha
e572d50107a7   feb5d9fea6a5   "/hello"      22 hours ago     Exited (0) 22 hours ago               stupefied_allen
3c9b4f1040cf   feb5d9fea6a5   "/hello"      22 hours ago     Exited (0) 22 hours ago               gifted_lumiere

ps

# 查看当前正在运行的容器
[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
# 查看历史运行记录
[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
e706a15f4f07   centos         "/bin/bash"   8 minutes ago   Exited (0) 4 minutes ago             laughing_bhabha
e572d50107a7   feb5d9fea6a5   "/hello"      22 hours ago    Exited (0) 22 hours ago              stupefied_allen
3c9b4f1040cf   feb5d9fea6a5   "/hello"      22 hours ago    Exited (0) 22 hours ago              gifted_lumiere
[root@izuf6567rc0o5t7oi23kmqz ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       7         eeb6ee3f44bd   3 months ago   204MB
centos       latest    5d0da3dc9764   3 months ago   231MB
[root@izuf6567rc0o5t7oi23kmqz ~]# docker run -it centos /bin/bash
[root@6197115f7bf5 /]# docker ps
bash: docker: command not found
# 快捷键 Ctrl+P+Q ,容器不停止退出 
[root@6197115f7bf5 /]# [root@izuf6567rc0o5t7oi23kmqz ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
6197115f7bf5   centos    "/bin/bash"   40 seconds ago   Up 39 seconds             hungry_banzai

rm

# 根据容器id 删除容器
[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
6197115f7bf5   centos         "/bin/bash"   3 minutes ago    Up 3 minutes                          hungry_banzai
a59f6d77934a   centos:7       "/bin/bash"   8 minutes ago    Exited (0) 8 minutes ago              sad_bhaskara
e706a15f4f07   centos         "/bin/bash"   19 minutes ago   Exited (0) 14 minutes ago             laughing_bhabha
e572d50107a7   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago               stupefied_allen
3c9b4f1040cf   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago               gifted_lumiere
# 不能删除正在运行的容器
[root@izuf6567rc0o5t7oi23kmqz ~]# docker rm a59f6d77934a
a59f6d77934a
[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
6197115f7bf5   centos         "/bin/bash"   4 minutes ago    Up 4 minutes                          hungry_banzai
e706a15f4f07   centos         "/bin/bash"   20 minutes ago   Exited (0) 15 minutes ago             laughing_bhabha
e572d50107a7   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago               stupefied_allen
3c9b4f1040cf   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago               gifted_lumiere
# 删除所有容器
$ docker rm -f $(docker ps -aq)
$ docker ps -a -q|xargs docker rm 

stop

[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
6197115f7bf5   centos         "/bin/bash"   4 minutes ago    Up 4 minutes                          hungry_banzai
e706a15f4f07   centos         "/bin/bash"   20 minutes ago   Exited (0) 15 minutes ago             laughing_bhabha
e572d50107a7   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago               stupefied_allen
3c9b4f1040cf   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago               gifted_lumiere
# 停止容器
[root@izuf6567rc0o5t7oi23kmqz ~]# docker stop 6197115f7bf5
6197115f7bf5
[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                        PORTS     NAMES
6197115f7bf5   centos         "/bin/bash"   10 minutes ago   Exited (127) 22 seconds ago             hungry_banzai
e706a15f4f07   centos         "/bin/bash"   26 minutes ago   Exited (0) 21 minutes ago               laughing_bhabha
e572d50107a7   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago                 stupefied_allen
3c9b4f1040cf   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago                 gifted_lumiere

start

[root@izuf6567rc0o5t7oi23kmqz ~]# docker start e706a15f4f07
e706a15f4f07
[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                       PORTS     NAMES
6197115f7bf5   centos         "/bin/bash"   13 minutes ago   Exited (127) 3 minutes ago             hungry_banzai
e706a15f4f07   centos         "/bin/bash"   29 minutes ago   Up 9 seconds                           laughing_bhabha
e572d50107a7   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago                stupefied_allen
3c9b4f1040cf   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago                gifted_lumiere

restart

[root@izuf6567rc0o5t7oi23kmqz ~]# docker restart e706a15f4f07
e706a15f4f07
[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                       PORTS     NAMES
6197115f7bf5   centos         "/bin/bash"   14 minutes ago   Exited (127) 4 minutes ago             hungry_banzai
e706a15f4f07   centos         "/bin/bash"   30 minutes ago   Up 6 seconds                           laughing_bhabha
e572d50107a7   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago                stupefied_allen
3c9b4f1040cf   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago                gifted_lumiere

kill

[root@izuf6567rc0o5t7oi23kmqz ~]# docker kill e706a15f4f07
e706a15f4f07
[root@izuf6567rc0o5t7oi23kmqz ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                        PORTS     NAMES
6197115f7bf5   centos         "/bin/bash"   14 minutes ago   Exited (127) 4 minutes ago              hungry_banzai
e706a15f4f07   centos         "/bin/bash"   30 minutes ago   Exited (137) 13 seconds ago             laughing_bhabha
e572d50107a7   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago                 stupefied_allen
3c9b4f1040cf   feb5d9fea6a5   "/hello"      23 hours ago     Exited (0) 23 hours ago                 gifted_lumiere