网站忘了怎么办,网站免费加速器,wordpress修改代码后,用来做收录的网站文章目录 一、使用容器运行Nginx应用1_使用docker run命令运行Nginx应用1 观察下载容器镜像过程2 观察容器运行情况 2_访问容器中运行的Nginx服务1 确认容器IP地址2 容器网络说明3 使用curl命令访问 二、Docker命令1_Docker命令获取帮助方法2_Docker官网提供的命令说明3_docker… 文章目录 一、使用容器运行Nginx应用1_使用docker run命令运行Nginx应用1 观察下载容器镜像过程2 观察容器运行情况 2_访问容器中运行的Nginx服务1 确认容器IP地址2 容器网络说明3 使用curl命令访问 二、Docker命令1_Docker命令获取帮助方法2_Docker官网提供的命令说明3_docker命令应用1 docker run2 docker ps3 docker inspect4 docker exec5 docker attach6 docker stop7 docker start8 docker top9 docker rm 一、使用容器运行Nginx应用
1_使用docker run命令运行Nginx应用
1 观察下载容器镜像过程
查找本地容器镜像文件
执行命令过程一下载容器镜像
[rootlocalhost ~]# docker run -d nginx:latest
Unable to find image nginx:latest locally
latest: Pulling from library/nginx
a2abf6c4d29d: Downloading 1.966MB/31.36MB 下载中
a9edb18cadd1: Downloading 1.572MB/25.35MB
589b7251471a: Download complete 下载完成
186b1aaa4aa6: Download complete
b4df32aa5a72: Waiting 等待下载
a0bcbecc962e: Waiting执行命令过程二下载容器镜像
[rootlocalhost ~]# docker run -d nginx:latest
Unable to find image nginx:latest locally
latest: Pulling from library/nginx
a2abf6c4d29d: Downloading 22.87MB/31.36MB
a9edb18cadd1: Downloading 22.78MB/25.35MB
589b7251471a: Waiting
186b1aaa4aa6: Waiting
b4df32aa5a72: Waiting执行命令过程三下载容器镜像
[rootlocalhost ~]# docker run -d nginx:latest
Unable to find image nginx:latest locally
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 下载完成
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Waiting 等待下载2 观察容器运行情况
[rootlocalhost ~]# docker run -d nginx:latest
9834c8c18a7c7c89ab0ea4119d11bafe9c18313c8006bc02ce57ff54d9a1cc0c命令解释说明docker run启动一个容器-d把容器镜像中需要执行的命令以daemon守护进程的方式运行nginx应用容器镜像的名称通常表示该镜像为某一个软件latest表示上述容器镜像的版本表示最新版本用户可自定义其标识例如v1或v2等
[rootlocalhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9834c8c18a7c nginx:latest /docker-entrypoint.… 24 seconds ago Up 23 seconds 80/tcp condescending_pare命令解释 docker ps 类似于Linux系统的ps命令查看正在运行的容器如果想查看没有运行的容器需要在此命令后使用 --all 输出内容解释
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES9834c8c18a7cnginx:latest“/docker-entrypoint.…”24 seconds agoUp 23 seconds80/tcpcondescending_pare容器ID容器镜像容器中运行的命令容器创建时间容器状态容器提供访问应用端口容器名称
2_访问容器中运行的Nginx服务
1 确认容器IP地址
实际工作中不需要此步操作9834 是容器ID缩写。
[rootlocalhost ~]# docker inspect 9834c8c18a7cGlobalIPv6Address: ,GlobalIPv6PrefixLen: 0,IPAddress: 172.17.0.2, 容器IP地址IPPrefixLen: 16,IPv6Gateway: ,MacAddress: 02:42:ac:11:00:02,Networks: {bridge: {IPAMConfig: null,Links: null,Aliases: null,NetworkID: d3de2fdbc30ee36a55c1431ef3ae4578392e552009f00b2019b4720735fe5a60,EndpointID: d91f47c9f756ff22dc599a207164f2e9366bd0c530882ce0f08ae2278fb3d50c,Gateway: 172.17.0.1,IPAddress: 172.17.0.2, 容器IP地址IPPrefixLen: 16,IPv6Gateway: ,GlobalIPv6Address: ,GlobalIPv6PrefixLen: 0,MacAddress: 02:42:ac:11:00:02,DriverOpts: null}}}}
]命令解释 docker inspect 为查看容器结构信息命令 9834 为前面生成的容器ID号前4位使用这个ID号时由于其较长使用时能最短识别即可。 2 容器网络说明 查看系统网络接口信息
ip a s # ip addr showdocker0网桥用于为容器提供桥接转发到主机之外的网络
5: docker0: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue state UP group defaultlink/ether 02:42:d5:c3:d4:cc brd ff:ff:ff:ff:ff:ffinet 172.17.0.1/16 brd 172.17.255.255 scope global docker0valid_lft forever preferred_lft foreverinet6 fe80::42:d5ff:fec3:d4cc/64 scope linkvalid_lft forever preferred_lft forever与容器中的虚拟网络设备在同一个命名空间中用于把容器中的网络连接到主机
9: veth393deceif8: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue master docker0 state UP group defaultlink/ether 02:e3:11:58:54:0f brd ff:ff:ff:ff:ff:ff link-netnsid 0inet6 fe80::e3:11ff:fe58:540f/64 scope linkvalid_lft forever preferred_lft forever3 使用curl命令访问
返回结果表示访问成功
[rootlocalhost ~]# curl 172.17.0.2
!DOCTYPE html
html
head
titleWelcome 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
h1Welcome to nginx!/h1
pIf you see this page, the nginx web server is successfully installed and
working. Further configuration is required./ppFor online documentation and support please refer to
a hrefhttp://nginx.org/nginx.org/a.br/
Commercial support is available at
a hrefhttp://nginx.com/nginx.com/a./ppemThank you for using nginx./em/p
/body
/html二、Docker命令
1_Docker命令获取帮助方法
[rootlocalhost ~]# docker -h
Flag shorthand -h has been deprecated, use --helpUsage: docker [OPTIONS] COMMANDA self-sufficient runtime for containersCommon Commands:run Create and run a new container from an imageexec Execute a command in a running containerps List containersbuild Build an image from a Dockerfilepull Download an image from a registrypush Upload an image to a registryimages List imageslogin Authenticate to a registrylogout Log out from a registrysearch Search Docker Hub for imagesversion Show the Docker version informationinfo Display system-wide informationManagement Commands:builder Manage buildsbuildx* Docker Buildxcompose* Docker Composecontainer Manage containerscontext Manage contextsimage Manage imagesmanifest Manage Docker image manifests and manifest listsnetwork Manage networksplugin Manage pluginssystem Manage Dockertrust Manage trust on Docker imagesvolume Manage volumesSwarm Commands:swarm Manage SwarmCommands:attach Attach local standard input, output, and error streams to a running containercommit Create a new image from a containers changescp Copy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a containers filesystemevents Get real time events from the serverexport Export a containers filesystem as a tar archivehistory Show the history of an imageimport Import the contents from a tarball to create a filesystem imageinspect Return low-level information on Docker objectskill Kill one or more running containersload Load an image from a tar archive or STDINlogs Fetch the logs of a containerpause Pause all processes within one or more containersport List port mappings or a specific mapping for the containerrename Rename a containerrestart Restart one or more containersrm Remove one or more containersrmi Remove one or more imagessave Save one or more images to a tar archive (streamed to STDOUT by default)start Start one or more stopped containersstats Display a live stream of container(s) resource usage statisticsstop Stop one or more running containerstag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGEtop Display the running processes of a containerunpause Unpause all processes within one or more containersupdate Update configuration of one or more containerswait Block until one or more containers stop, then print their exit codesGlobal Options:--config string Location of client config files (default /root/.docker)-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with docker context use)-D, --debug Enable debug mode-H, --host list Daemon socket 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 quitRun docker COMMAND --help for more information on a command.For more help on how to use Docker, head to https://docs.docker.com/go/guides/2_Docker官网提供的命令说明
网址链接https://docs.docker.com/reference/ 3_docker命令应用
1 docker run
docker run -i -t --name c1 centos:latest bash
[root948f234e22a1 /]#命令解释docker run运行一个命令在容器中命令是主体没有命令容器就会消亡-i交互式-t提供终端–name c1把将运行的容器命名为c1centos:latest使用centos最新版本容器镜像bash在容器中执行的命令
注意看主机名
[root948f234e22a1 /]#查看网络信息
[root948f234e22a1 /]# ip a s
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft forever
12: eth0if13: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue state UP group defaultlink/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0valid_lft forever preferred_lft forever查看进程
[root948f234e22a1 /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 12036 2172 pts/0 Ss 09:58 0:00 bash
root 16 0.0 0.0 44652 1784 pts/0 R 10:02 0:00 ps aux查看用户
[root948f234e22a1 /]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin查看目录(跟我们系统基本一样
[root948f234e22a1 /]# pwd
/
[root948f234e22a1 /]# ls
bin etc lib lostfound mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr退出命令执行观察容器运行情况
[root948f234e22a1 /]# exit
exit
[rootlocalhost ~]#2 docker ps
[rootlocalhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES命令解释 docker ps 查看正在运行的容器本案例由于没有命令在容器中运行因此容器被停止了所以本次查看没有结果。 [rootlocalhost ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
948f234e22a1 centos:latest bash 10 minutes ago Exited (0) 2 minutes ago c1CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES948f234e22a1centos:latest“bash”10 minutes agoExited (0) 2 minutes agoc1 命令解释 docker ps --all 可以查看正在运行的和停止运行的容器 3 docker inspect
[rootlocalhost ~]# docker run -it --name c2 centos:latest bash
[rootb4182fd1f2cc /]# 操作说明
在上述提示符处按住ctrl键再按p键与q键可以退出交互式的容器容器会处于运行状态。
[rootlocalhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4182fd1f2cc centos:latest bash About a minute ago Up About a minute c2命令解释 可以看到容器处于运行状态 [rootlocalhost ~]# docker inspect c2
Networks: {bridge: {IPAMConfig: null,Links: null,Aliases: null,NetworkID: d3de2fdbc30ee36a55c1431ef3ae4578392e552009f00b2019b4720735fe5a60,EndpointID: d1a2b7609f2f73a6cac67229a4395eef293f695c0ac4fd6c9c9e6913c9c85c1c,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 inpect 查看容器详细信息 4 docker exec
[rootlocalhost ~]# docker exec -it c2 ls /root
anaconda-ks.cfg anaconda-post.log original-ks.cfg命令解释docker exec在容器外实现与容器交互执行某命令-it交互式c2正在运行的容器名称ls /root在正在运行的容器中运行相关的命令
下面命令与上面命令执行效果一致
[rootlocalhost ~]# docker exec c2 ls /root
anaconda-ks.cfg
anaconda-post.log
original-ks.cfg5 docker attach
查看正在运行的容器
[rootlocalhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4182fd1f2cc centos:latest bash 8 minutes ago Up 8 minutes c2
ef02ada6ba49 nginx:latest /docker-entrypoint.… 2 hours ago Up 4 seconds 80/tcp cool_bardeen进入容器内部
[rootlocalhost ~]# docker attach c2
[rootb4182fd1f2cc /]# 命令解释docker attach类似于ssh命令可以进入到容器中c2正在运行的容器名称 说明 docker attach 退出容器时如不需要容器再运行可直接使用exit退出如需要容器继续运行可使用ctrlpq 另外由于另外一个 nginx 容器并不是交互式的所以不允许我们进入内部如果尝试的话会卡住
docker attach cool_bardeen # 卡住,并且Ctrlc退出时容器也会随之停止6 docker stop
[rootlocalhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4182fd1f2cc centos:latest bash 11 minutes ago Up 11 minutes c2
[rootlocalhost ~]# docker stop b4182
b4182
[rootlocalhost ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4182fd1f2cc centos:latest bash 12 minutes ago Exited (0) 28 seconds ago c2支持批量停止以及使用容器名称 7 docker start
[rootlocalhost ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4182fd1f2cc centos:latest bash 12 minutes ago Exited (0) 28 seconds ago c2
[rootlocalhost ~]# docker start b4182
b4182
[rootlocalhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4182fd1f2cc centos:latest bash 14 minutes ago Up 16 seconds c2还可以批量开启以及使用容器名称代替容器ID 8 docker top
在Docker Host查看容器中运行的进程信息
docker top c2如下输出
UIDPIDPPIDCSTIMETTYTIMECMDroot394835394784018:37pts/000:00:00bash
命令解释
输出信息说明UID容器中运行的命令用户IDPID容器中运行的命令PIDPPID容器中运行的命令父PID由于PPID是一个容器此可指为容器在Docker Host中进程IDC占用CPU百分比STIME启动时间TTY运行所在的终端TIME运行时间CMD执行的命令
[rootlocalhost ~]# ps aux | grep 394784
root 394784 0.0 0.4 1237928 15756 ? Sl 05:43 0:00 /usr/bin/containerd-shim-runc-v2 -namespace moby -id b4182fd1f2ccfea5799bd63206ed562c38e53b18c93def3a56262fd73122e8ab -address /run/containerd/containerd.sock
root 408828 0.0 0.0 221680 2304 pts/0 S 05:47 0:00 grep --colorauto 394784
[rootlocalhost ~]# ps aux | grep 394835
root 394835 0.0 0.0 12052 3200 pts/0 Ss 05:43 0:00 bash
root 399829 0.0 0.0 221680 2304 pts/0 S 05:44 0:00 grep --colorauto 394835
[rootlocalhost ~]# docker exec -it c2 ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 21:43 pts/0 00:00:00 bash
root 14 0 0 21:45 pts/1 00:00:00 ps -efdocker top 查看container内进程信息指在docker host上查看与docker exec -it c2 ps -ef不同。 另外一个容器中运行多个进程也是存在的可以尝试去访问 nginx 看看。 9 docker rm
如果容器已停止使用此命令可以直接删除如果容器处于运行状态则需要提前关闭容器后再删除容器。
下面演示容器正在运行关闭后删除的方法。
指定删除容器
[rootlocalhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4182fd1f2cc centos:latest bash 30 minutes ago Up 16 minutes c2停止容器
docker stop c2 # 或 docker stop b4182fd1f2cc删除容器
docker rm c2 # 或 docker rm b4182fd1f2cc批量删除容器
-a 与 --all 是等价的
[rootlocalhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3bd60ca310ca centos:latest bash About an hour ago Exited (127) 48 minutes ago c1
c71fb30f25da nginx:latest /docker-entrypoint.… 2 hours ago Exited (0) 2 hours ago zen_swirles
ef02ada6ba49 nginx:latest /docker-entrypoint.… 2 hours ago Exited (0) 20 minutes ago cool_bardeen批量删除容器
docker ps --all | awk {if (NR2){print $1}} | xargs docker rm上述命令可以把第一个命令执行的结果看作文本作为 awk 输入awk 将从第二行开始每行的第一个元素(元素以空格作为分隔)输出作为 xargs 的输入。