e460笔记本u盘启动 (e460 u盘启动)

前面简单的介绍了如何在Linux中安装Docker,这节内容,我们学习Docker镜像启动

我们Docker启动镜像从哪里来呢?镜像由我们自己或者他人构建,构建好的镜像可以直接放在本地或者上传到远程镜像仓库。当我们运行一个Docker镜像时,会先在本地查找是否存在所要运行的镜像,如果没有则会去远程镜像仓库拉取,默认为官方的镜像仓库,当然,我们也可以改为自己的私有镜像仓库。接下来,我们先了解几个简单的命令。

docker images 列出本地主机中的镜像docker pull [OPTIONS] 拉取镜像仓库中的镜像docker search [OPTIONS] 搜索仓库中的镜像docker rmi [OPTIONS] 删除本地镜像docker rm [OPTIONS] 删除容器docker run [OPTIONS] 启动镜像,先检查本地是否存在镜像,不存在则远程仓库下载docker build [OPTIONS] 构建镜像docker ps [OPTIONS] 运行中的镜像容器docker cp [OPTIONS] 宿主机与容器之间的文件复制启动Nginx镜像

我们直接在安装好Docker的主机上执行docker run nginx


我们从运行日志可以看到这样的字眼:

12

Unable to find image 'nginx:latest' locallylatest: Pulling from library/nginx

docker发现本地不存在nginx的镜像文件,便直接去仓库中查找下载并运行,因为我们没有让镜像后台运行,所以这次运行起来的容器会随着这次远程连接断开而停止。当我按下ctrl+c时,容器便会停止

12345678910

^C2021/12/28 03:24:32 [notice] 1#1: signal 2 (SIGINT) received, exiting2021/12/28 03:24:32 [notice] 31#31: exiting2021/12/28 03:24:32 [notice] 32#32: exiting2021/12/28 03:24:32 [notice] 31#31: exit2021/12/28 03:24:32 [notice] 32#32: exit2021/12/28 03:24:32 [notice] 1#1: signal 17 (SIGCHLD) received from 312021/12/28 03:24:32 [notice] 1#1: worker process 31 exited with code 02021/12/28 03:24:32 [notice] 1#1: worker process 32 exited with code 02021/12/28 03:24:32 [notice] 1#1: exit

如果要让容器后台运行,则需要在启动时加-d这个参数,

123

[root@VM-12-3-centos ~]# docker run -d nginx8dd8b3dd27aa28f913bea43de632d105c17901561d69c502b4433e0f473ed453

我们来看一下当前运行中的容器

1234

[root@VM-12-3-centos ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES8dd8b3dd27aa nginx "/docker-entrypoint.…" 23 seconds ago Up 23 seconds 80/tcp goofy_faraday

可以看到,我们的nginx是启动起来了,但是,我们并不能访问它。容器有自己的一套虚拟系统,如:网络、文件。如果我们需要访问,则需要给容器和宿主机做一个映射,让宿主机和容器能够交互。这里,我们就给nginx增加端口和配置文件映射。我为了省事,就直接把容器中的配置文件复制出来用

1

docker cp 8dd8b3dd27aa:/etc/nginx/ ~/

接下来,我们便来建立这个映射关系

1

docker run -d --name nginx-c -v /root/nginx/:/etc/nginx/ -p 8888:80 nginx

来看看容器是否启动成功

1234

[root@VM-12-3-centos nginx]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5ae0319e1795 nginx "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 0.0.0.0:8888->80/tcp, :::8888->80/tcp nginx-c

这时候,我们便能访问我们的nginx服务,

前面已经说到,容器有自己的虚拟系统,如果需要持久化的数据不映射到宿主机上,那么当容器销毁时,数据也会随之丢失,所以,我们在用容器运行时,一定要做好数据的保存方式。

Docker常用命令

在前面,我们列出了几个常用的Docker命令,这里,我们把这几个常用命令稍微讲解一下,

docker ps

ps主要是查询正常运行的容器
docker ps 是当前正在运行的容器

1234

[root@VM-12-3-centos nginx]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5ae0319e1795 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:8888->80/tcp, :::8888->80/tcp nginx-c

这里面的CONTAINER ID很重要,后面我们的很多操作都需要基于这个CONTAINER ID或者NAMES。
docker ps -a则是列出运行中和停止中的所有容器,

docker start/stop/restart/rm [CONTAINER ID]

这几个参数这是启动/停止/重启/删除容器的参数,如:docker restart 5ae0319e1795,如果要删除容器,必须要先停止,否则会提示

1

rror response from daemon: You cannot remove a running container 5ae0319e1795f3295247e0fa14c1d19054f59578381d8367e8955f22eebdd182. Stop the container before attempting removal or force remove

docker run

docker run [OPTIONS] IMAGE [COMMAND] [ARG…],它的运行参数就比较复杂了,

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108

[root@VM-12-3-centos nginx]# 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 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 --cgroupns string Cgroup namespace to use (host|private) 'host': Run the container in the Docker host's cgroup namespace 'private': Run the container in its own private cgroup namespace '': Use the cgroup namespace as configured by the default-cgroupns-mode option on the daemon (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 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) --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

这里还是只介绍几个常用的命令参数吧,

-d: 后台运行容器-P: 容器内部端口随机映射到主机的端口-p: 指定端口映射–name: 指定一个容器的名称–cpuset: 指定CPU运行-m(–memory): 设置容器内存最大值(单位可以为 b,k,M,g;最小为4M),除了内存限制,还可以设置内存+交换分区大小(–memory-swap)–link=[]: 链接到另一个容器-v(–volume): 指定容器与宿主机的映射目录

运行示例docker run -d --name nginx-cc -v /root/nginx/:/etc/nginx/ -p 9999:80 -m 256M nginx

docker cp

cp命令主要是用于宿主机和容器间的文件复制,一般格式如下:
docker cp [OPTIONS] 容器名/容器Id:容器文件路径 宿主机文件路径 从容器复制到宿主机
docker cp [OPTIONS] 宿主机文件路径 容器名/容器Id:容器文件路径 从宿主机复制到容器中

docker inspect

inspect主要是查看容器或者镜像元数据,如:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244

[root@VM-12-3-centos nginx]# docker inspect cd1309c02a5e[ { "Id": "cd1309c02a5efc9855b0987e6dba44c6b858162f14dc7fbae4138b1ca4361dae", "Created": "2021-12-28T05:54:50.381961654Z", "Path": "/docker-entrypoint.sh", "Args": [ "nginx", "-g", "daemon off;" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 3172754, "ExitCode": 0, "Error": "", "StartedAt": "2021-12-28T05:54:50.720774527Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:f6987c8d6ed59543e9f34327c23e12141c9bad1916421278d720047ccc8e1bee", "ResolvConfPath": "/var/lib/docker/containers/cd1309c02a5efc9855b0987e6dba44c6b858162f14dc7fbae4138b1ca4361dae/resolv.conf", "HostnamePath": "/var/lib/docker/containers/cd1309c02a5efc9855b0987e6dba44c6b858162f14dc7fbae4138b1ca4361dae/hostname", "HostsPath": "/var/lib/docker/containers/cd1309c02a5efc9855b0987e6dba44c6b858162f14dc7fbae4138b1ca4361dae/hosts", "LogPath": "/var/lib/docker/containers/cd1309c02a5efc9855b0987e6dba44c6b858162f14dc7fbae4138b1ca4361dae/cd1309c02a5efc9855b0987e6dba44c6b858162f14dc7fbae4138b1ca4361dae-json.log", "Name": "/nginx-cc", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": [ "/root/nginx/:/etc/nginx/" ], "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "80/tcp": [ { "HostIp": "", "HostPort": "9999" } ] }, "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": 268435456, "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": 536870912, "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/8731f576cc3fb7511f9f9761e58d8480e2e6249889119962b9d0809e2105310b-init/diff:/var/lib/docker/overlay2/618f7e094534925da63be88e1409cf0020cc0b6561b266fafca8df4d77c07cf3/diff:/var/lib/docker/overlay2/05f6ffd411a6ec713698611d0a3732d55c7d6bf13105c736492734f1c32965cb/diff:/var/lib/docker/overlay2/47646e0e9a17db5299cbd0cbdc8a7ac300be03ed728f60fb331b6c236680859a/diff:/var/lib/docker/overlay2/fd51e460d7679d64ac84fed908c691f98ab6bcce6cddfaa947ace1071f541a52/diff:/var/lib/docker/overlay2/bebc9613df0154882c65ba436f084e864152fb77b9ff25f796d5f56bf8af7ff1/diff:/var/lib/docker/overlay2/5291486555a45b2adddeb8dbf77548c892f222b96c93208ccc87180025fb2a05/diff", "MergedDir": "/var/lib/docker/overlay2/8731f576cc3fb7511f9f9761e58d8480e2e6249889119962b9d0809e2105310b/merged", "UpperDir": "/var/lib/docker/overlay2/8731f576cc3fb7511f9f9761e58d8480e2e6249889119962b9d0809e2105310b/diff", "WorkDir": "/var/lib/docker/overlay2/8731f576cc3fb7511f9f9761e58d8480e2e6249889119962b9d0809e2105310b/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "bind", "Source": "/root/nginx", "Destination": "/etc/nginx", "Mode": "", "RW": true, "Propagation": "rprivate" } ], "Config": { "Hostname": "cd1309c02a5e", "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.21.4", "NJS_VERSION=0.7.0", "PKG_RELEASE=1~bullseye" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "Image": "nginx", "Volumes": null, "WorkingDir": "", "Entrypoint": [ "/docker-entrypoint.sh" ], "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGQUIT" }, "NetworkSettings": { "Bridge": "", "SandboxID": "f8caff2c6de91132584c5f577d8fa7a695d718a8fe91913842f8120a1fd755b2", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "80/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "9999" }, { "HostIp": "::", "HostPort": "9999" } ] }, "SandboxKey": "/var/run/docker/netns/f8caff2c6de9", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "1840b9975ae073a167161b927ba9b5cbd5f523afc1deba41b935de41ed254025", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.3", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:03", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "fed3528a99f04939a651d8b67872ec51e7b99dff8338726598e3ce0300ae7c93", "EndpointID": "1840b9975ae073a167161b927ba9b5cbd5f523afc1deba41b935de41ed254025", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.3", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:03", "DriverOpts": null } } } }]

从返回的信息中,我们可以得到,启动时所设置的启动参数。如:

1234567891011

"PortBindings": { "80/tcp": [ { "HostIp": "", "HostPort": "9999" } ] },"Binds": [ "/root/nginx/:/etc/nginx/" ]

如果,那天我们忘记之前容器启动的参数时,便可以通过inspect来帮我们找回来。

docker logs

logs主要是查询docker容器的运行日志,如:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546

[root@VM-12-3-centos ~]# docker logs 5ae0319e1795/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d//docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh/docker-entrypoint.sh: Configuration complete; ready for start up2021/12/28 03:59:20 [notice] 1#1: using the "epoll" event method2021/12/28 03:59:20 [notice] 1#1: nginx/1.21.42021/12/28 03:59:20 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)2021/12/28 03:59:20 [notice] 1#1: OS: Linux 4.18.0-305.3.1.el8.x86_642021/12/28 03:59:20 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:10485762021/12/28 03:59:20 [notice] 1#1: start worker processes2021/12/28 03:59:20 [notice] 1#1: start worker process 242021/12/28 03:59:20 [notice] 1#1: start worker process 25172.17.0.1 - - [28/Dec/2021:03:59:36 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.61.1" "-"2021/12/28 05:09:38 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down2021/12/28 05:09:38 [notice] 25#25: gracefully shutting down2021/12/28 05:09:38 [notice] 25#25: exiting2021/12/28 05:09:38 [notice] 25#25: exit2021/12/28 05:09:38 [notice] 24#24: gracefully shutting down2021/12/28 05:09:38 [notice] 24#24: exiting2021/12/28 05:09:38 [notice] 24#24: exit2021/12/28 05:09:38 [notice] 1#1: signal 17 (SIGCHLD) received from 242021/12/28 05:09:38 [notice] 1#1: worker process 24 exited with code 02021/12/28 05:09:38 [notice] 1#1: signal 29 (SIGIO) received2021/12/28 05:09:38 [notice] 1#1: signal 17 (SIGCHLD) received from 252021/12/28 05:09:38 [notice] 1#1: worker process 25 exited with code 02021/12/28 05:09:38 [notice] 1#1: exit/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d//docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh/docker-entrypoint.sh: Configuration complete; ready for start up2021/12/28 05:09:38 [notice] 1#1: using the "epoll" event method2021/12/28 05:09:38 [notice] 1#1: nginx/1.21.42021/12/28 05:09:38 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)2021/12/28 05:09:38 [notice] 1#1: OS: Linux 4.18.0-305.3.1.el8.x86_642021/12/28 05:09:38 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:10485762021/12/28 05:09:38 [notice] 1#1: start worker processes2021/12/28 05:09:38 [notice] 1#1: start worker process 242021/12/28 05:09:38 [notice] 1#1: start worker process 25