Docker Compose部署Nginx的方法步骤
利用Docker Compose来部署Nginx,首先需要创建一个名为docker-compose.yaml的配置文件:
[root@192 ~]# vim docker-compose.yaml
在该文件中填入如下配置内容:
version: '3'
services:
nginx:
container_name: nginx
image: nginx
restart: always
ports:
- 8080:80
privileged: true
networks:
- mynginx
networks:
mynginx:
driver: bridge
依据docker-compose.yaml文件的内容,执行命令以创建并启动相应的容器实例:
[root@192 ~]# docker compose up
[+] Running 7/7
⠿ nginx Pulled 20.5s
⠿ 5eb5b503b376 Pull complete 12.6s
⠿ 1ae07ab881bd Pull complete 14.8s
⠿ 78091884b7be Pull complete 14.9s
⠿ 091c283c6a66 Pull complete 15.0s
⠿ 55de5851019b Pull complete 15.1s
⠿ b559bad762be Pull complete 15.2s
[+] Running 2/2
⠿ Network root_mynginx Created 0.3s
⠿ Container nginx Created 1.8s
Attaching to nginx
nginx | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx | 2026/02/13 03:43:39 [notice] 1#1: using the "epoll" event method
nginx | 2026/02/13 03:43:39 [notice] 1#1: nginx/1.21.6
nginx | 2026/02/13 03:43:39 [notice] 1#1: built by gcc 10.2.1 20260110 (Debian 10.2.1-6)
nginx | 2026/02/13 03:43:39 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64
nginx | 2026/02/13 03:43:39 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx | 2026/02/13 03:43:39 [notice] 1#1: start worker processes
nginx | 2026/02/13 03:43:39 [notice] 1#1: start worker process 31
为了确保外部能够顺利访问,需要执行以下操作来关闭系统防火墙:
[root@192 ~]# systemctl stop firewalld
如果在本地浏览器中能够正常访问页面,则表明Nginx已经成功完成部署。

此外,通过Docker命令行界面,也可以查询到由Docker Compose所拉取的镜像资源以及当前创建的容器信息。
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c316d5a335a5 2 weeks ago 142MB
lazyteam/lazydocker latest 3b572700e20d 10 months ago 69.9MB
portainer/portainer latest 580c0e4e98b0 11 months ago 79.1MB
openjdk 8-jdk-alpine a3562aa0b991 2 years ago 105MB
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e31945577104 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx
若希望将服务置于后台运行,可使用up命令(注意:此操作前需先开启防火墙,否则可能会引发报错):
[root@192 ~]# systemctl start firewalld
[root@192 ~]# docker compose up -d
[+] Running 1/1
⠿ Container nginx Started
当需要暂停服务运行时,可执行以下指令:
[root@192 ~]# docker compose stop nginx
[+] Running 1/1
⠿ Container nginx Stopped
若需重新启动已停止的服务,请使用以下命令:
[root@192 ~]# docker compose start nginx
[+] Running 1/1
⠿ Container nginx Started
至此,关于利用Docker Compose进行Nginx部署的详细步骤已介绍完毕。如需了解更多相关技术内容,欢迎查阅本站往期文章或继续浏览后续推荐内容,感谢大家的支持!