docker基础命令

镜像相关

docker search 搜索镜像

查找自己想要的镜像。
使用方法:

➜  ~ docker search --help

Usage:  docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output

这个命令没有什么特殊的选项。

➜  ~ docker search mongo
NAME                                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mongo                               MongoDB document databases provide high avai…   8404      [OK]
mongo-express                       Web-based MongoDB admin interface, written w…   1107      [OK]
tutum/mongodb                       MongoDB Docker image – listens in port 27017230                  [OK]
mongoclient/mongoclient             Official docker image for Mongoclient, featu…   106                  [OK]

docker pull 下载镜像

找到之后我们下载镜像。

➜  ~ docker pull --help

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform capable
  -q, --quiet                   Suppress verbose output

下载一个MongoDB的镜像:

➜  ~ docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
7b1a6ab2e44d: Pull complete
90eb44ebc60b: Pull complete
5085b59f2efb: Pull complete
c7499923d022: Pull complete
019496b6c44a: Pull complete
c0df4f407f69: Pull complete
351daa315b6c: Pull complete
a233e6240acc: Downloading [============>                                      ]   5
a233e6240acc: Pull complete
a3f57d6be64f: Pull complete
dd1b5b345323: Pull complete
Digest: sha256:5be752bc5f2ac4182252d0f15d74df080923aba39700905cb26d9f70f39e9702
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest

docker images 查看镜像

  • docker images -a,list所有镜像
~ docker images -a
REPOSITORY                           TAG                                                     IMAGE ID       CREATED         SIZE
nginx                                latest                                                  605c77e624dd   4 days ago      141MB
redis                                latest                                                  7614ae9453d1   12 days ago     113MB
  • docker images -q,只显示镜像id
➜  ~ docker images -q
605c77e624dd
7614ae9453d1
ffe9d497c324

查看指定镜像的镜像ID:

➜  ~ docker images -q mongo
dfda7a2cf273

docker image 镜像操作

➜  ~ docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

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

docker images只是提供了查看镜像的功能,而docker image则可以跟命令结合,实现docker镜像的查看、操作,功能相比docker images更强大一些。
比如使用:docker image ls -a就可以实现docker images -a

➜  ~ docker image ls -a
REPOSITORY                           TAG                                                     IMAGE ID       CREATED         SIZE
nginx                                latest                                                  605c77e624dd   4 days ago      141MB
redis                                latest                                                  7614ae9453d1   12 days ago     113MB

使用docker image COMMAND --help,可以查看对应的使用方法。比如docker image ls --help可以查看ls的使用方法。

➜  ~ docker image ls --help

Usage:  docker image ls [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  ls, list

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs

docker rmi 删除镜像

➜  ~ docker rmi --help

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents

删除MongoDB的镜像:

➜  ~ docker rmi mongo
Untagged: mongo:latest
Untagged: mongo@sha256:5be752bc5f2ac4182252d0f15d74df080923aba39700905cb26d9f70f39e9702
Deleted: sha256:dfda7a2cf27349457de1696cb5c97b422a4292af17a7ef3b7032348b5e6ff1b8
Deleted: sha256:c2a1ff2060f2163361f196626090c8efd48471780bb4752f238fd021aa259265
Deleted: sha256:88d7a07e0767b895faa1f4a8e32cb74bed4e33cd10daf2a4d43f1ba32c0f41c8
Deleted: sha256:486a009ce06bea43135986f5c89627c02b5917cc147e227ee218a2f501e596d2
Deleted: sha256:0e40e70049e1c439978b6b42f365e7c82509045af81569318d9d25ca4ae43ac8
Deleted: sha256:e32d56044445e386c117695c522d1b55b4404a67941d15373d927f6ba6ab998c
Deleted: sha256:6a74e1261a8f05a0e160b5576b517a617b1cde61e2e06caf741dc0288ab0f39a
Deleted: sha256:47ef14d94756f01bd1ab6df49c41c0df1653ef7596009418ec45f0b8fbf942eb
Deleted: sha256:d24f1d22ac6c19e32087304f478f921a57bfb717d26a1ec173ad98b892ec189c
Deleted: sha256:8e1fec7b6b6c506a19122cb48d9cffdb7c81dd3122c2cb444db0917a805937ac
Deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b

也可以使用镜像ID进行删除:

➜  ~ docker rmi $(docker images -q mongo)
Untagged: mongo:latest
Untagged: mongo@sha256:5be752bc5f2ac4182252d0f15d74df080923aba39700905cb26d9f70f39e9702
Deleted: sha256:dfda7a2cf27349457de1696cb5c97b422a4292af17a7ef3b7032348b5e6ff1b8
Deleted: sha256:c2a1ff2060f2163361f196626090c8efd48471780bb4752f238fd021aa259265
Deleted: sha256:88d7a07e0767b895faa1f4a8e32cb74bed4e33cd10daf2a4d43f1ba32c0f41c8
Deleted: sha256:486a009ce06bea43135986f5c89627c02b5917cc147e227ee218a2f501e596d2
Deleted: sha256:0e40e70049e1c439978b6b42f365e7c82509045af81569318d9d25ca4ae43ac8
Deleted: sha256:e32d56044445e386c117695c522d1b55b4404a67941d15373d927f6ba6ab998c
Deleted: sha256:6a74e1261a8f05a0e160b5576b517a617b1cde61e2e06caf741dc0288ab0f39a
Deleted: sha256:47ef14d94756f01bd1ab6df49c41c0df1653ef7596009418ec45f0b8fbf942eb
Deleted: sha256:d24f1d22ac6c19e32087304f478f921a57bfb717d26a1ec173ad98b892ec189c
Deleted: sha256:8e1fec7b6b6c506a19122cb48d9cffdb7c81dd3122c2cb444db0917a805937ac
Deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b

$符号里面表示参数,上面的例子中,参数是使用docker images命令拿到的镜像ID。

~ docker rmi $(docker images -aq) #删除所有镜像,有点危险

注意docker rmidocker rm的区别,docker rmi是删除镜像,而docker rm是删除容器。

容器相关

docker run 启动容器

docker run后面能跟的参数太多了,只列举一些比较常用的。

➜  ~ docker run --name ubuntu_docker -it ubuntu /bin/bash
root@950dedd922da:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

--name string,给容器起一个名字;
-it,以交互的方式启动容器;

使用exit退出并停止容器。
如果不想让容器停止,只想退出,使用快捷键Ctrl+P+Q即可。

docker ps 显示当前容器

使用方法:

➜  ~ docker ps --help

Usage:  docker ps [OPTIONS]

List containers

Options:
  -a, --all             Show all containers (default shows just running) 显示所有容器(默认不加-a的话,只显示正在运行的容器)
  -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 container IDs 只显示容器ID
  -s, --size            Display total file sizes 显示文件大小

docker ps默认只显示运行中的容器:

➜  ~ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS     NAMES
3de70d84f17e   ubuntu                   "/bin/bash"              2 minutes ago    Up 6 seconds              ubuntu_docker

我们把ubuntu_docker这个容器停止:

➜  ~ docker stop ubuntu_docker
ubuntu_docker

然后再使用docker ps,发现已经看不到ubuntu_docker这个容器了。

➜  ~ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS     NAMES

但我们使用docker ps -a就可以看到:

➜  ~ docker ps -a
CONTAINER ID   IMAGE    COMMAND                  CREATED             STATUS                          PORTS    NAMES
3de70d84f17e   ubuntu   "/bin/bash"              5 minutes ago       Exited (0) About a minute ago            ubuntu_docker

可以通过STATUS看出当前容器的状态是Exited.

docker start 启动容器

start后面可以跟容器名或者容器ID。

➜  ~ 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

-i表示交互式方式启动:

➜  ~ docker start -i ubuntu_docker
root@950dedd922da:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@950dedd922da:/# pwd
/

docker stop 停止容器

➜  ~ docker stop --help

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

Stop one or more running containers

Options:
  -t, --time int   Seconds to wait for stop before killing it (default 10)

使用容器名停止容器:

➜  ~ docker start ubuntu_docker
ubuntu_docker
➜  ~ docker stop ubuntu_docker
ubuntu_docker

使用容器ID停止容器:

➜  ~ docker start ubuntu_docker
ubuntu_docker
➜  ~ docker ps -a | grep "ubuntu*"
950dedd922da   ubuntu                   "/bin/bash"              10 hours ago    Up 4 seconds                                                                       ubuntu_docker
➜  ~ docker stop 950dedd922da
950dedd922da
➜  ~ docker ps -a | grep "ubuntu*"
950dedd922da   ubuntu                   "/bin/bash"              10 hours ago    Exited (0) 5 seconds ago                                                            ubuntu_docker

docker restart 重启容器

可以将一个运行中的容器重启:

➜  ~ docker ps -a | grep "ubuntu*"
950dedd922da   ubuntu                   "/bin/bash"              10 hours ago     Exited (0) About a minute ago                                                            ubuntu_docker
➜  ~ docker start ubuntu_docker
ubuntu_docker
➜  ~ docker ps -a | grep "ubuntu*"
950dedd922da   ubuntu                   "/bin/bash"              10 hours ago     Up 4 seconds                                                                       ubuntu_docker
➜  ~ docker restart ubuntu_docker
ubuntu_docker
➜  ~ docker ps -a | grep "ubuntu*"
950dedd922da   ubuntu                   "/bin/bash"              10 hours ago     Up 2 seconds                                                                       ubuntu_docker

docker kill 强制停止运行中的容器

只能kill运行中的容器,kill已经停止的容器会报错。

➜  ~ docker kill --help

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

Kill one or more running containers

Options:
  -s, --signal string   Signal to send to the container (default "KILL")

docker rm 删除容器

docker rmi是删除镜像,而docker rm则是删除容器。

➜  ~ docker rm --help

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

Remove one or more containers

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)  强制删除,可以删除运行中的容器
  -l, --link      Remove the specified link
  -v, --volumes   Remove anonymous volumes associated with the container
# 使用容器名删除
➜  ~ docker rm ubuntu_docker
ubuntu_docker
# 使用容器ID删除
➜  ~ docker rm c4b1240226be
c4b1240226be

可以结合其他命令,实现删除所有的容器:

➜  ~ docker ps -aq | xargs docker rm
➜  ~ docker rm -f $(docker ps -aq)

docker top 查看容器中运行的进程

➜  ~ docker top ubuntu_docker
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                21873               21846               0                   14:24               ?                   00:00:00            /bin/bash

也可以使用容器ID:

➜  ~ docker top 93741aafef2e
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                21873               21846               0                   14:24               ?                   00:00:00            /bin/bash

docker inspect 获取容器/镜像的元数据

可以对镜像使用,也可以对容器使用。

➜  ~ docker inspect --help

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type

我们获取一下Ubuntu的元数据:

➜  ~ docker inspect ubuntu_docker
[
    {
        "Id": "93741aafef2e15fb8572a507d09b38d83a1c9474756530f64ba3e53fe3793a7f",
        "Created": "2022-01-03T14:24:03.057478862Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 21873,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-01-03T14:24:23.066538165Z",
            "FinishedAt": "2022-01-03T14:24:17.715952449Z"
        },
        "Image": "sha256:fb52e22af1b01869e23e75089c368a1130fa538946d0411d47f964f8b1076180",
        "ResolvConfPath": "/var/lib/docker/containers/93741aafef2e15fb8572a507d09b38d83a1c9474756530f64ba3e53fe3793a7f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/93741aafef2e15fb8572a507d09b38d83a1c9474756530f64ba3e53fe3793a7f/hostname",
        "HostsPath": "/var/lib/docker/containers/93741aafef2e15fb8572a507d09b38d83a1c9474756530f64ba3e53fe3793a7f/hosts",
        "LogPath": "/var/lib/docker/containers/93741aafef2e15fb8572a507d09b38d83a1c9474756530f64ba3e53fe3793a7f/93741aafef2e15fb8572a507d09b38d83a1c9474756530f64ba3e53fe3793a7f-json.log",
        "Name": "/ubuntu_docker",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/087a725afa5929ec44225a040c7b3a7c1e5f2414b3211f9d80b373feddbd4823-init/diff:/var/lib/docker/overlay2/be3d4b1af171de1f8426513fe130dff2e86416e96319ccbd68e5c16968295d69/diff",
                "MergedDir": "/var/lib/docker/overlay2/087a725afa5929ec44225a040c7b3a7c1e5f2414b3211f9d80b373feddbd4823/merged",
                "UpperDir": "/var/lib/docker/overlay2/087a725afa5929ec44225a040c7b3a7c1e5f2414b3211f9d80b373feddbd4823/diff",
                "WorkDir": "/var/lib/docker/overlay2/087a725afa5929ec44225a040c7b3a7c1e5f2414b3211f9d80b373feddbd4823/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "93741aafef2e",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "ubuntu",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "5c5ac7895faf481a9612e5513a83d9aefc30a858a789ed8d044197bcb323e802",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/5c5ac7895faf",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "d7ee68b4d40f5b141d4f5143f3b8334226d8113ceb5de827632a93ff008bf170",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "a9c87945519e8c98be75c28bbc128a424e28088b68308a3fb26cc2befda1f40c",
                    "EndpointID": "d7ee68b4d40f5b141d4f5143f3b8334226d8113ceb5de827632a93ff008bf170",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

查看镜像的元数据:

➜  ~ docker inspect mysql
[
    {
        "Id": "sha256:0716d6ebcc1a61c5a296fcb187e71f93531e510d4e4400267e2e502103d0194c",
        "RepoTags": [
            "mysql:latest"
        ],
        "RepoDigests": [
            "mysql@sha256:99e0989e7e3797cfbdb8d51a19d32c8d286dd8862794d01a547651a896bcf00c"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-03T07:24:57.3195538Z",
        "Container": "4d9c9240ab2e61cee08cefb683ff1cf2dfb0ce578b3831c54366f0e0f144b2a6",
        "ContainerConfig": {
            "Hostname": "4d9c9240ab2e",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.12",
                "MYSQL_MAJOR=8.0",
                "MYSQL_VERSION=8.0.26-1debian10"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"mysqld\"]"
            ],
            "Image": "sha256:a976a30b759ca376fc345688cb19cb5f8b3e5600bc88840c8044d60ec4f5d989",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "20.10.7",
.........................        

进入容器

进入容器可以使用docker exec,也可以使用docker attach,两者还是有些区别的。

docker exec:Run a command in a running container,翻译过来就是在一个正在运行的容器中执行命令,exec是针对已运行的容器实例进行操作,在已运行的容器中执行命令,不创建和启动新的容器,退出shell不会导致容器停止运行。docker attach:Attach local standard input, output, and error streams to a running container,翻译过来,将本机的标准输入(键盘)、标准输出(屏幕)、错误输出(屏幕)附加到一个运行的容器,也就是说本机的输入直接输到容器中,容器的输出会直接显示在本机的屏幕上,如果退出容器的shell,容器会停止运行。
作者:bin ruan
链接:https://www.zhihu.com/question/276485274/answer/1013996168
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

➜  ~ docker exec -it ubuntu_docker bin/bash
root@93741aafef2e:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@93741aafef2e:/# pwd
/
root@93741aafef2e:/# exit
exit
➜  ~ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS     NAMES
93741aafef2e   ubuntu                   "/bin/bash"              15 minutes ago   Up 15 minutes             ubuntu_docker

从上面可以看出,使用docker exec进入容器后,输入exit退出容器,容器依然是运行的。
但是如果使用attach的话,容器就会停止,我们下面试一下。

➜  ~ docker attach --help

Usage:  docker attach [OPTIONS] CONTAINER

Attach local standard input, output, and error streams to a running container

Options:
      --detach-keys string   Override the key sequence for detaching a container
      --no-stdin             Do not attach STDIN
      --sig-proxy            Proxy all received signals to the process (default true)
➜  ~ docker attach ubuntu_docker
root@93741aafef2e:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@93741aafef2e:/# exit
exit
➜  ~ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS     NAMES	

可以看出,使用docker attach后,在容器中执行exit,会使容器停止。

docker cp 拷贝文件

➜  ~ docker cp --help

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
	docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值