手把手帶你學Docker:Docker容器日常管理(四)

運行Docker容器的命令:docker run == docker create + docker start

查看容器

docker ps:查看正在運行的容器
docker ps -a:查看已經創建的容器

[root@docker01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
2f2bb2e87136        nginx               "nginx -g 'daemon of…"   24 seconds ago      Up 22 seconds       0.0.0.0:80->80/tcp   thirsty_montalcini
[root@docker01 ~]# 
[root@docker01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                    PORTS                NAMES
2f2bb2e87136        nginx               "nginx -g 'daemon of…"   30 seconds ago       Up 29 seconds             0.0.0.0:80->80/tcp   thirsty_montalcini
2f7c755645e6        nginx               "nginx -g 'daemon of…"   About a minute ago   Created                                        kind_joliot
efe791c12bb9        nginx               "nginx -g 'daemon of…"   18 hours ago         Exited (0) 17 hours ago                        youthful_mccarthy
c57ccf765ec7        nginx               "nginx -g 'daemon of…"   19 hours ago         Created                                        trusting_lumiere
[root@docker01 ~]# 

停止容器

docker stop container_id
docker container stop container_id (等效)

[root@docker01 ~]# docker stop 2f2bb2e87136
2f2bb2e87136
[root@docker01 ~]# 

KILL容器

docker kill container_id/container_name
docker container kill container_id/container_name(等效)

[root@docker01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
33f8577ba02e        nginx               "nginx -g 'daemon of…"   15 seconds ago      Up 13 seconds       0.0.0.0:80->80/tcp   xenodochial_boyd
[root@docker01 ~]# ^C
[root@docker01 ~]# docker kill 33f8577ba02e
33f8577ba02e
[root@docker01 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@docker01 ~]# 

刪除容器

可以刪除多個: docker rm container_id1 container_id2 container_id3

批量刪除容器:docker rm -f docker ps -a -q

[root@docker01 ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
33f8577ba02e        nginx               "nginx -g 'daemon of…"   4 minutes ago       Exited (137) 4 minutes ago                       xenodochial_boyd
2f2bb2e87136        nginx               "nginx -g 'daemon of…"   4 hours ago         Exited (0) 6 minutes ago                         thirsty_montalcini
2f7c755645e6        nginx               "nginx -g 'daemon of…"   4 hours ago         Created                                          kind_joliot
efe791c12bb9        nginx               "nginx -g 'daemon of…"   22 hours ago        Exited (0) 20 hours ago                          youthful_mccarthy
c57ccf765ec7        nginx               "nginx -g 'daemon of…"   22 hours ago        Created                                          trusting_lumiere
[root@docker01 ~]# docker rm 33f8577ba02e 2f2bb2e87136
33f8577ba02e
2f2bb2e87136
[root@docker01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@docker01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES
2f7c755645e6        nginx               "nginx -g 'daemon of…"   4 hours ago         Created                                       kind_joliot
efe791c12bb9        nginx               "nginx -g 'daemon of…"   22 hours ago        Exited (0) 20 hours ago                       youthful_mccarthy
c57ccf765ec7        nginx               "nginx -g 'daemon of…"   22 hours ago        Created                                 
[root@docker01 ~]# docker rm `docker ps -a -q`
2f7c755645e6
efe791c12bb9
c57ccf765ec7
[root@docker01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@docker01 ~]# 

查看容器命令

查看其他命令:docker container

[root@docker01 ~]# docker container

Usage:  docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  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
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  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
  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
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.
[root@docker01 ~]# 

容器運行命令擴展

docker run == docker create + docker start

創建容器:docker container create nginx:latest
啓動容器: docker container start container_id

[root@docker01 ~]# docker container create nginx:latest
6cea7211b48456a29e89139c802a08e3a40cdabb9d278deb08f8e7915f43a3ba
[root@docker01 ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6cea7211b484        nginx:latest        "nginx -g 'daemon of…"   6 seconds ago       Created                                 lucid_sanderson
[root@docker01 ~]# docker start 6cea7211b484
6cea7211b484
[root@docker01 ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6cea7211b484        nginx:latest        "nginx -g 'daemon of…"   47 seconds ago      Up 4 seconds        80/tcp              lucid_sanderson
[root@docker01 ~]# docker run -d nginx:latest
8a3657a09f00e8b36f6e42c3e6860e7b7a2255da380fcb4d846a3b79f2462606
root@docker01 ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
8a3657a09f00        nginx:latest        "nginx -g 'daemon of…"   36 seconds ago       Up 36 seconds       80/tcp              distracted_cartwright
6cea7211b484        nginx:latest        "nginx -g 'daemon of…"   About a minute ago   Up About a minute   80/tcp              lucid_sanderson
[root@docker01 ~]# 

Docker容器的核心理念

Docker的本質是: 在隔離的環境運行的一個進程 所以:docker容器內的第一個進程必須一直處於前臺運行的狀態(必須夯住),否則這個容器,就會處於退出狀態!

正常情況下: docker run -d centos:6.8 是無法讓centos在後臺運行,能夯住的命令:tail -f /-F

[root@docker01 ~]# docker run -d centos:6.8 
a374f287482a063ced2831335bbeec9034524bc651d978037359c5b9fa537389
[root@docker01 ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                           PORTS               NAMES
a374f287482a        centos:6.8          "/bin/bash"              3 seconds ago        Exited (0) 2 seconds ago                             wonderful_feistel
[root@docker01 ~]# docker run -d centos:6.8 tail -F /var/log.txt
d52b42604011a90b516fc3313c4ad331f918eb69c0c79f7a031d908baeba3f2a
[root@docker01 ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
d52b42604011        centos:6.8          "tail -F /var/log.txt"   6 seconds ago       Up 5 seconds                                         competent_galileo

總結

本篇文章講述了Docker容器的日常管理,由於純手打,難免會有紕漏,如果發現錯誤的地方,請第一時間告訴我,這將是我進步的一個很重要的環節。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章