前提条件
- Linux环境
- 安装好Docker,可参考Rocky Linux9下安装Docker和卸载Docker
Docker命令图
帮助命令
帮助命令,查看有哪些命令可以用 [root@localhost ~]# docker --help 查看某个命令的帮助,例如:run [root@localhost ~]# docker run --help
容器命令的使用
运行容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 常用参数: --name=NAME:为容器指定名字为NAME,不使用的话系统自动为容器命名 -d: 后台运行容器并返回容器ID,也即启动守护式容器(后台运行); -i:以交互模式运行容器,通常与 -t 同时使用; -t:为容器重新分配一个伪输入终端,通常与 -i 同时使用; -P: 随机端口映射,大写P, -p: 指定端口映射,小写p, 格式 -p 宿主机端口:容器端口 -v:挂载
使用镜像,运行容器
sudo docker run --name nginx-test -p 8080:80 -d nginx
参数说明:
-
--name nginx-test :容器名称。
-
-p 8080:80 : 端口进行映射,将宿主机(本地) 8080 端口映射到容器内部的 80 端口。
-
-d nginx : 设置容器在在后台一直运行。
操作
[hadoop@node1 ~]$ sudo docker run --name nginx-test -p 90:80 -d nginx 703ef7d2c6b4a44a18ef729f11b8fdd235941d0e3a5b2ff3d5e4d25abc71cdf8
提示:以root用户执行docker命令不需要加sudo,普通用户在执行docker命令时需要添加sudo。
查看容器
查看正在运行的容器
docker ps
查看正在所有容器,包括之前运行过已停止但还未删除的。
docker ps -a
操作
[hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 703ef7d2c6b4 nginx "/docker-entrypoint.…" 18 seconds ago Up 17 seconds 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test
[hadoop@node1 ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 703ef7d2c6b4 nginx "/docker-entrypoint.…" 18 seconds ago Up 17 seconds 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test 6a5ddf625373 nginx "/docker-entrypoint.…" 21 hours ago Exited (0) About a minute ago nginx
访问Nginx
[hadoop@node1 ~]$ curl localhost:90 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
浏览器访问
ip:90
注意:如果是虚拟机的Linux,需要关闭防火墙;如果是云服务器,需要在云安全组放开90端口。
停止容器
docker stop <容器 ID>
操作
[hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 703ef7d2c6b4 nginx "/docker-entrypoint.…" 13 minutes ago Up 13 minutes 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test [hadoop@node1 ~]$ sudo docker stop 703ef7d2c6b4 703ef7d2c6b4 [hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
停止容器后,再次curl访问90端口
[hadoop@node1 ~]$ curl localhost:90 curl: (7) Failed connect to localhost:90; Connection refused
因为nginx容器已经停止,所以访问不了了。
查看所有容器,包括已停止的容器,查看到了刚才的运行的Nginx容器
[hadoop@node1 ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 703ef7d2c6b4 nginx "/docker-entrypoint.…" 16 minutes ago Exited (0) 2 minutes ago nginx-test
重启容器
停止的容器可以通过 docker restart 重启:
$ docker restart <容器 ID>
操作
[hadoop@node1 ~]$ sudo docker restart 703ef7d2c6b4 703ef7d2c6b4
查看正在运行的docker容器
[hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 703ef7d2c6b4 nginx "/docker-entrypoint.…" 41 minutes ago Up 25 seconds 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test
进入容器
在使用 -d 参数时,容器启动后会进入后台。此时想要进入容器,可以通过以下指令进入:
-
docker attach:进入容器,退出后,容器也会停止。
-
docker exec :推荐大家使用 docker exec 命令,因为此命令会退出容器终端,但不会导致容器的停止。
操作
attach方式
[hadoop@node1 ~]$ sudo docker attach 703ef7d2c6b4 按住Ctrl + C退出 ^C2024/08/15 13:59:39 [notice] 1#1: signal 2 (SIGINT) received, exiting 2024/08/15 13:59:39 [notice] 25#25: exiting 2024/08/15 13:59:39 [notice] 25#25: exit 2024/08/15 13:59:39 [notice] 24#24: exiting 2024/08/15 13:59:39 [notice] 24#24: exit 2024/08/15 13:59:39 [notice] 1#1: signal 17 (SIGCHLD) received from 24 2024/08/15 13:59:39 [notice] 1#1: worker process 24 exited with code 0 2024/08/15 13:59:39 [notice] 1#1: worker process 25 exited with code 0 2024/08/15 13:59:39 [notice] 1#1: exit 再次查看运行的容器,发现容器已经停止运行 [hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
exec方式(推荐使用)
[hadoop@node1 ~]$ sudo docker restart 703ef7d2c6b4 703ef7d2c6b4 [hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 703ef7d2c6b4 nginx "/docker-entrypoint.…" 46 minutes ago Up 3 seconds 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test 进入容器内部 [hadoop@node1 ~]$ sudo docker exec -it 703ef7d2c6b4 /bin/bash root@703ef7d2c6b4:/# ls bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@703ef7d2c6b4:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
修改页面内容
容器内部查看页面默认的内容 root@703ef7d2c6b4:/# cat /usr/share/nginx/html/index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> 修改页面内容 root@703ef7d2c6b4:/# echo "hello world" > /usr/share/nginx/html/index.html 访问容器内部80端口内容 root@703ef7d2c6b4:/# curl localhost:80 hello world
浏览器访问ip:90,看到了修改的内容
退出容器
root@703ef7d2c6b4:/# exit exit [hadoop@node1 ~]$
查看容器日志
docker logs -f <容器ID>
操作
[hadoop@node1 ~]$ sudo docker logs -f 703ef7d2c6b4 ... 省略部分输出 ... 117.189.125.113 - - [15/Aug/2024:14:23:50 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" "-"
最后一条日志是浏览器访问时产生的
117.189.125.113 - - [15/Aug/2024:14:23:50 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" "-"
可以再次浏览器访问,看看输出日志的变化
按Ctrl + C 退出 查看日志。
查看容器进程
docker top <容器ID>或者<容器名称>
操作
[hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 703ef7d2c6b4 nginx "/docker-entrypoint.…" About an hour ago Up 27 minutes 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test [hadoop@node1 ~]$ sudo docker top 703ef7d2c6b4 UID PID PPID C STIME TTY TIME CMD root 27814 27793 0 22:01 ? 00:00:00 nginx: master process nginx -g daemon off; 101 27850 27814 0 22:01 ? 00:00:00 nginx: worker process 101 27851 27814 0 22:01 ? 00:00:00 nginx: worker process [hadoop@node1 ~]$ sudo docker top nginx-test UID PID PPID C STIME TTY TIME CMD root 27814 27793 0 22:01 ? 00:00:00 nginx: master process nginx -g daemon off; 101 27850 27814 0 22:01 ? 00:00:00 nginx: worker process 101 27851 27814 0 22:01 ? 00:00:00 nginx: worker process [hadoop@node1 ~]$
查看容器资源占用情况
显示容器资源的使用情况,包括:CPU、内存、网络 I/O 等。
docker stats [OPTIONS] [CONTAINER...] OPTIONS: --all , -a :显示所有的容器,包括未运行的。 --format :指定返回值的模板文件。 --no-stream :展示当前状态就直接退出了,不再实时更新。 --no-trunc :不截断输出。 example: docker stats mynginx # 容器名 docker stats af7928654200 # 容器ID
操作
[root@localhost ~]# docker run --name nginx-test -p 8080:80 -d nginx [root@localhost ~]# docker stats nginx-test
结果动态刷新
按Ctrl + c返回命令行。
暂停容器运行及取消暂停容器运行
暂停容器帮助 [root@localhost ~]# docker pause --help Usage: docker pause CONTAINER [CONTAINER...] Pause all processes within one or more containers Aliases: docker container pause, docker pause 取消暂停容器帮助 [root@localhost ~]# docker unpause --help Usage: docker unpause CONTAINER [CONTAINER...] Unpause all processes within one or more containers Aliases: docker container unpause, docker unpause
操作
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fe352b7ad389 nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp nginx-test [root@localhost ~]# curl localhost:8080 ... 有返回内容 暂停容器 [root@localhost ~]# docker pause nginx-test nginx-test [root@localhost ~]# curl localhost:8080 无返回内容 取消暂停容器 [root@localhost ~]# docker unpause nginx-test nginx-test [root@localhost ~]# curl localhost:8080 ... 有返回内容
容器差异对比
修改容器
进入容器 [root@localhost ~]# docker exec -it nginx-test /bin/bash 查看容器内部nginx首页 root@fe352b7ad389:/# ls /usr/share/nginx/html/ 50x.html index.html 修改首页 容器为精简版 root@fe352b7ad389:/# vi 1.txt bash: vi: command not found root@fe352b7ad389:/# echo 'hello world' > /usr/share/nginx/html/index.html root@fe352b7ad389:/# exit exit [root@localhost ~]# curl localhost:8080 hello world
对比
对比命令帮助 [root@localhost ~]# docker diff --help Usage: docker diff CONTAINER Inspect changes to files or directories on a container's filesystem Aliases: docker container diff, docker diff 对比 [root@localhost ~]# docker diff nginx-test C /etc C /etc/nginx C /etc/nginx/conf.d C /etc/nginx/conf.d/default.conf C /root A /root/.bash_history C /usr C /usr/share C /usr/share/nginx C /usr/share/nginx/html C /usr/share/nginx/html/index.html C /var C /var/cache C /var/cache/nginx A /var/cache/nginx/scgi_temp A /var/cache/nginx/uwsgi_temp A /var/cache/nginx/client_temp A /var/cache/nginx/fastcgi_temp A /var/cache/nginx/proxy_temp C /run A /run/nginx.pid
看到一些配置文件在容器运行时有更改,添加了一些缓存文件,同时index.html的修改体现出来了。
容器复制文件
帮助命令
[root@localhost ~]# 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. Aliases: docker container cp, docker cp Options: -a, --archive Archive mode (copy all uid/gid information) -L, --follow-link Always follow symbol link in SRC_PATH -q, --quiet Suppress progress output during copy. Progress output is automatically suppressed if no terminal is attached
从容器复制文件到宿主机
复制nginx容器的index.html到宿主机的当前目录 [root@localhost ~]# docker cp nginx-test:/usr/share/nginx/html/index.html ./ Successfully copied 2.05kB to /root/./ [root@localhost ~]# ls anaconda-ks.cfg index.html [root@localhost ~]# cat index.html hello world
从宿主机复制文件到容器
修改宿主机index.html文件 [root@localhost ~]# echo "hello docker" >> index.html [root@localhost ~]# cat index.html hello world hello docker 复制宿主机文件到容器 [root@localhost ~]# docker cp ./index.html nginx-test:/usr/share/nginx/html/index.html Successfully copied 2.05kB to nginx-test:/usr/share/nginx/html/index.html 再次访问8080端口 [root@localhost ~]# curl localhost:8080 hello world hello docker
查看Docker容器底层信息
docker inspect记录底层信息包括,容器的配置及状态信息
docker inspect
操作
[hadoop@node1 ~]$ sudo docker inspect nginx-test [ { "Id": "703ef7d2c6b4a44a18ef729f11b8fdd235941d0e3a5b2ff3d5e4d25abc71cdf8", "Created": "2024-08-15T13:14:23.488407988Z", "Path": "/docker-entrypoint.sh", "Args": [ "nginx", "-g", "daemon off;" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 27814, "ExitCode": 0, "Error": "", "StartedAt": "2024-08-15T14:01:09.620292061Z", "FinishedAt": "2024-08-15T13:59:39.032902367Z" }, "Image": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85", "ResolvConfPath": "/var/lib/docker/containers/703ef7d2c6b4a44a18ef729f11b8fdd235941d0e3a5b2ff3d5e4d25abc71cdf8/resolv.conf", "HostnamePath": "/var/lib/docker/containers/703ef7d2c6b4a44a18ef729f11b8fdd235941d0e3a5b2ff3d5e4d25abc71cdf8/hostname", "HostsPath": "/var/lib/docker/containers/703ef7d2c6b4a44a18ef729f11b8fdd235941d0e3a5b2ff3d5e4d25abc71cdf8/hosts", "LogPath": "/var/lib/docker/containers/703ef7d2c6b4a44a18ef729f11b8fdd235941d0e3a5b2ff3d5e4d25abc71cdf8/703ef7d2c6b4a44a18ef729f11b8fdd235941d0e3a5b2ff3d5e4d25abc71cdf8-json.log", "Name": "/nginx-test", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "bridge", "PortBindings": { "80/tcp": [ { "HostIp": "", "HostPort": "90" } ] }, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "ConsoleSize": [ 24, 80 ], "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", "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": [], "BlkioDeviceWriteBps": [], "BlkioDeviceReadIOps": [], "BlkioDeviceWriteIOps": [], "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": [], "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", "/sys/devices/virtual/powercap" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/85c05f16ba70e0f7b0ae92a6308b58f607e549f9363b06e245e336cd988be4fd-init/diff:/var/lib/docker/overlay2/b02fecf833fb25cd1e94324f315c363cc73162f4985aa53923b4e8fbf5055d81/diff:/var/lib/docker/overlay2/bac61cbe5790634305a8432a9dfb0b0034d09cd8dc45fd6d1ae25631ecbd8daf/diff:/var/lib/docker/overlay2/e8bb25e3796f5c494ca93d2481199f291206a5cd3cce2a4886596ac5156ddf19/diff:/var/lib/docker/overlay2/4df03de3126bc9f6084e68b4ce7bb5f617e52a8a12e06d8375a02c99fe75306a/diff:/var/lib/docker/overlay2/fce829188934d76a0d63f2346c5969ad3d1a559f1d7737f43cb25bfda66dd350/diff:/var/lib/docker/overlay2/f920083f8c3b268e9444ffe6768575ccd9db9f512be0297baabd12e4ed1175b3/diff", "MergedDir": "/var/lib/docker/overlay2/85c05f16ba70e0f7b0ae92a6308b58f607e549f9363b06e245e336cd988be4fd/merged", "UpperDir": "/var/lib/docker/overlay2/85c05f16ba70e0f7b0ae92a6308b58f607e549f9363b06e245e336cd988be4fd/diff", "WorkDir": "/var/lib/docker/overlay2/85c05f16ba70e0f7b0ae92a6308b58f607e549f9363b06e245e336cd988be4fd/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "703ef7d2c6b4", "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.5", "NJS_VERSION=0.7.1", "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": "c3315125e38a83e2b871a1fc3ceb6547310f731215b83667972afd1e4f4caab5", "SandboxKey": "/var/run/docker/netns/c3315125e38a", "Ports": { "80/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "90" }, { "HostIp": "::", "HostPort": "90" } ] }, "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "cd8cb3aa2535557c36b545a81dc1e9cc7c745dbc4103766e16f9b0ef39c0c2ea", "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, "MacAddress": "02:42:ac:11:00:02", "NetworkID": "49699130213cd4e33a15ab50fa9d6f6244d1ce9f660c28fb33730c239f7ecc34", "EndpointID": "cd8cb3aa2535557c36b545a81dc1e9cc7c745dbc4103766e16f9b0ef39c0c2ea", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "DriverOpts": null, "DNSNames": null } } } } ] [hadoop@node1 ~]$
导出容器
docker export
操作
导出容器快照 [hadoop@node1 ~]$ sudo docker export 703ef7d2c6b4 > mynginx.tar [hadoop@node1 ~]$ ls --add-repo docker-client docker-common docker-latest docker-logrotate flinkdemo-1.0-SNAPSHOT.jar soft bin docker-client-latest docker-engine docker-latest-logrotate flink-connector-kafka-1.17.1.jar mynginx.tar softinstall [hadoop@node1 ~]$
导入容器
docker import
操作
[hadoop@node1 ~]$ ll total 141244 ... -rw-rw-r-- 1 hadoop hadoop 144026112 Aug 15 22:43 mynginx.tar drwxrwxr-x 7 hadoop hadoop 4096 Mar 29 00:09 soft drwxrwxr-x 2 hadoop hadoop 4096 Mar 28 23:51 softinstall 导入容器快照成为镜像 [hadoop@node1 ~]$ cat mynginx.tar | sudo docker import - test/mynginx:v1 sha256:ada934093571adf88696ef0da86537c1f81d66635be079ad5da36f52e0605ac8 或者 直接使用docker import导入容器快照 [hadoop@node1 ~]$ sudo docker import mynginx.tar test/mynginx1:v1.0 sha256:bd0f5df43117a76d81eaa820d25e963c9ce3dbfabb8100948f69f8c450061dd9 [hadoop@node1 ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE test/mynginx1 v1.0 bd0f5df43117 6 seconds ago 140MB test/mynginx v1 ada934093571 13 minutes ago 140MB nginx latest 605c77e624dd 2 years ago 141MB hello-world latest feb5d9fea6a5 2 years ago 13.3kB
如果不写版本号,默认TAG是latest
此外,也可以通过指定 URL 或者某个目录来导入,例如:
$ docker import http://example.com/exampleimage.tgz example/imagerepo
运行容器,报错如下
[hadoop@node1 ~]$ sudo docker run --name mynginx1 -p 92:80 -d test/mynginx1:v1.0 docker: Error response from daemon: no command specified. See 'docker run --help'.
报错提示需要指定COMMAND
查看COMMAND
查看COMMAND为:/docker-entrypoint.sh nginx -g 'daemon off;' [hadoop@node1 ~]$ sudo docker ps --no-trunc CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1cfee1f2323f08c75d0fef8b42f8e2bde9094b2bfef416f48198e36ec1c4b331 nginx "/docker-entrypoint.sh nginx -g 'daemon off;'" 9 minutes ago Up 9 minutes 0.0.0.0:91->80/tcp, :::91->80/tcp nginx-test1 703ef7d2c6b4a44a18ef729f11b8fdd235941d0e3a5b2ff3d5e4d25abc71cdf8 nginx "/docker-entrypoint.sh nginx -g 'daemon off;'" 2 hours ago Up About an hour 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test 带上命令参数运行,成功如下 [hadoop@node1 ~]$ sudo docker run --name mynginx1 -p 92:80 -d test/mynginx1:v1.0 /docker-entrypoint.sh nginx -g 'daemon off;' aca58f0340a60ec8f93936c945e83cd8547e4b4669b2d5f9f6620b7dd909443b 查看镜像 [hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES aca58f0340a6 test/mynginx1:v1.0 "/docker-entrypoint.…" 14 seconds ago Up 13 seconds 0.0.0.0:92->80/tcp, :::92->80/tcp mynginx1 1cfee1f2323f nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:91->80/tcp, :::91->80/tcp nginx-test1 703ef7d2c6b4 nginx "/docker-entrypoint.…" 2 hours ago Up About an hour 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test 命令访问页面 [hadoop@node1 ~]$ curl localhost:92 hello world
移除容器
docker rm <容器ID>/<name>
操作
[hadoop@node1 ~]$ sudo docker rm mynginx1 Error response from daemon: cannot remove container "/mynginx1": container is running: stop the container before removing or force remove
移除容器之前必须先删除容器,或者加上-f强制删除
[hadoop@node1 ~]$ sudo docker rm --help Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...] Remove one or more containers Aliases: docker container rm, docker container remove, docker rm 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 [hadoop@node1 ~]$
先停止容器,再删除容器
停止容器 [hadoop@node1 ~]$ sudo docker stop mynginx1 mynginx1 删除容器 [hadoop@node1 ~]$ sudo docker rm mynginx1 mynginx1 查看正在运行的容器,没有mynginx1 [hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1cfee1f2323f nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:91->80/tcp, :::91->80/tcp nginx-test1 703ef7d2c6b4 nginx "/docker-entrypoint.…" 2 hours ago Up About an hour 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test 查看所有容器,也没有mynginx1 [hadoop@node1 ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1cfee1f2323f nginx "/docker-entrypoint.…" 25 minutes ago Up 25 minutes 0.0.0.0:91->80/tcp, :::91->80/tcp nginx-test1 703ef7d2c6b4 nginx "/docker-entrypoint.…" 2 hours ago Up About an hour 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test
-f强制删除容器
启动容器 [hadoop@node1 ~]$ sudo docker run --name mynginx2 -p 93:80 -d test/mynginx:v1 /docker-entrypoint.sh nginx -g 'daemon off;' 8914e1b3fe3fdeca9db2143976bceab6fcaecb80ba9ff474758b0d2a04c1b512 查看运行的容器 [hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8914e1b3fe3f test/mynginx:v1 "/docker-entrypoint.…" 13 seconds ago Up 13 seconds 0.0.0.0:93->80/tcp, :::93->80/tcp mynginx2 1cfee1f2323f nginx "/docker-entrypoint.…" 27 minutes ago Up 27 minutes 0.0.0.0:91->80/tcp, :::91->80/tcp nginx-test1 703ef7d2c6b4 nginx "/docker-entrypoint.…" 2 hours ago Up About an hour 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test 访问93端口 [hadoop@node1 ~]$ curl localhost:93 hello world 删除容器 [hadoop@node1 ~]$ sudo docker rm -f 8914e1b3fe3f 8914e1b3fe3f 再次查看运行的容器 [hadoop@node1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1cfee1f2323f nginx "/docker-entrypoint.…" 29 minutes ago Up 29 minutes 0.0.0.0:91->80/tcp, :::91->80/tcp nginx-test1 703ef7d2c6b4 nginx "/docker-entrypoint.…" 2 hours ago Up About an hour 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test 查看所有容器 [hadoop@node1 ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1cfee1f2323f nginx "/docker-entrypoint.…" 30 minutes ago Up 30 minutes 0.0.0.0:91->80/tcp, :::91->80/tcp nginx-test1 703ef7d2c6b4 nginx "/docker-entrypoint.…" 2 hours ago Up About an hour 0.0.0.0:90->80/tcp, :::90->80/tcp nginx-test
批量删除容器
批量删除容器 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b27b99ac2f44 nginx "/docker-entrypoint.…" 2 weeks ago Exited (255) 13 hours ago 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp nginx-test 80cd29b75497 hello-world "/hello" 2 weeks ago Exited (0) 2 weeks ago charming_liskov e62e04394ccc hello-world "/hello" 4 weeks ago Exited (0) 4 weeks ago jolly_driscoll [root@localhost ~]# docker rm $(docker ps -a -q) b27b99ac2f44 80cd29b75497 e62e04394ccc [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES docker ps帮助 [root@localhost ~]# docker ps --help Usage: docker ps [OPTIONS] List containers Aliases: docker container ls, docker container list, docker container ps, docker ps Options: -a, --all Show all containers (default shows just running) -f, --filter filter Filter output based on conditions provided --format string Format output using a custom template: 'table': Print output in table format with column headers (default) 'table TEMPLATE': Print output in table format using the given Go template 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates -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
思考
Docker容器与宿主机的关系?