Verified guide
This guide was verified by creating a cloud server and running every command in it, from start to finish, exactly as written.
- OS
- Ubuntu 26.04.1 LTS · x86_64
- Installed version
- Docker 29.9.2, Docker Compose 5.6.1, nginx:1.30 image (Nginx 1.30.6)
- Test server
- DigitalOcean · 1 vCPU / 2 GB RAM / 50 GB SSD (s-1vcpu-2gb, sgp1)
- Last checked
- 2026-10-27
This guide contains affiliate links. If you sign up through them, Codesand may earn a commission. This doesn't affect the content or our assessments.
This guide runs the official Nginx 1.30 image with Docker Compose on an Ubuntu 26.04 server. It covers writing compose.yaml, mounting the page and configuration folders, checking logs, making Nginx reload after configuration changes, the restart policy, common errors, and how to uninstall.
1. Requirements
- One Ubuntu 26.04 LTS server with Docker and Docker Compose installed (see How to Install Docker on Ubuntu 26.04)
- A regular user account that can run
dockercommands withoutsudo(member of thedockergroup) - Nginx must not be installed directly on the server (both cannot use port 80 at the same time)
If you don't have a server, try creating a DigitalOcean Droplet (1 vCPU, 2 GB). Every step in this guide was verified on a Droplet (1 vCPU, 2 GB memory, 50 GB SSD, Ubuntu 26.04, Singapore) with Docker 29.9.2 and Docker Compose 5.6.1 installed from the official Docker repository, using a regular user account. External access was tested from another PC (example IP 203.0.113.10) to the server (example IP 203.0.113.20).
docker --versionDocker version 29.9.2, build a82e528docker compose versionDocker Compose version v5.6.12. Create the project folder and files
Inside the project folder, create an html folder for web pages and a conf.d folder for the Nginx configuration. Both folders are mounted to folders inside the container. Even if you delete and recreate the container, the files in these two folders remain.
mkdir -p ~/nginx-compose/html ~/nginx-compose/conf.d && cd ~/nginx-compose && pwd/home/devuser/nginx-composeCreate the first page. The command prints nothing.
echo '<h1>Hello from Docker Nginx</h1>' > ~/nginx-compose/html/index.htmlCreate the Nginx site configuration. Inside the container, Nginx reads the .conf files in the /etc/nginx/conf.d folder, so this file replaces the default configuration (default.conf) included in the image. The command prints nothing.
cat > ~/nginx-compose/conf.d/default.conf <<'EOF'
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
EOFCreate compose.yaml. The command prints nothing.
cat > ~/nginx-compose/compose.yaml <<'EOF'
services:
nginx:
image: nginx:1.30
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html:ro
- ./conf.d:/etc/nginx/conf.d:ro
restart: unless-stopped
EOFimage: nginx:1.30: Uses the 1.30 line (stable version) of the official Nginx image on Docker Hub. If you don't specify a version, as innginx:latest, the version may change every time you pull the image.ports: "80:80": Forwards requests that arrive on port 80 of the server to port 80 of the container.volumes: The left side is the folder on the server, and the right side is the folder inside the container.:romeans the container can only read the folder and cannot change it.restart: unless-stopped: Starts the container again if it stops or the server reboots, except when you stopped it yourself (docker compose stop). This is checked in step 6.
cd ~/nginx-compose && find . -type f | sort./compose.yaml
./conf.d/default.conf
./html/index.html3. Start the container
Run this in the folder that contains compose.yaml. -d runs the container in the background and returns the terminal immediately. On the first run, lines for downloading the nginx:1.30 image (Pulling, Pulled) appear before the result below.
docker compose up -d[+] up 2/2
✔ Network nginx-compose_default Created 0.1s
✔ Container nginx-compose-nginx-1 Started 0.5sIf STATUS is Up and PORTS shows 0.0.0.0:80->80/tcp, it is running normally.
docker compose psNAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
nginx-compose-nginx-1 nginx:1.30 "/docker-entrypoint.…" nginx 2 seconds ago Up 1 second 0.0.0.0:80->80/tcp, [::]:80->80/tcpcurl -i http://localhostHTTP/1.1 200 OK
Server: nginx/1.30.6
Date: Tue, 27 Oct 2026 08:45:58 GMT
Content-Type: text/html
Content-Length: 33
Last-Modified: Tue, 27 Oct 2026 08:45:43 GMT
Connection: keep-alive
ETag: "6ae064b7-21"
Accept-Ranges: bytes
<h1>Hello from Docker Nginx</h1>You can also check the Nginx version inside the container. docker compose exec runs a command inside a running container.
docker compose exec nginx nginx -vnginx version: nginx/1.30.6Connect from the PC (203.0.113.10) as well.
curl http://203.0.113.20<h1>Hello from Docker Nginx</h1>sudo ufw statusStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)4. Check the logs
The official Nginx image sends access and error logs to the container output instead of files, so you view them with docker compose logs. The first part is the record from when the container started, and the last two lines are the requests sent from inside the server and from the PC.
docker compose logs nginxnginx-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx-1 | 10-listen-on-ipv6-by-default.sh: info: can not modify /etc/nginx/conf.d/default.conf (read-only file system?)
nginx-1 | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx-1 | 2026/10/27 08:45:56 [notice] 1#1: using the "epoll" event method
nginx-1 | 2026/10/27 08:45:56 [notice] 1#1: nginx/1.30.6
nginx-1 | 2026/10/27 08:45:56 [notice] 1#1: built by gcc 14.2.0 (Debian 14.2.0-19)
nginx-1 | 2026/10/27 08:45:56 [notice] 1#1: OS: Linux 7.0.0-31-generic
nginx-1 | 2026/10/27 08:45:56 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1024:524288
nginx-1 | 2026/10/27 08:45:56 [notice] 1#1: start worker processes
nginx-1 | 2026/10/27 08:45:56 [notice] 1#1: start worker process 21
nginx-1 | 172.18.0.1 - - [27/Oct/2026:08:45:58 +0000] "GET / HTTP/1.1" 200 33 "-" "curl/8.18.0" "-"
nginx-1 | 203.0.113.10 - - [27/Oct/2026:08:46:05 +0000] "GET / HTTP/1.1" 200 33 "-" "curl/8.7.1" "-"can not modify … (read-only file system?) on the fourth line is not an error. When the image starts, it tries to add IPv6 settings to the default configuration, and this message says that step was skipped because the configuration folder is mounted with :ro. Requests sent from inside the server are recorded with a Docker internal address (172.18.0.1).
To see only the last few lines, use --tail. If you add -f (docker compose logs -f nginx), new log lines keep appearing as they are written; press Ctrl+C to stop.
docker compose logs --tail 2 nginxnginx-1 | 172.18.0.1 - - [27/Oct/2026:08:45:58 +0000] "GET / HTTP/1.1" 200 33 "-" "curl/8.18.0" "-"
nginx-1 | 203.0.113.10 - - [27/Oct/2026:08:46:05 +0000] "GET / HTTP/1.1" 200 33 "-" "curl/8.7.1" "-"5. Change the configuration and reload it
Changes to page files in the html folder take effect immediately. Configuration files take effect only after Nginx reads them again. Let's add an address for status checks (/health). The command prints nothing.
cat > ~/nginx-compose/conf.d/default.conf <<'EOF'
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location = /health {
default_type text/plain;
return 200 "ok\n";
}
}
EOFFirst, use the Nginx inside the container to check the configuration for errors.
docker compose exec nginx nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulMake Nginx reload only the configuration without restarting the container. Requests in progress are not dropped.
docker compose exec nginx nginx -s reload2026/10/27 08:46:21 [notice] 35#35: signal process startedcurl http://localhost/healthok6. Check the restart policy
Check the restart policy applied to the container.
docker inspect -f "{{.HostConfig.RestartPolicy.Name}}" nginx-compose-nginx-1unless-stoppedReboot the server, then connect again to check. uptime -p shows how long the server has been running. The server has been up for less than a minute, and the container is already running again.
sudo systemctl rebootuptime -pup 0 minutescd ~/nginx-compose && docker compose psNAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
nginx-compose-nginx-1 nginx:1.30 "/docker-entrypoint.…" nginx 48 seconds ago Up 1 second 0.0.0.0:80->80/tcp, [::]:80->80/tcpcurl http://203.0.113.20/healthokA container you stopped yourself with docker compose stop does not start again after a reboot.
docker compose stop Container nginx-compose-nginx-1 Stopping
Container nginx-compose-nginx-1 StoppedThis is the result checked after rebooting in this state. -a also shows stopped containers.
docker compose ps -aNAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
nginx-compose-nginx-1 nginx:1.30 "/docker-entrypoint.…" nginx About a minute ago Exited (0) 42 seconds agoTo start it again, run docker compose up -d.
7. Troubleshooting
nginx: [emerg] invalid number of arguments in "default_type" directive
This means there is a syntax error in a configuration file. The following is the result after removing the semicolon at the end of the default_type text/plain; line. default.conf:14 is the file and line number with the error.
docker compose exec nginx nginx -t2026/10/27 08:46:30 [emerg] 42#42: invalid number of arguments in "default_type" directive in /etc/nginx/conf.d/default.conf:14
nginx: [emerg] invalid number of arguments in "default_type" directive in /etc/nginx/conf.d/default.conf:14
nginx: configuration file /etc/nginx/nginx.conf test failednginx -s reload fails, and Nginx keeps running with the previous configuration. However, if you run docker compose restart in this state, Nginx cannot start, and the container keeps restarting (Restarting).
docker compose restart nginx Container nginx-compose-nginx-1 Restarting
Container nginx-compose-nginx-1 StartedIt shows Started, but a few seconds later the status is Restarting.
docker compose psNAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
nginx-compose-nginx-1 nginx:1.30 "/docker-entrypoint.…" nginx 45 seconds ago Restarting (1) 1 second agoDuring this time the site cannot be reached, and the last lines of the log show the cause.
docker compose logs --tail 3 nginxnginx-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx-1 | 2026/10/27 08:46:40 [emerg] 1#1: invalid number of arguments in "default_type" directive in /etc/nginx/conf.d/default.conf:14
nginx-1 | nginx: [emerg] invalid number of arguments in "default_type" directive in /etc/nginx/conf.d/default.conf:14Fix the line shown, then run docker compose restart nginx. When you change the configuration, checking with nginx -t and using nginx -s reload as in step 5, rather than restart, prevents this kind of outage.
Error response from daemon: Container … is restarting, wait until the container is running
This means the container keeps restarting, so you cannot use docker compose exec.
docker compose exec nginx nginx -tError response from daemon: Container c82c8472b36530ebcbdd1c857b43d7c29a56038fa7b513a7f6cd34f32a914afa is restarting, wait until the container is runningAfter fixing the configuration file, you can create a temporary container with docker compose run to check the configuration. --rm deletes the temporary container when the check finishes, and --no-deps does not start other services along with it. The startup record at the beginning is omitted.
docker compose run --rm --no-deps nginx nginx -t…
/docker-entrypoint.sh: Configuration complete; ready for start up
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulWhen the check passes, restart with docker compose restart nginx.
docker compose restart nginx Container nginx-compose-nginx-1 Restarting
Container nginx-compose-nginx-1 Starteddocker compose psNAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
nginx-compose-nginx-1 nginx:1.30 "/docker-entrypoint.…" nginx About a minute ago Up 3 seconds 0.0.0.0:80->80/tcp, [::]:80->80/tcpfailed to bind host port 0.0.0.0:80/tcp: address already in use
This means another program is using port 80 on the server. The following is the result of running it while another program (a Python web server) is using port 80.
docker compose up -dError response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint nginx-compose-nginx-1 (0ccae67bcbf725b9d64756697d4bc6273e741d20df19fa92e7df2a8c7685f9c0): failed to bind host port 0.0.0.0:80/tcp: address already in useFind the program using port 80 with sudo ss -tlnp | grep ":80 " and stop it, or change ports in compose.yaml to another port, such as "8080:80".
After freeing the port, be sure to run docker compose up -d again. On this server, running up -d once more right after the failure started the container without the port mapping. STATUS is Up, but the PORTS column is empty and the site cannot be reached.
docker compose psNAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
nginx-compose-nginx-1 nginx:1.30 "/docker-entrypoint.…" nginx 19 seconds ago Up 16 secondsIf you run docker compose up -d with the port free, the container is recreated (Recreate) and the port is mapped.
docker compose up -d Container nginx-compose-nginx-1 Recreate
Container nginx-compose-nginx-1 Recreated
Container nginx-compose-nginx-1 Starting
Container nginx-compose-nginx-1 Starteddocker compose psNAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
nginx-compose-nginx-1 nginx:1.30 "/docker-entrypoint.…" nginx 1 second ago Up Less than a second 0.0.0.0:80->80/tcp, [::]:80->80/tcp8. Uninstall
Remove the container and the network that Compose created. The files in the html and conf.d folders are on the server, so they are not deleted.
cd ~/nginx-compose
docker compose down Container nginx-compose-nginx-1 Stopping
Container nginx-compose-nginx-1 Stopped
Container nginx-compose-nginx-1 Removing
Container nginx-compose-nginx-1 Removed
Network nginx-compose_default Removing
Network nginx-compose_default RemovedRemove the downloaded Nginx image.
docker image rm nginx:1.30Untagged: nginx:1.30
Deleted: sha256:a7675fcee220f59b4a99f7098c06a0d5d999c2c0ee37c0211efb780536e5cb20Delete the project folder. The commands print nothing.
cd ~
rm -rf ~/nginx-composeCheck that no containers or images remain.
docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdocker imagesIMAGE ID DISK USAGE CONTENT SIZE EXTRATry it yourself
No server yet? You can spin up an Ubuntu server in a few minutes with one of these providers. We verified this guide on DigitalOcean.
Was this guide helpful? Let us know if anything is wrong or out of date.
Request a correction