docker--容器操作

docker容器操作详解

[root@localhost ~]# docker container --help
 
 
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.
 
run运行容器
commit 使用当前运行的容器制作镜像
exec可以在容器中执行命令或者进入容器
inspect查看容器的详细信息,也可以查看镜像的信息
kill强制杀掉容器
logs查看容器产生的日志信息
ls查看当前运行的容器
ps查看当前运行的容器
pause暂停容器
unpause开启容器
port查看容器端口映射信息
rename对容器改名
restart重启容器
start启动容器
stop停掉容器
rm删除容器,默认删除不了正在运行的容器。-f强制删除
stats查看容器的运行状态
top查看容器的系统内存等使用信息
 
 
 

例子1:启动容器

[root@localhost ~]# 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
      --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)
      --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
 
 
  • 前台运行容器

-i交互模式
-t分配终端
-i -t意思是打开一个终端,然后执行命令,可以写成-it
[root@localhost ~]# docker run -i -t busybox ip a  #执行ip a命令
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
 
  • 后台运行容器

-d参数是后台启动容器
注意:这里我的docker中没有nginx这个镜像,在运行他时会去dockerhub中拉取下来然后启动
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              83aa35aa1c79        4 weeks ago         1.22MB
busybox             1.31.0              19485c79a9bb        7 months ago        1.22MB
busybox             1.32.0              19485c79a9bb        7 months ago        1.22MB
[root@localhost ~]# docker run -d nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
039b124d5cc35a88ac456af4fa869a6c07efef9c713c4bd85828c0c9faa00476
 
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
039b124d5cc3        nginx               "nginx -g 'daemon of…"   28 seconds ago      Up 28 seconds               80/tcp              cranky_roentgen
609ae5eaf41f        busybox             "/bin/sh"                5 minutes ago       Exited (0) 4 minutes ago                        gallant_moser
ea361ca42de3        busybox             "ip a"                   7 minutes ago       Exited (0) 7 minutes ago                        peaceful_elgamal
ebcad500c838        busybox             "/bin/sh"                12 minutes ago      Exited (0) 12 minutes ago                       affectionate_cori
a638e5186cc0        busybox             "ip a"                   13 minutes ago      Exited (0) 13 minutes ago                       goofy_stonebraker
 

总结:

当镜像类型是工具类时,一般使用-i -t交互式模式运行

当镜像类型是服务类时,一般使用-d后台运行

 
  • #运行容器加多个参数

[root@localhost ~]# docker run --name nginxtest -d --rm nginx:latest #指定容器名,启动后删除容器
0b5b8eab5c64d518cf7bedcbbeffe98799ef24e56fe58893de2f1204cb7535f1
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0b5b8eab5c64        nginx:latest        "nginx -g 'daemon of…"   5 seconds ago       Up 4 seconds        80/tcp              nginxtest
--name指定容器名字,如果不指定的话启动后就是随机生成一个名字
-d后台启动
-rm容器停掉之后自动删除
 
 
 
 
 

 例子2:-p是指定端口

1 先查看容器内部暴露出来的端口是什么
docker inspect nginx
2 把容器暴露的端口指定到物理机
[root@localhost ~]# docker run -d -p 88:80 nginx #后台启动nginx容器,并把容器暴露的80端口指定到物理机的88端口 6ecd35acc438f3635da13943dfb9e3e510314dc6a1dde04f2228adaa0f5254e7 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6ecd35acc438 nginx "nginx -g 'daemon of…" 6 seconds ago Up 4 seconds 0.0.0.0:88->80/tcp nice_mendel 039b124d5cc3 nginx "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 80/tcp cranky_roentgen
访问:
 
 
 
 

#例子3:查看正在运行的容器

-a 参数是查看所有容器
[root@localhost ~]# 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 numeric IDs
-s, --size Display total file sizes


[root@localhost ~]# docker ps#查看正在运行的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]# docker ps -a#查看所有容器
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS               NAMES
609ae5eaf41f        busybox             "/bin/sh"           About a minute ago   Exited (0) 33 seconds ago                       gallant_moser
ea361ca42de3        busybox             "ip a"              3 minutes ago        Exited (0) 3 minutes ago                        peaceful_elgamal
ebcad500c838        busybox             "/bin/sh"           8 minutes ago        Exited (0) 8 minutes ago                        affectionate_cori
a638e5186cc0        busybox             "ip a"              8 minutes ago        Exited (0) 8 minutes ago                        goofy_stonebraker
 
 

 例子4,进入到容器内部

[root@localhost ~]# docker run -i -t busybox /bin/sh  #进入到容器内部
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # pwd
/
/ #
 
/ # exit#退出容器
[root@localhost ~]#
 
 

例子5:在容器外执行命令

[root@localhost ~]# docker exec -it 609ae5eaf41f ls
Error response from daemon: Container 609ae5eaf41f79fc2516c69e174a79a85f8e1b46fc454301383a1fd45fc3dadd is not running
 
 
 
注意:如果你想要操作容器的话,必须保证容器是up状态即正在运行的状态,exited状态的容器无法是无法进行操作的。
 
 
 
 
 

 例子6,查看容器运行状态

systemctl status docker
[root@localhost ~]# systemctl status docker#查看docker守护进程的运行状态
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-04-11 05:35:21 CST; 1 day 5h ago
     Docs: https://docs.docker.com
Main PID: 5369 (dockerd)
    Tasks: 15
   Memory: 198.2M
   CGroup: /system.slice/docker.service
           ├─5369 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
           └─8355 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 88 -container-ip 172.17.0.3 -container-p...
 
 
Apr 12 10:31:35 localhost.localdomain dockerd[5369]: time="2020-04-12T10:31:35.323655071+08:00" level=error msg="Ha...own"
Apr 12 10:31:35 localhost.localdomain dockerd[5369]: time="2020-04-12T10:31:35.376338150+08:00" level=info msg="ign...ete"
Apr 12 10:32:23 localhost.localdomain dockerd[5369]: time="2020-04-12T10:32:23.887202664+08:00" level=info msg="ign...ete"
Apr 12 10:37:20 localhost.localdomain dockerd[5369]: time="2020-04-12T10:37:20.423832752+08:00" level=error msg="Ha...own"
Apr 12 10:37:20 localhost.localdomain dockerd[5369]: time="2020-04-12T10:37:20.497206110+08:00" level=info msg="ign...ete"
Apr 12 10:39:57 localhost.localdomain dockerd[5369]: time="2020-04-12T10:39:57.883564829+08:00" level=info msg="ign...ete"
Apr 12 10:54:43 localhost.localdomain dockerd[5369]: time="2020-04-12T10:54:43.330826467+08:00" level=error msg="Er...ing"
Apr 12 10:55:36 localhost.localdomain dockerd[5369]: time="2020-04-12T10:55:36.083074621+08:00" level=info msg="ign...ete"
Apr 12 10:55:45 localhost.localdomain dockerd[5369]: time="2020-04-12T10:55:45.926918343+08:00" level=info msg="ign...ete"
Apr 12 10:56:37 localhost.localdomain dockerd[5369]: time="2020-04-12T10:56:37.452285681+08:00" level=info msg="ign...ete"
Hint: Some lines were ellipsized, use -l to show in full.
 
 
 
 
 
 
 
 
 

例子7,删除容器

  • 方法一:长ID

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
b73bed19d100        busybox:latest      "sh"                     30 minutes ago      Exited (0) 30 minutes ago                        magical_bhabha
eb15e92f4f19        busybox             "sh"                     31 minutes ago      Exited (0) 31 minutes ago                        festive_wu
51d851f2cbaf        busybox             "sh"                     31 minutes ago      Exited (0) 31 minutes ago                        goofy_moser
6ecd35acc438        nginx               "nginx -g 'daemon of…"   38 minutes ago      Up 38 minutes               0.0.0.0:88->80/tcp   nice_mendel
039b124d5cc3        nginx               "nginx -g 'daemon of…"   42 minutes ago      Up 42 minutes               80/tcp               cranky_roentgen
609ae5eaf41f        busybox             "/bin/sh"                47 minutes ago      Exited (0) 46 minutes ago                        gallant_moser
ea361ca42de3        busybox             "ip a"                   49 minutes ago      Exited (0) 49 minutes ago                        peaceful_elgamal
ebcad500c838        busybox             "/bin/sh"                54 minutes ago      Exited (0) 54 minutes ago                        affectionate_cori
a638e5186cc0        busybox             "ip a"                   55 minutes ago      Exited (0) 55 minutes ago                        goofy_stonebraker
[root@localhost ~]# docker rm a638e5186cc0 #容器ID全长
a638e5186cc0
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
b73bed19d100        busybox:latest      "sh"                     31 minutes ago      Exited (0) 31 minutes ago                        magical_bhabha
eb15e92f4f19        busybox             "sh"                     32 minutes ago      Exited (0) 32 minutes ago                        festive_wu
51d851f2cbaf        busybox             "sh"                     32 minutes ago      Exited (0) 32 minutes ago                        goofy_moser
6ecd35acc438        nginx               "nginx -g 'daemon of…"   39 minutes ago      Up 39 minutes               0.0.0.0:88->80/tcp   nice_mendel
039b124d5cc3        nginx               "nginx -g 'daemon of…"   43 minutes ago      Up 43 minutes               80/tcp               cranky_roentgen
609ae5eaf41f        busybox             "/bin/sh"                49 minutes ago      Exited (0) 48 minutes ago                        gallant_moser
ea361ca42de3        busybox             "ip a"                   50 minutes ago      Exited (0) 50 minutes ago                        peaceful_elgamal
ebcad500c838        busybox             "/bin/sh"                56 minutes ago      Exited (0) 55 minutes ago                        affectionate_cori
 
 
 
  • 方法二:短ID

[root@localhost ~]# docker rm ebcad500 #不一定全写,能标识唯一id就ok
ebcad500
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
b73bed19d100        busybox:latest      "sh"                     33 minutes ago      Exited (0) 33 minutes ago                        magical_bhabha
eb15e92f4f19        busybox             "sh"                     34 minutes ago      Exited (0) 34 minutes ago                        festive_wu
51d851f2cbaf        busybox             "sh"                     34 minutes ago      Exited (0) 34 minutes ago                        goofy_moser
6ecd35acc438        nginx               "nginx -g 'daemon of…"   41 minutes ago      Up 41 minutes               0.0.0.0:88->80/tcp   nice_mendel
039b124d5cc3        nginx               "nginx -g 'daemon of…"   46 minutes ago      Up 46 minutes               80/tcp               cranky_roentgen
609ae5eaf41f        busybox             "/bin/sh"                51 minutes ago      Exited (0) 50 minutes ago                        gallant_moser
ea361ca42de3        busybox             "ip a"                   53 minutes ago      Exited (0) 53 minutes ago                        peaceful_elgamal
 
 
 
  • 方法三:容器名

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
b73bed19d100        busybox:latest      "sh"                     37 minutes ago      Exited (0) 37 minutes ago                        magical_bhabha
eb15e92f4f19        busybox             "sh"                     38 minutes ago      Exited (0) 38 minutes ago                        festive_wu
51d851f2cbaf        busybox             "sh"                     38 minutes ago      Exited (0) 38 minutes ago                        goofy_moser
6ecd35acc438        nginx               "nginx -g 'daemon of…"   45 minutes ago      Up 45 minutes               0.0.0.0:88->80/tcp   nice_mendel
039b124d5cc3        nginx               "nginx -g 'daemon of…"   50 minutes ago      Up 50 minutes               80/tcp               cranky_roentgen
609ae5eaf41f        busybox             "/bin/sh"                55 minutes ago      Exited (0) 54 minutes ago                        gallant_moser
ea361ca42de3        busybox             "ip a"                   57 minutes ago      Exited (0) 57 minutes ago                        peaceful_elgamal
[root@localhost ~]# docker rm peaceful_elgamal
peaceful_elgamal
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
b73bed19d100        busybox:latest      "sh"                     38 minutes ago      Exited (0) 38 minutes ago                        magical_bhabha
eb15e92f4f19        busybox             "sh"                     39 minutes ago      Exited (0) 39 minutes ago                        festive_wu
51d851f2cbaf        busybox             "sh"                     39 minutes ago      Exited (0) 39 minutes ago                        goofy_moser
6ecd35acc438        nginx               "nginx -g 'daemon of…"   46 minutes ago      Up 46 minutes               0.0.0.0:88->80/tcp   nice_mendel
039b124d5cc3        nginx               "nginx -g 'daemon of…"   50 minutes ago      Up 50 minutes               80/tcp               cranky_roentgen
609ae5eaf41f        busybox             "/bin/sh"                55 minutes ago      Exited (0) 54 minutes ago                        gallant_moser

注意:删除镜像也是一样的!

 
 
 

例子8,#启动容器 start命令

[root@localhost ~]# 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



[root@localhost ~]# docker start 039b124d5cc3
039b124d5cc3
 

docker中run和start的区别?

docker run 后面指定的是一个镜像
而docker start指定的是一个容器
docker run是利用镜像生成容器,并启动容器,而docker start是启动一个之前生成过的容器
 
 

例子9,#停止容器 stop命令

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
039b124d5cc3        nginx               "nginx -g 'daemon of…"   59 minutes ago      Up 59 minutes       80/tcp              cranky_roentgen
[root@localhost ~]# docker stop 039b124d5cc3
039b124d5cc3
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 
 

例子10,#杀掉容器

docker kill 容器id
 
 
docker容器操作的kill和stop的区别:
stop优雅退出。
kill -9 
 
 
 
 

例子11,#查看容器详细信息

方法一:history命令
[root@localhost ~]# docker history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
ed21b7a8aee9        12 days ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           12 days ago         /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B                  
<missing>           12 days ago         /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           12 days ago         /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           12 days ago         /bin/sh -c set -x     && addgroup --system -…   57.6MB              
<missing>           12 days ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B                  
<missing>           12 days ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.3.9        0B                  
<missing>           12 days ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.17.9     0B                  
<missing>           12 days ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           12 days ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           12 days ago         /bin/sh -c #(nop) ADD file:d1f1b387a158136fb…   69.2MB
 
方法二:inspect命令
[root@localhost ~]# docker inspect nginx
[
    {
        "Id": "sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-03-31T03:19:30.487069362Z",
        "Container": "5c86b143cf5caec0aed5c331922c243f00600152ec0e9ecbe8531771562e72b8",
        "ContainerConfig": {
            "Hostname": "5c86b143cf5c",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.9",
                "NJS_VERSION=0.3.9",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:9be1fc3b00d81a66f4de11f5f5bf176e0748434be056cf3152386cc917307e7f",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGTERM"
        },
        "DockerVersion": "18.09.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.9",
                "NJS_VERSION=0.3.9",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:9be1fc3b00d81a66f4de11f5f5bf176e0748434be056cf3152386cc917307e7f",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGTERM"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 126769107,
        "VirtualSize": 126769107,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/61ad7ab4f29396e49df88b46fe1694a315c9097f003f7ad8241d6f7e2256a03a/diff:/var/lib/docker/overlay2/72145eea111b480aba9e5880bf966d868371d47e5d16fa912fb117c6fb2eba78/diff",
                "MergedDir": "/var/lib/docker/overlay2/c3caa8f42de7d71095d8df370d7198ed37a55c4a18a9f5e38181da918b4dd2f7/merged",
                "UpperDir": "/var/lib/docker/overlay2/c3caa8f42de7d71095d8df370d7198ed37a55c4a18a9f5e38181da918b4dd2f7/diff",
                "WorkDir": "/var/lib/docker/overlay2/c3caa8f42de7d71095d8df370d7198ed37a55c4a18a9f5e38181da918b4dd2f7/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:c3a984abe8a88059915bb6c7a1d249fd1ccc16d931334ac8816540b0eb686b45",
                "sha256:99134ec7f247e5a211c7205fec587bf72a6d4aac339b21858b892e9c04f78920",
                "sha256:d37eecb5b7691ec21bd19989e37f8bb4d20b340a775591d0f3db5897d606b0e4"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
View Code
 
 
 
 
 

例子12,#查看容器的运行状态

docker stats
 
 
 
 
 
 
 
 
 
 
 
 

例子13,#查看容器日志 log命令

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS             
d4d7e7115dc9        nginx:latest        "nginx -g 'daemon of…"   2 minutes ago       Up 5 seconds        80/tcp            
[root@localhost ~]# docker logs d4d7e7115dc9
 
-f实时查看日志
[root@localhost ~]# docker logs d4d7e7115dc9 -f
 
 

例子14,#容器改名 docker rename 【容器名】 【要修改的名】

[root@localhost ~]# docker rename sad_hellman nginx-test
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d4d7e7115dc9        nginx:latest        "nginx -g 'daemon of…"   3 days ago          Up 3 minutes        80/tcp              nginx-test
 
 
 
 
 

例子15,#查看容器系统使用信息

[root@localhost ~]# docker top nginx-test
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4028                4012                0                   21:45               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 4058                4028                0                   21:45               ?                   00:00:00            nginx: worker process
 
 

例子16,#暂停容器

[root@localhost ~]# docker pause nginx-test
nginx-test
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS               NAMES
d4d7e7115dc9        nginx:latest        "nginx -g 'daemon of…"   3 days ago          Up 6 minutes (Paused)   80/tcp              nginx-test
 
 
 

例子17,#开启容器

[root@localhost ~]# docker unpause nginx-test
nginx-test
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d4d7e7115dc9        nginx:latest        "nginx -g 'daemon of…"   3 days ago          Up 7 minutes        80/tcp              nginx-test
 
 
 
 

例子18,#进入容器:

可以选择sh或者bash,bash显示的信息更全
[root@localhost ~]# docker port d4d7e7115dc9
[root@localhost ~]# docker exec -it d4d7e7115dc9 sh
#
#
#
# exit
[root@localhost ~]# docker exec -it d4d7e7115dc9 bash
root@d4d7e7115dc9:/#
root@d4d7e7115dc9:/#
root@d4d7e7115dc9:/# exit
exit
[root@localhost ~]#
总结:
进入容器的方式:4种
1 exec #在容器里面退出,容器本身不会退出,还是up状态。推荐使用
2 attach #容器本身会退出exited状态
3 ssh#
4 nsenter
 
 
 

猜你喜欢

转载自www.cnblogs.com/wenm1128/p/12714878.html