Docker自学文档
学前零星使用操作
一、基础镜像篇
二、基础容器篇
三、容器数据卷
四、DockerFile构建镜像
五、网络篇
六、Docker Compose
docker 在已经启动的容器中开启端口映射
ubuntu22.04使用docker-compose部署最美博客系统
本文档使用 MrDoc 发布
-
+
home page
二、基础容器篇
# 三、容器篇 ## 1、基本容器命令 说明:有了镜像才可以创建容器。 实验前准备:下载一个最新的centos的镜像,当前最新的是centos8.x ``` [root@localhost ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest ``` ### 1.1、docker run #启动镜像 #### (1)常用参数说明 ``` --name #为容器分配一个名称 --detach,-d #在后台运行容器并打印容器ID --interactive,-i #即使未连接STDIN,也应使其保持打开状态, #-it 使用交互方式运行,进入容器查看内容 --tty,-t #分配伪TTY --publish,-p #将容器的端口发布到主机,指定容器端口(小p) -p #ip:主机端口:容器端口 -p #主机端口:容器端口(常用) -p #容器端口 --publish-all,-P #将所有公开的端口发布到随机端口,随机指定端口(大P) ``` #### (2)使用交互方式进入容器,并退出 ``` [root@localhost ~]# docker run -it centos /bin/bash #使用bash命令 [root@0fdfd540855e /]# ls #查看容器内的centos,基础版本,很多命令不完善 bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@0fdfd540855e /]# pwd / [root@0fdfd540855e /]# exit #停止运行并退出,使用快捷键ctrl+p+q,容器运行并退出 exit ``` ### 1.2、docker ps #显示所有正在运行的容器 #### (1)常用参数 ``` -a #列出当前正在运行的容器+历史运行的容器 -n=? #显示最近创建的容器,后面跟个数 -q #只显示容器的编号 ``` #### (2)操作示例 ``` [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]# docker ps -a #显示正在运行过的容器+历史运行的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0fdfd540855e centos "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago compassionate_shirley 36ad1e1372aa 9c7a54a9a43c "/hello" 24 hours ago Exited (0) 24 hours ago epic_franklin [root@localhost ~]# docker ps -a -n=1 #显示当前正在运行的一个容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0fdfd540855e centos "/bin/bash" 8 minutes ago Exited (0) 6 minutes ago compassionate_shirley [root@localhost ~]# docker ps -aq #显示所有运行的容器的编号 0fdfd540855e 36ad1e1372aa #启动容器,并运行退出 [root@localhost ~]# docker run -it centos /bin/bash [root@6fa211a6ad77 /]# [root@localhost ~]# #使用快捷键ctrl+p+q,容器运行并退出 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6fa211a6ad77 centos "/bin/bash" 18 seconds ago Up 18 seconds practical_ganguly ``` ### 1.3、dcoker rm #删除镜像 ``` docker rm 容器ID #删除指定容器,不能删除正在运行的容器,强制删除加-f参数 docker rm -f $(docker ps -qa) #删除所有容器 docker ps -a -q|xargs docker rm #删除所有容器 ``` ### 1.4、启动和停止容器 ``` docker start 容器ID #启动容器 docker restart 容器ID #重启容器 docker stop 容器ID #停止容器 docker kill 容器ID #强制停止当前容器 [root@localhost ~]# docker ps #查看正在运行的容器,当下有一个正在运行的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6fa211a6ad77 centos "/bin/bash" 9 minutes ago Up 9 minutes practical_ganguly [root@localhost ~]# docker stop 6fa211a6ad77 #停止正在运行编号为6fa211a6ad77的容器 6fa211a6ad77 [root@localhost ~]# docker ps #查看正在运行的容器,当下为空 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]# docker start 6fa211a6ad77 #启动编号为6fa211a6ad77的容器 6fa211a6ad77 [root@localhost ~]# docker ps #查看正在运行的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6fa211a6ad77 centos "/bin/bash" 10 minutes ago Up 3 seconds practical_ganguly [root@localhost ~]# docker kill 6fa211a6ad77 #杀掉正在运行编号为6fa211a6ad77的容器 6fa211a6ad77 ``` ### 1.5、后台启动容器 ``` # 命令 docker run -d 镜像名 [root@kuangshen /]# docker run -d centos # 问题docker ps,发现 centos 停止了 # 常见的坑:docker 容器使用后台运行,就必须要有要一个前台进程,doker发现没有应用,就会自动停止 # nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了 ``` ### 1.6、查看日志 ``` docker logs -tf --tail {number} 容器ID或名称 # 编写一段脚本测试日志 [root@localhost ~]# docker run -d centos /bin/bash -c "while true;do echo 1111111;sleep 1;done" #一直重复打印10 731862fc784fe25f293a005100d0a9aa5a83531a77dfbec637a6017b127a81be [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 731862fc784f centos "/bin/bash -c 'while…" 5 seconds ago Up 4 seconds angry_clarke #显示日志 -tf 显示所以日志 --tail {number} 显示日志条数 [root@localhost ~]# docker logs -tf --tail 10 731862fc784f #查看容器日志 2023-08-01T07:14:28.894179120Z 1111111 2023-08-01T07:14:29.897299254Z 1111111 2023-08-01T07:14:30.900909443Z 1111111 2023-08-01T07:14:31.907148662Z 1111111 2023-08-01T07:14:32.909287030Z 1111111 2023-08-01T07:14:33.915254585Z 1111111 2023-08-01T07:14:34.919197223Z 1111111 2023-08-01T07:14:35.925241347Z 1111111 2023-08-01T07:14:36.929827146Z 1111111 2023-08-01T07:14:37.935321914Z 1111111 2023-08-01T07:14:38.943338330Z 1111111 2023-08-01T07:14:39.943163111Z 1111111 2023-08-01T07:14:40.945254725Z 1111111 2023-08-01T07:14:41.948768216Z 1111111 2023-08-01T07:14:42.954059901Z 1111111 ``` ### 1.7、查看容器中的进程信息 ``` # docker top 容器ID [root@localhost ~]# docker top 731862fc784f UID PID PPID C STIME TTY TIME CMD root 41246 41227 0 15:14 ? 00:00:00 /bin/bash -c while true;do echo 1111111;sleep 1;done ``` ### 1.8、查看镜像的元素据(所有信息) ``` # docker inspect 容器ID [root@localhost ~]# docker inspect 731862fc784f [ { "Id": "731862fc784fe25f293a005100d0a9aa5a83531a77dfbec637a6017b127a81be", "Created": "2023-08-01T07:14:16.551045014Z", "Path": "/bin/bash", "Args": [ "-c", "while true;do echo 1111111;sleep 1;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 41246, "ExitCode": 0, "Error": "", "StartedAt": "2023-08-01T07:14:16.85962692Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6", "ResolvConfPath": "/var/lib/docker/containers/731862fc784fe25f293a005100d0a9aa5a83531a77dfbec637a6017b127a81be/resolv.conf", "HostnamePath": "/var/lib/docker/containers/731862fc784fe25f293a005100d0a9aa5a83531a77dfbec637a6017b127a81be/hostname", "HostsPath": "/var/lib/docker/containers/731862fc784fe25f293a005100d0a9aa5a83531a77dfbec637a6017b127a81be/hosts", "LogPath": "/var/lib/docker/containers/731862fc784fe25f293a005100d0a9aa5a83531a77dfbec637a6017b127a81be/731862fc784fe25f293a005100d0a9aa5a83531a77dfbec637a6017b127a81be-json.log", "Name": "/angry_clarke", "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, "ConsoleSize": [ 43, 134 ], "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": 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/6c394f1bb1783f0f7ca31a92a58838de80338e2e179b42483b3fdc9a746380bd-init/diff:/var/lib/docker/overlay2/5bdac050f96f04a15bf9ac61ae0b18158b53f84ec511c6f31db06fc226e9a1e8/diff", "MergedDir": "/var/lib/docker/overlay2/6c394f1bb1783f0f7ca31a92a58838de80338e2e179b42483b3fdc9a746380bd/merged", "UpperDir": "/var/lib/docker/overlay2/6c394f1bb1783f0f7ca31a92a58838de80338e2e179b42483b3fdc9a746380bd/diff", "WorkDir": "/var/lib/docker/overlay2/6c394f1bb1783f0f7ca31a92a58838de80338e2e179b42483b3fdc9a746380bd/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "731862fc784f", "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/bash", "-c", "while true;do echo 1111111;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": "aa668f2a7346ecd96309fa3a4f104c03b6207376bc8c6cda8aa3c275dcb4011d", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/aa668f2a7346", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "7fa2c9092a34ffc7563fc96acc662018d16c0da879e4ccb8c14bdcba601bb256", "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": "d1acdd238aad79b8aef4190edc8da5f59590fdeddcd53f84f3e99f4ee2395c6e", "EndpointID": "7fa2c9092a34ffc7563fc96acc662018d16c0da879e4ccb8c14bdcba601bb256", "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 } } } } ] ``` ### 1.9、宿主机与容器之间的文件拷贝 ``` #命令 docker cp 容器ID:容器内路径 目的主机路径 #反之一样 #测试,宿主机拷贝文件至容器 [root@localhost ~]# docker run -d -it centos /bin/bash 3c5874f6363fcb8aa8fcb7a973cd0c71c7568a7d6eba125b9af0df9db3509e88 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3c5874f6363f centos "/bin/bash" 7 seconds ago Up 5 seconds cranky_swartz [root@localhost ~]# echo ceshi > 1.txt [root@localhost ~]# docker cp 1.txt 3c5874f6363f:/root/1.txt Successfully copied 2.05kB to 3c5874f6363f:/root/1.txt [root@localhost ~]# docker exec -it 3c5874f6363f bash [root@3c5874f6363f /]# cat /root/1.txt ceshi [root@3c5874f6363f /]# ``` ## 2、commit提交一个自己的镜像 ``` docker commit 提交容器成为一个新的副本,类似虚拟机的快照 # 命令和git原理类似 docker commit -m "提交秒速信息" -a="作者" 容器ID 目标镜像名,[TAG] ``` ### 2.1、提交一个自己修改过后的tomcat镜像 官方默认的版本是没有webapps的,自己添加文件进去 #### 2.1.1、查询tomcat镜像 ``` [root@localhost ~]# docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 3573 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 109 [OK] bitnami/tomcat Bitnami Tomcat Docker Image 49 [OK] bitnamicharts/tomcat 0 secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK] vulhub/tomcat 0 islandora/tomcat 0 wnprcehr/tomcat 0 hivdb/tomcat-with-nucamino 0 jumpserver/tomcat 0 sismics/tomcat Apache Tomcat Servlet Container 1 eclipse/rdf4j-workbench Dockerfile for Eclipse RDF4J Server and Work… 6 semoss/docker-tomcat Tomcat, Java, Maven, and Git on top of debian 0 [OK] eclipse/hadoop-dev Ubuntu 14.04, Maven 3.3.9, JDK8, Tomcat 8 0 [OK] gbif/ipt The GBIF Integrated Publishing Toolkit (IPT)… 0 dhis2/base-dev Images in this repository contains DHIS2 WAR… 0 eclipse/alpine_jdk8 Based on Alpine 3.3. JDK 1.8, Maven 3.3.9, T… 1 [OK] misolims/miso-base MySQL 5.7 Database and Tomcat 8 Server neede… 0 dhis2/base Images in this repository contains DHIS2 WAR… 0 jelastic/tomcat An image of the Tomcat Java application serv… 4 cfje/tomcat-resource Tomcat Concourse Resource 2 rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl… 7 [OK] amd64/tomcat Apache Tomcat is an open source implementati… 6 arm64v8/tomcat Apache Tomcat is an open source implementati… 8 softwareplant/tomcat Tomcat images for jira-cloud testing 0 [OK] ``` #### 2.1.2、下载Tomcat最新镜像 ``` [root@localhost ~]# docker pull tomcat Using default tag: latest latest: Pulling from library/tomcat 0e29546d541c: Pull complete 9b829c73b52b: Pull complete cb5b7ae36172: Pull complete 6494e4811622: Pull complete 668f6fcc5fa5: Pull complete dc120c3e0290: Pull complete 8f7c0eebb7b1: Pull complete 77b694f83996: Pull complete 0f611256ec3a: Pull complete 4f25def12f23: Pull complete Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324 Status: Downloaded newer image for tomcat:latest docker.io/library/tomcat:latest ``` #### 2.1.3、创建一个容器并启动 ``` root@localhost ~]# docker run --name tomcat01 -d -p 80:8080 tomcat 0e031df6bcf990257475f0403eff57ca59fb6d175b5d1c229a5869e2f38197e2 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0e031df6bcf9 tomcat "catalina.sh run" 6 seconds ago Up 5 seconds 0.0.0.0:80->8080/tcp, :::80->8080/tcp tomcat01 e6509e47ca82 portainer/portainer "/portainer" 24 hours ago Up 5 hours 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp sweet_wozniak [root@localhost ~]# ``` #### 2.1.4、测试,测试机访问地址:http://ip:80 ![](/media/202309/image-20230808155636964.png) 出现问题404,因为官方默认的Tomcat做了精简,webapps下没有文件,我们将webapps.dist下文件拷贝致webapps下 ``` [root@localhost ~]# docker exec -it tomcat01 bash root@0e031df6bcf9:/usr/local/tomcat# cp -r webapps.dist/* webapps ``` 再次测试,测试结果正常,tomcat版本为10.0.14 <img src="images/image-20230808155859905.png" alt="image-20230808155859905" style="zoom:50%;" /> #### 2.1.5、将修改后的容器通过commit提交,方便下次自己使用 ``` [root@localhost ~]# docker commit -a="zxw" -m="add webapps app" 0e031df6bcf9 tomcat_01:1.0 sha256:452d84ab3a010a1b17f5f39c575aa5cabcd6e384d4d421524ac2be0dedd46e7b [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat_01 1.0 452d84ab3a01 11 seconds ago 684MB #自己提交的镜像 nginx 1.25.1 bcf638382021 8 days ago 1.16GB tomcat latest fb5657adc892 19 months ago 680MB hello-world latest feb5d9fea6a5 22 months ago 13.3kB centos latest 5d0da3dc9764 23 months ago 231MB portainer/portainer latest 580c0e4e98b0 2 years ago 79.1MB [root@localhost ~]# ``` ------ ### 2.2.提交一个安装好nginx+php的镜像 > 本次采用rpm二进制包安装,nginx版本为1.24.0,php版本为8.0 #### 2.2.1.启动一个基础的centos7镜像 ```shell [root@localhost ~]# docker run -itd --name nginx_php --privileged=true centos:centos7.9.2009 /usr/sbin/init ``` #默认容器是普通用户权限,使用--privileged=true 将容器运行权限调整为特权,方便后续一些命令的执行 #### 2.2.2.将宿主机提前准保好的yum文件复制到容器中 ```shell [root@localhost ~]# docker cp Centos-7.repo nginx_php:/etc/yum.repos.d/. Successfully copied 4.1kB to nginx_php:/etc/yum.repos.d/. ``` #### 2.2.3.进入容器安装相关工具,配置清华站点php的yum源,下载nginx二进制包 ```shell [root@cf4a8afce6a4 /]# rm -rf /etc/yum.repos.d/CentOS-* [root@cf4a8afce6a4 /]# yum install wget vim -y [root@cf4a8afce6a4 /]# yum -y install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm [root@cf4a8afce6a4 /]# yum -y install yum-utils [root@cf4a8afce6a4 /]# wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.24.0-1.el7.ngx.x86_64.rpm ``` #### 2.2.4.查看可使用的php版本yum信息 ```shell [root@cf4a8afce6a4 /]# ll /etc/yum.repos.d total 60 -rw-r--r--. 1 root root 2523 Aug 23 02:30 Centos-7.repo -rw-r--r--. 1 root root 1050 Oct 2 2017 epel-testing.repo -rw-r--r--. 1 root root 951 Oct 2 2017 epel.repo -rw-r--r--. 1 root root 855 Jan 5 2023 remi-modular.repo -rw-r--r--. 1 root root 456 Jan 5 2023 remi-php54.repo -rw-r--r--. 1 root root 1314 Jan 5 2023 remi-php70.repo -rw-r--r--. 1 root root 1314 Jan 5 2023 remi-php71.repo -rw-r--r--. 1 root root 1314 Jan 5 2023 remi-php72.repo -rw-r--r--. 1 root root 1314 Jan 5 2023 remi-php73.repo -rw-r--r--. 1 root root 1314 Jan 5 2023 remi-php74.repo -rw-r--r--. 1 root root 1314 Jan 5 2023 remi-php80.repo -rw-r--r--. 1 root root 1314 Jan 5 2023 remi-php81.repo -rw-r--r--. 1 root root 1314 Jan 5 2023 remi-php82.repo -rw-r--r--. 1 root root 750 Jan 5 2023 remi-safe.repo -rw-r--r--. 1 root root 2605 Jan 5 2023 remi.repo ``` #### 2.2.5.安装nginx和php ```shell #安装nginx [root@cf4a8afce6a4 /]# yum install nginx-1.24.0-1.el7.ngx.x86_64.rpm -y #安装php及相关插件 [root@cf4a8afce6a4 /]# yum -y install php80-php-cli php80-php-common php80-php-devel php80-php-embedded php80-php-fpm php80-php-gd php80-php-mbstring php80-php-mysqlnd php80-php-pdo php80-php-opcache php80-php-xml php80-php-soap ``` #### 2.2.6.给php配置一个软连接,配置开机自启动 ```shell [root@f7118a887bfe /]# ln -sf /opt/remi/php80/root/usr/bin/php* /usr/local/bin [root@f7118a887bfe /]# php -v PHP 8.0.30 (cli) (built: Aug 3 2023 17:13:08) ( NTS gcc x86_64 ) Copyright (c) The PHP Group Zend Engine v4.0.30, Copyright (c) Zend Technologies with Zend OPcache v8.0.30, Copyright (c), by Zend Technologies [root@f7118a887bfe /]# systemctl enable php80-php-fpm Created symlink from /etc/systemd/system/multi-user.target.wants/php80-php-fpm.service to /usr/lib/systemd/system/php80-php-fpm.service. [root@f7118a887bfe /]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. ``` #### 2.2.7.配置nginx可解析php ```shell #配置nginx可以解析php [root@f7118a887bfe /]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; #修改之一,添加php首页文件 } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # 修改之二,以下是php配置信息修改 location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } #检查nginx语法错误 [root@f7118a887bfe /]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful ``` #### 2.2.8.启动nginx和php服务并测试 ```shell [root@f7118a887bfe /]# systemctl start nginx [root@f7118a887bfe /]# systemctl start php80-php-fpm ``` #编写一个php文件,测试 ```shell [root@f7118a887bfe /]# vim /usr/share/nginx/html/phpinfo.php <?php Phpinfo(); ?> ``` #本地测试 ```shell [root@f7118a887bfe /]# curl http://127.0.0.1/phpinfo.php ``` ![](/media/202309/image-20230823150511589.png) #### 2.2.9.将配置好的容器commit一个新镜像 ```shell [root@localhost ~]# docker commit -m="add nginx.1.24.0 php8.0" -a="zxw" f7118a887bfe nginx/php8:1.0 sha256:19da84ca53522f0f89528fa4956dae2ac29af5e75307f8362bdb7e0dd8d8deb6 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx/php8 1.0 19da84ca5352 14 seconds ago 753MB ``` #### 2.2.10.使用自己修改过的镜像启动容器,并测试 ```shell [root@localhost ~]# docker run -itd --name mynp -p 80:80 --privileged=true nginx/php8:1.0 /usr/sbin/init 43f33e493f3dffb288a360df72e24418d60f406509fe948f5346142b3171ee06 ``` ![](/media/202309/image-20230823151335480.png)
admin
Sept. 19, 2023, 4:50 p.m.
转发文档
Collection documents
Last
Next
手机扫码
Copy link
手机扫一扫转发分享
Copy link
IT集中营导航
IT集中营导航
Markdown文件
share
link
type
password
Update password