一、环境准备
(1)使用docker pull centos 下载一个centos的最新版本的镜像
[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE[root@localhost ~]# docker pull centosUsing default tag: latestlatest: Pulling from library/centosa1d0c7532777: Pull completeDigest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177Status: Downloaded newer image for centos:latestdocker.io/library/centos:latest[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 5d0da3dc9764 3 weeks ago 231MB[root@localhost ~]#
二、docker操作常用的命令
2.1 docker run 命令
(1)docker run —help 帮助信息
[root@localhost ~]# docker run --helpUsage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]Run a command in a new containerOptions:--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 and1000, 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--cgroupns string Cgroup namespace to use (host|private)'host': Run the container in the Dockerhost's cgroup namespace'private': Run the container in its own privatecgroup namespace'': Use the cgroup namespace asconfigured by thedefault-cgroupns-mode option on thedaemon (default)--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 adevice (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' topass 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 initializebefore 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 forwardssignals 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' toenable 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)--platform string Set platform if server is multi-platform capable--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--pull string Pull image before running("always"|"missing"|"never") (default "missing")--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@localhost ~]#
(2)docker run [可选参数] 镜像名称
—name 设置docker容器的名称
-d 在后台运行docker
-it 使用交互的方式运行docker
-p 执行容器的端口,如 -p 80:80
-P 随机指定端口
如下为以交互的方式启动并进入docker容器
[root@localhost ~]# docker run -it centos /bin/bash[root@3e394ae7eda7 /]#
(3)docker run -d 镜像名称 后台启动docker
如下为后台启动centos容器,这里需要注意的是,使用-d后台启动docker镜像后,如果docker镜像中没有前台进程,则docker容器停止,即当docker发现没有前台进程时就会自动停止
[root@localhost ~]# docker run -d centos3cab830ec97c4f964b62a42726e3e541f689f906e00ee2a24289f2ce44a2b94e[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3cab830ec97c centos "/bin/bash" About a minute ago Exited (0) About a minute ago inspiring_benz[root@localhost ~]#
(4)docker run -d 镜像名称 /bin/bash -C “xxx” 后台启动一个docker容器,并且执行xxx的shell脚本
如下为后台启动一个docker,并且docker容器中循环打印hello_docker,此时可以发现docker处于运行状态
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo hello_docker;sleep 1;done"9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9f427c9ff601 centos "/bin/sh -c 'while t…" 3 seconds ago Up 2 seconds nice_almeida[root@localhost ~]#
2.2 退出容器
(1)使用exit 命令,注意如果使用exit命令进入退出后,docker就停止了
[root@3e394ae7eda7 /]# exitexit[root@localhost ~]#
(2)使用快捷键 Ctrl+p Ctrl + q 快捷键可以做到退出docker容器,同时docker容器不会停止
2.3 docker ps 命令
(1)docker ps 帮助信息
[root@localhost ~]# docker ps --helpUsage: docker ps [OPTIONS]List containersOptions:-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 container IDs-s, --size Display total file sizes[root@localhost ~]#
(2)docker ps 列出正在运行的容器
如下,表示当前没有docker在运行
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@localhost ~]#
(3)docker ps -a 列出正在运行和历史运行过的docker容器
[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3e394ae7eda7 centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago unruffled_lederbergc2f1f112cbbe feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago loving_morse0150f4c51f21 feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago optimistic_albattani[root@localhost ~]#
(4)docker ps -n 个数 列出最近创建的容器
如下表示最近创建的1个容器
[root@localhost ~]# docker ps -n 1CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3e394ae7eda7 centos "/bin/bash" 7 minutes ago Exited (0) 5 minutes ago unruffled_lederberg[root@localhost ~]#
(5)docker ps -aq 显示所有运行过的容器的id
[root@localhost ~]# docker ps -aq3e394ae7eda7c2f1f112cbbe0150f4c51f21[root@localhost ~]#
2.4 docker rm -f 容器id 命令
(1)docker rm -f 容器id 删除容器
[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3e394ae7eda7 centos "/bin/bash" 14 minutes ago Up 3 minutes unruffled_lederbergc2f1f112cbbe feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago loving_morse0150f4c51f21 feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago optimistic_albattani[root@localhost ~]# docker rm -f 0150f4c51f210150f4c51f21[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3e394ae7eda7 centos "/bin/bash" 14 minutes ago Up 3 minutes unruffled_lederbergc2f1f112cbbe feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago loving_morse[root@localhost ~]#
(2)docker rm -f $(docker ps -aq) 删除所有docker容器
[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3e394ae7eda7 centos "/bin/bash" 17 minutes ago Up 6 minutes unruffled_lederbergc2f1f112cbbe feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago loving_morse[root@localhost ~]# docker rm -f $(docker ps -aq)3e394ae7eda7c2f1f112cbbe[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@localhost ~]#
(3)docker ps -aq|xargs docker rm -f 也可以删除所有的容器
[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2fe6ca22d978 centos "/bin/bash" 4 seconds ago Exited (0) 3 seconds ago centos0240dbe1daff99 centos "/bin/bash" 12 seconds ago Exited (0) 12 seconds ago centos01[root@localhost ~]# docker ps -aq|xargs docker rm -f2fe6ca22d97840dbe1daff99[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@localhost ~]#
2.5 启动和停止容器
(1)docker start 容器id 启动容器
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaa2976a9dbb1 centos "/bin/bash" 19 seconds ago Exited (0) 15 seconds ago blissful_kirch[root@localhost ~]# docker start aa2976a9dbb1aa2976a9dbb1[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaa2976a9dbb1 centos "/bin/bash" 33 seconds ago Up 2 seconds blissful_kirch[root@localhost ~]#
(2)docker restart 容器id 重启重启
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaa2976a9dbb1 centos "/bin/bash" 7 minutes ago Up 6 minutes blissful_kirch[root@localhost ~]# docker restart aa2976a9dbb1aa2976a9dbb1[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaa2976a9dbb1 centos "/bin/bash" 7 minutes ago Up 1 second blissful_kirch[root@localhost ~]#
(3)docker stop 容器id 停止容器
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaa2976a9dbb1 centos "/bin/bash" 8 minutes ago Up 30 seconds blissful_kirch[root@localhost ~]# docker stop aa2976a9dbb1aa2976a9dbb1[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaa2976a9dbb1 centos "/bin/bash" 8 minutes ago Exited (0) 4 seconds ago blissful_kirch[root@localhost ~]#
(4)docker kill 容器id 停止容器(当使用docker stop 无法停止时,可以使用docker kill 直接杀掉)
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaa2976a9dbb1 centos "/bin/bash" 9 minutes ago Up 9 seconds blissful_kirch[root@localhost ~]# docker kill aa2976a9dbb1aa2976a9dbb1[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaa2976a9dbb1 centos "/bin/bash" 9 minutes ago Exited (137) 5 seconds ago blissful_kirch[root@localhost ~]#
2.6 docker logs 查看日志命令
(1)docker logs —help 查看docker logs 的帮助信息
[root@localhost ~]# docker logs --helpUsage: docker logs [OPTIONS] CONTAINERFetch the logs of a containerOptions:--details Show extra details provided to logs-f, --follow Follow log output--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) orrelative (e.g. 42m for 42 minutes)-n, --tail string Number of lines to show from the end of the logs (default "all")-t, --timestamps Show timestamps--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) orrelative (e.g. 42m for 42 minutes)[root@localhost ~]#
(2)docker logs -tf —tail num 容器id 可以查看启动容器的最新num行的日志
如下查看10行日志,并且会一直更新。类似linux中的tail -f 的效果
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo hello_docker;sleep 1;done"9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9f427c9ff601 centos "/bin/sh -c 'while t…" 3 seconds ago Up 2 seconds nice_almeida[root@localhost ~]# docker logs -tf --tail 10 9f427c9ff6012021-10-10T15:27:02.215242337Z hello_docker2021-10-10T15:27:03.218393417Z hello_docker2021-10-10T15:27:04.220812877Z hello_docker2021-10-10T15:27:05.223520640Z hello_docker2021-10-10T15:27:06.226058326Z hello_docker2021-10-10T15:27:07.229459499Z hello_docker2021-10-10T15:27:08.232441670Z hello_docker2021-10-10T15:27:09.235056718Z hello_docker2021-10-10T15:27:10.238007922Z hello_docker2021-10-10T15:27:11.240768549Z hello_docker
2.7 docker top 查看容器的进程信息
(1)docker top 容器id 查看容器中的进程信息
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9f427c9ff601 centos "/bin/sh -c 'while t…" 8 minutes ago Up 8 minutes nice_almeida[root@localhost ~]# docker top 9f427c9ff601UID PID PPID C STIME TTY TIME CMDroot 68199 68179 0 23:24 ? 00:00:00 /bin/sh -c while true;do echo hello_docker;sleep 1;doneroot 70194 68199 0 23:32 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1[root@localhost ~]#
2.8 docker inspect 查看容器内部信息
(1)docker inspect 容器id 查看容器的内部信息
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9f427c9ff601 centos "/bin/sh -c 'while t…" 11 minutes ago Up 11 minutes nice_almeida[root@localhost ~]# docker inspect 9f427c9ff601[{"Id": "9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33","Created": "2021-10-10T15:24:06.297442771Z","Path": "/bin/sh","Args": ["-c","while true;do echo hello_docker;sleep 1;done"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 68199,"ExitCode": 0,"Error": "","StartedAt": "2021-10-10T15:24:06.590095928Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6","ResolvConfPath": "/var/lib/docker/containers/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33/resolv.conf","HostnamePath": "/var/lib/docker/containers/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33/hostname","HostsPath": "/var/lib/docker/containers/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33/hosts","LogPath": "/var/lib/docker/containers/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33-json.log","Name": "/nice_almeida","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/12f4200467ee3b6ec7fff8878dae6acd648b8967f4b1d32601d4b8adb8549c4c-init/diff:/var/lib/docker/overlay2/70d40b13bd8c041fcbd2dcffcde4522bc11106bd5e21dc5c647aa94764caa269/diff","MergedDir": "/var/lib/docker/overlay2/12f4200467ee3b6ec7fff8878dae6acd648b8967f4b1d32601d4b8adb8549c4c/merged","UpperDir": "/var/lib/docker/overlay2/12f4200467ee3b6ec7fff8878dae6acd648b8967f4b1d32601d4b8adb8549c4c/diff","WorkDir": "/var/lib/docker/overlay2/12f4200467ee3b6ec7fff8878dae6acd648b8967f4b1d32601d4b8adb8549c4c/work"},"Name": "overlay2"},"Mounts": [],"Config": {"Hostname": "9f427c9ff601","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd": ["/bin/sh","-c","while true;do echo hello_docker;sleep 1;done"],"Image": "centos","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": {"org.label-schema.build-date": "20210915","org.label-schema.license": "GPLv2","org.label-schema.name": "CentOS Base Image","org.label-schema.schema-version": "1.0","org.label-schema.vendor": "CentOS"}},"NetworkSettings": {"Bridge": "","SandboxID": "a86ecda3eaf8cf0aa94ff0c783890015394d6103e3054ac57746c9f08c9eb5d3","HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"Ports": {},"SandboxKey": "/var/run/docker/netns/a86ecda3eaf8","SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "7ac083dc3fc1b560462577a3c50c8435a2382afc3f763876b26c9b733a9d021e","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": "863db47971b47481718e154eb2b43ab2d76f8e09b32924a48074808084fe9f10","EndpointID": "7ac083dc3fc1b560462577a3c50c8435a2382afc3f763876b26c9b733a9d021e","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}}}}][root@localhost ~]#
2.9 进入docker容器的命令
(1)docker exec -it 容器id bash 重新打开一个终端的方式进入docker
如下为进入docker后重新打开一个终端,这种方式可以通过exit方式退出docker
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9f427c9ff601 centos "/bin/sh -c 'while t…" 17 minutes ago Up 17 minutes nice_almeida[root@localhost ~]# docker exec -it 9f427c9ff601 bash[root@9f427c9ff601 /]#
(2)docker attach
进入容器中正在运行的终端
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9f427c9ff601 centos "/bin/sh -c 'while t…" 19 minutes ago Up 19 minutes nice_almeida[root@localhost ~]# docker attach 9f427c9ff601hello_dockerhello_dockerhello_dockerhello_dockerhello_dockerhello_dockerhello_dockerhello_dockerhello_dockerhello_docke
2.10 从容器内拷贝文件到主机上
(1)docker cp 容器id:容器文件路径 本地文件路径
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd1df9f4c810d centos "/bin/sh -c 'while t…" 16 minutes ago Up 16 minutes confident_franklin[root@localhost ~]# docker exec -it d1df9f4c810d bash[root@d1df9f4c810d /]# lsbin etc lib lost+found mnt proc run srv tmp vardev home lib64 media opt root sbin sys usr[root@d1df9f4c810d /]# cd /opt[root@d1df9f4c810d opt]# ls[root@d1df9f4c810d opt]# echo 'hello docker' >> test.txt[root@d1df9f4c810d opt]# lstest.txt[root@d1df9f4c810d opt]# exitexit[root@localhost ~]# docker cp d1df9f4c810d:/opt/test.txt /opt/test.txt[root@localhost ~]# cat /opt/test.txthello docker[root@localhost ~]#
