其他分享
首页 > 其他分享> > 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 ~]# 

杀死容器:
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容器其他命令:
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 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 run -it centos:6.8 /bin/bash
-it 分配交互式的终端
–name 指定容器的名字
/bin/sh 覆盖容器的初始命令
–cpus 指定cpu的数量
–memory 限定内存的大小
-h 指定容器的主机名

[root@docker01 ~]# docker run -it centos:6.8 /bin/bash
[root@186fe66e29b4 /]# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:04  
          inet addr:172.17.0.4  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:508 (508.0 b)  TX bytes:0 (0.0 b)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

[root@186fe66e29b4 /]# hostname
186fe66e29b4
[root@186fe66e29b4 /]# cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.4	186fe66e29b4
[root@186fe66e29b4 /]# 

#镜像中打包了哪些命令,容器中就存在哪些命令,ip是容器自动按照顺序分配,hosts也自动解析
#COMMAND 为进入容器的初始命令,比如说启动镜像后睡眠10s

[root@docker01 ~]# docker run -it centos:6.8 sleep 10
[root@docker01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
b8861cc3369a        centos:6.8          "sleep 10"               20 seconds ago      Exited (0) 9 seconds ago                          kind_brown
186fe66e29b4        centos:6.8          "/bin/bash"              5 minutes ago       Exited (130) 35 seconds ago                       laughing_shamir
8a3657a09f00        nginx:latest        "nginx -g 'daemon of…"   9 minutes ago       Up 9 minutes                  80/tcp              distracted_cartwright
6cea7211b484        nginx:latest        "nginx -g 'daemon of…"   10 minutes ago      Up 9 minutes                  80/tcp              lucid_sanderson
[root@docker01 ~]# 

–name=fxw 指定容器的名字(或–name fxw)

[root@docker01 ~]# docker run -d --name=fxw centos:6.8
e7efc2f1dcb120812d1f995eefb0147c35ff42ce392f676758191470fb00980a
[root@docker01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
e7efc2f1dcb1        centos:6.8          "/bin/bash"              10 seconds ago      Exited (0) 9 seconds ago                         fxw
b8861cc3369a        centos:6.8          "sleep 10"               2 minutes ago       Exited (0) 2 minutes ago                         kind_brown
186fe66e29b4        centos:6.8          "/bin/bash"              8 minutes ago       Exited (130) 3 minutes ago                       laughing_shamir
8a3657a09f00        nginx:latest        "nginx -g 'daemon of…"   11 minutes ago      Up 11 minutes                80/tcp              distracted_cartwright
6cea7211b484        nginx:latest        "nginx -g 'daemon of…"   13 minutes ago      Up 12 minutes                80/tcp              lucid_sanderson
[root@docker01 ~]#
-h centos:指定容器的主机名

[root@docker01 ~]# docker run -it -h centos1233 centos:6.8 /bin/bash
[root@centos1233 /]# 

其他命令操作:(不一一讲解)

[root@docker01 ~]# 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
      --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
      --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
      --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 string                 Connect a container to a network (default "default")
      --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)
      --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
      --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@docker01 ~]# 

docker进入容器的方法:
进入容器的目的:排错,调试
方法一:docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
例子:docker exec -it 容器id或容器名称 /bin/bash
进入容器默认初始命令为/bin/bash,如下:

[root@docker01 ~]# docker run -it centos:6.8
[root@512d307de28d /]# exit
exit
[root@docker01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
512d307de28d        centos:6.8          "/bin/bash"              13 seconds ago      Exited (0) 3 seconds ago                          dreamy_banzai
a2752e2fef9e        centos:6.8          "/bin/bash"              8 minutes ago       Exited (127) 5 minutes ago                        fervent_galois
[root@docker01 ~]
[root@docker01 ~]# docker container exec -it 2d50be31e6f2 /bin/bash
[root@2d50be31e6f2 /]# 

方法二:docker attach [OPTIONS] CONTAINER
例子: docker attach 容器id或容器名字
nsenter(安装yum install -y util-linux 弃用)

[root@docker01 ~]# docker container attach 2d50be31e6f2
[root@2d50be31e6f2 /]#      

该容器只有两个进程一个是:/bin/bash,另一个是:运行命令进程
attach与exec的区别:attach与run采用的是同一个终端,相当于同一个线程,如果采用多个窗口开发,则在一边输入命令,另一边会同步出现。而exec不是与run同一个终端,每次采用exec连接容器时,都会重新分配终端(线程),不会导致输入命令同步的问题,所以建议采用exec连接进入容器,否则可能导致误操作。

Docker ps命令解释:

[root@docker01 ~]# docker ps -a -l --no-trunc
CONTAINER ID                                                       IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
2d50be31e6f26b029f3954692f620bf1bbec32daef11b6c6daeba1846566b2d5   centos:6.8          "/bin/bash"         26 minutes ago      Exited (0) 2 minutes ago                       peaceful_booth
[root@docker01 ~]# 

-a :显示所有的容器,包括未运行的。
-f :根据条件过滤显示的内容。
–format :指定返回值的模板文件。
-l :显示最近创建的容器。
-n :列出最近创建的n个容器。
–no-trunc :不截断输出。
-q :静默模式,只显示容器编号。
-s :显示总的文件大小。

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

标签:容器,container,--,nginx,日常,docker,root,docker01
来源: https://blog.csdn.net/qq_31776219/article/details/116228725