Docker: docker container常用命令实战

容器管理,容器常用选项

选项

描述

-i, –interactive

交互式

-t, –tty

分配一个伪终端

-d, –detach

运行容器到后台

-e, –env

设置环境变量

-p, –publish list

发布容器端口到主机

–name string

指定容器名称

-h, –hostname

设置容器主机名

–restart string

容器退出时重启策略 默认no,可选值:[always|on-failure]

[BEGIN] 2019/3/12 12:37:50
#查看docker container 的帮助
[root@192 ~]# 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.
#查看docker的帮助
[root@192 ~]# docker --help

Usage:    docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -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:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  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.
#查看docker container run的帮助
[root@192 ~]# docker container run --help

Usage:    docker container 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
#列出images
[root@192 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              881bd08c0b08        6 days ago          109MB
busybox             latest              d8233ab899d4        3 weeks ago         1.2MB
#从镜像仓库拉取name=centos:latest镜像
[root@192 ~]# docker image pull centos:latest
latest: Pulling from library/centos

Digest: sha256:38777a4106f0072649128ea4236241345a3ed21c55abbbd53bad5342549f6126
Status: Downloaded newer image for centos:latest
#列出镜像,确认centos:latest镜像拉取成功
[root@192 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              881bd08c0b08        6 days ago          109MB
busybox             latest              d8233ab899d4        3 weeks ago         1.2MB
centos              latest              1e1148e4cc2c        3 months ago        202MB
#从镜像仓库拉取name=centos:7镜像
[root@192 ~]# docker container run centos:7
Unable to find image 'centos:7' locally
7: Pulling from library/centos
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for centos:7
#运行一个容器,来自名为centos:7的镜像
[root@192 ~]# docker container run centos:7
#列出最近启动的容器, 这里为空,是因为centos:7是一个基础镜像,没有加-itd 参数,启动后,默认马上退出
[root@192 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
#列出所有状态的容器,可以看到centos:7 status=Exited,说明启动后,退出了
[root@192 ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
d9101c06b8ec        centos:7            "/bin/bash"              2 minutes ago       Exited (0) 2 minutes ago                       practical_hertz
63b0e38408f8        centos:7            "/bin/bash"              2 minutes ago       Exited (0) 2 minutes ago                       goofy_elbakyan
0be8e5611dcc        nginx               "nginx -g 'daemon of…"   9 minutes ago       Exited (0) 4 minutes ago                       elastic_sammet
#删除容器名=d9101c06b8ec的容器
[root@192 ~]# docker container rm d9101c06b8ec
d9101c06b8ec
#删除容器名=63b0e38408f8的容器
[root@192 ~]# docker container rm 63b0e38408f8
63b0e38408f8
[root@192 ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
0be8e5611dcc        nginx               "nginx -g 'daemon of…"   10 minutes ago      Exited (0) 5 minutes ago                       elastic_sammet
#一个镜像可以启动多个容器,就类似windows的快捷键,镜像是分层存储的,容器也是分层存储的,容器的运行不互相影响,因为不同的容器,有自己的分层存储文件。
[root@192 ~]# docker container run centos:7
[root@192 ~]# docker container run -d centos:7
a11b2d9ebb84ab7abc6aa0c51ae41d303d5f33d7f6a72d14988f0d3ae7458077
[root@192 ~]# docker container run -itd centos:7
9a9c6d523f30cc9bda9eab1f3cbd2a8ea0991bacaea1c744ce34e2c3b5bee0a1
[root@192 ~]# docker container run -itd centos:7 bash
7894ef6ccb2f7521f77968c5ad7dde18e868ccb128a2cccf1974965fc1ace1a6
[root@192 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7894ef6ccb2f        centos:7            "bash"              46 seconds ago      Up 45 seconds                           condescending_torvalds
9a9c6d523f30        centos:7            "/bin/bash"         56 seconds ago      Up 55 seconds                           focused_einstein
#进入容器名=7894ef6ccb2f,并打开一个终端
[root@192 ~]# docker container exec -it 7894ef6ccb2f bash
#进入终端后,可运行linux的基础命令
[root@7894ef6ccb2f /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@7894ef6ccb2f /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 11:53 pts/0    00:00:00 bash
root         14      0  0 11:54 pts/1    00:00:00 bash
root         28     14  0 11:55 pts/1    00:00:00 ps -ef
#退出容器
[root@7894ef6ccb2f /]# exit
exit

#可以通过带-e 参数:对容器内进行环境变量设置, -name 给当前的容器一个名字, -h 设置容器的主机名
这里本地没有java:8这个镜像,所以默认从镜像仓库拉取,拉取成功后,自动运行
[root@192 ~]# docker run -e JAVA_HOME=/usr/local/jdk --name firstjava -h java8 java:8
Unable to find image 'java:8' locally
8: Pulling from library/java


Digest: sha256:34959eed5df6bfe74a17fbf459ed29505d51a48d5cb4a39f215b613c65b8023b
Status: Downloaded newer image for java:8
#列出所有状态的容器
[root@192 ~]# docker container ls -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
e1ebf7553d34        java:8              "/bin/bash"         56 seconds ago      Exited (0) 53 seconds ago                       firstjava
#删除容器
[root@192 ~]# docker container rm e1ebf7553d34
e1ebf7553d34
#加上 -d参数,运行容器
[root@192 ~]# docker run -d -e JAVA_HOME=/usr/local/jdk --name firstjava -h java8 java:8
dd74f7cf5d0fe0d42b4ab678e2ac93343c5dd21f88c08b78eff9636087d88d12
#加上 -d参数,运行容器,java:8 启动后还是自动退出了
[root@192 ~]# docker container ls -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
dd74f7cf5d0f        java:8              "/bin/bash"         21 seconds ago      Exited (0) 20 seconds ago                       firstjava
#删除容器
[root@192 ~]# docker container rm dd74f7cf5d0f
dd74f7cf5d0f
#加上 -itd参数,运行容器
[root@192 ~]# docker run -itd -e JAVA_HOME=/usr/local/jdk --name firstjava -h java8 java:8
375d4cc961386a1fdeddd4277d92b466ae477e71743ee004d60965d5591ac684
#加上 -d参数,运行容器,java:8 启动后容器状态为启动着。这里要说明一下,因为java:8也是一个基础镜像,前面2次启动容器后,由于容器内没有一直运行的进行,所以容器自动退出,加了-itd的后,容器在后台运行,并且启动着一个tty终端,所以不会自动退出了。
[root@192 ~]# docker container ls -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
375d4cc96138        java:8              "/bin/bash"         6 seconds ago       Up 4 seconds                            firstjava
#进入上面创建的java:8容器,确认-e JAVA_HOME=/usr/local/jdk 变量是否成功
[root@192 ~]# docker container exec -it firstjava bash
#给容器设置JAVA_HOME变量,设置成功
root@java8:/# echo $JAVA_HOME
/usr/local/jdk
#给容器设置hostname设置成功
root@java8:/# hostname
java8
#退出容器
root@java8:/# exit
exit
#给容器配置 容器退出时重启策略(测试通过:先将nginx容器stop. reboot宿主机,宿主机启动后查看nginx默认启动状态)
[root@192 ~]# docker run -itd -e JAVA_HOME=/usr/local/jdk --name test01 -h java8 --restart=always java:8
d19228751012d93568bba75827660c5cb848423416ec57a65c125cf8e75ebee7
[root@192 ~]# docker run -itd  --restart=always java:8
e1b00707869712515b968ef4a98569a31cf4c0e5bccb5f4fcac57b978311279b

#列出容器资源使用状态,可以看到容器总内容是972.6MiB,已使用492KiB,MEM %=0.05%内存使用百分比,NET I/O是网络IO
[root@192 ~]# docker container stats test01
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
d19228751012        test01              0.00%               492KiB / 972.6MiB   0.05%               656B / 0B           0B / 0B             1
[root@192 ~]# docker container ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS               NAMES
d64127b97ed8        java:8              "/bin/bash"              About an hour ago   Up About an hour                             pensive_franklin
e1b007078697        java:8              "/bin/bash"              2 hours ago         Up 2 hours                                   musing_elion
d19228751012        java:8              "/bin/bash"              2 hours ago         Up 2 hours                                   test01
375d4cc96138        java:8              "/bin/bash"              2 hours ago         Up 2 hours                                   firstjava
7894ef6ccb2f        centos:7            "bash"                   2 hours ago         Up 2 hours                                   condescending_torvalds
9a9c6d523f30        centos:7            "/bin/bash"              2 hours ago         Up 2 hours                                   focused_einstein
a11b2d9ebb84        centos:7            "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                       clever_brown
e6946111e2e9        centos:7            "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                       trusting_bardeen
0be8e5611dcc        nginx               "nginx -g 'daemon of…"   3 hours ago         Exited (0) 3 hours ago                       elastic_sammet
#停止上面所有容器 也可使用 docker container stop $(docker ps -q -a)
[root@192 ~]# docker container stop $(docker ps -a|awk '{print $1}')
d64127b97ed8
e1b007078697
d19228751012
375d4cc96138
7894ef6ccb2f
9a9c6d523f30
a11b2d9ebb84
e6946111e2e9
0be8e5611dcc
Error response from daemon: No such container: CONTAINER
#确认容器都已停止
[root@192 ~]# docker container ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
d64127b97ed8        java:8              "/bin/bash"              2 hours ago         Exited (0) 20 seconds ago                         pensive_franklin
e1b007078697        java:8              "/bin/bash"              2 hours ago         Exited (0) 20 seconds ago                         musing_elion
d19228751012        java:8              "/bin/bash"              2 hours ago         Exited (0) 20 seconds ago                         test01
375d4cc96138        java:8              "/bin/bash"              2 hours ago         Exited (0) 20 seconds ago                         firstjava
7894ef6ccb2f        centos:7            "bash"                   2 hours ago         Exited (137) 10 seconds ago                       condescending_torvalds
9a9c6d523f30        centos:7            "/bin/bash"              2 hours ago         Exited (137) 10 seconds ago                       focused_einstein
a11b2d9ebb84        centos:7            "/bin/bash"              3 hours ago         Exited (0) 3 hours ago                            clever_brown
e6946111e2e9        centos:7            "/bin/bash"              3 hours ago         Exited (0) 3 hours ago                            trusting_bardeen
0be8e5611dcc        nginx               "nginx -g 'daemon of…"   3 hours ago         Exited (0) 3 hours ago                            elastic_sammet
#删除上面所有容器 也可使用 docker container rm $(docker ps -q -a), 其中 -f删除可以强制删除运行中的容器
[root@192 ~]# docker container rm -f $(docker ps -a|awk '{print $1}')
d64127b97ed8
e1b007078697
d19228751012
375d4cc96138
7894ef6ccb2f
9a9c6d523f30
a11b2d9ebb84
e6946111e2e9
0be8e5611dcc
Error: No such container: CONTAINER
#确认容器都已被删除
[root@192 ~]# docker container ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
#查看镜像
[root@192 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              881bd08c0b08        6 days ago          109MB
busybox             latest              d8233ab899d4        3 weeks ago         1.2MB
centos              7                   1e1148e4cc2c        3 months ago        202MB
centos              latest              1e1148e4cc2c        3 months ago        202MB
java                8                   d23bdf5b1b1b        2 years ago         643MB
#启动一个nginx容器
[root@192 ~]# docker container run -d -p 8800:80 nginx
a03e32f2e657358c75003e0f2dc8659d534bb92c2a5cd6f0d6134a760cc42384
#列出运行中的容器 
[root@192 ~]# docker container ps -qa
a03e32f2e657
#查看容器端口
[root@192 ~]# docker container port a03e32f2e657
80/tcp -> 0.0.0.0:8800
#查看容器日志
[root@192 ~]# docker container logs a03e32f2e657
192.168.1.5 - - [11/Mar/2019:14:29:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-"
#查看容器日志,带-f ,实时打印新增的日志
[root@192 ~]# docker container logs a03e32f2e657 -f
192.168.1.5 - - [11/Mar/2019:14:29:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-"
192.168.1.5 - - [11/Mar/2019:14:31:37 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-"
192.168.1.5 - - [11/Mar/2019:14:31:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-"
192.168.1.5 - - [11/Mar/2019:14:31:49 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-"
^C
#列出容器里正在运行的进程
[root@192 ~]# docker container top a03e32f2e657
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                20099               20080               0                   22:26               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 20136               20099               0                   22:26               ?                   00:00:00            nginx: worker process
#以下做容器内外文件的copy
[root@192 ~]# ls
anaconda-ks.cfg  nginx.tar
[root@192 ~]# touch a.out
[root@192 ~]# ls
anaconda-ks.cfg  a.out  nginx.tar
#把宿主机中的文件a.out copy 到容器a03e32f2e657的 /目录
[root@192 ~]# docker container cp a.out a03e32f2e657:/
#进入容器查看a.out是否被copy进了宿主机,在容器中a.out存在,测试通过
[root@192 ~]# docker container exec -it a03e32f2e657 bash
root@a03e32f2e657:/# ls
a.out  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run    sbin  srv  sys    tmp  usr  var
#在容器中创建 b.out,
root@a03e32f2e657:/# touch b.out
root@a03e32f2e657:/# ls
a.out  bin   dev  home    lib64  mnt  proc  run    srv  tmp  var
b.out  boot  etc  lib    media  opt  root  sbin    sys  usr
root@a03e32f2e657:/# exit
exit
#将容器中的/b.out文件copy到宿主机用户的当前目录
[root@192 ~]# docker container cp a03e32f2e657:/b.out .
#查看宿主机中b.out存在,测试通过
[root@192 ~]# ls
anaconda-ks.cfg  a.out  b.out  nginx.tar

作者: 梅梅~

出处: https://www.cnblogs.com/keeptesting

关于作者:专注软件测试,测试运维相关工作,请多多赐教!

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题,可加微信联系。 微信:yangguangkg20140901 暗号:博客园.

原文地址:https://www.cnblogs.com/keeptesting/p/10529111.html