Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
VGSources
Blog

How to Run Nginx in a Docker Container on Ubuntu

A practical Ubuntu guide to running NGINX in Docker, from Docker Engine verification and port publishing to custom website mounts, nginx.conf, persistence, image tags, and network troubleshooting.
Length9 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The shortest reliable path is to install Docker Engine, verify that its service is running, then start the official NGINX image with a host-to-container port mapping:

docker run --name mynginx -p 8080:80 -d nginx

Browse to http://<ubuntu-host>:8080. Port 8080 belongs to Ubuntu; port 80 is NGINX’s port inside the container. You do not need a separate NGINX installation on the Ubuntu host.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What you need before starting

  • An Ubuntu system with administrative access through sudo.
  • Docker Engine installed from Docker’s official Ubuntu apt repository.
  • The Docker service running before you execute docker run.
  • Network access to the Ubuntu machine if you plan to open the site from another computer.

The documented Docker package set is docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin, and docker-compose-plugin. Configure Docker’s official Ubuntu repository using its current installation instructions, then install that set with:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Check the daemon and start it if necessary:

sudo systemctl status docker
sudo systemctl start docker

Verify the installation independently of NGINX:

sudo docker run hello-world

If hello-world completes successfully, Docker can pull and start images. If the command reports that it cannot connect to the Docker daemon, return to systemctl status docker and inspect the service before troubleshooting NGINX.

#1 Best Overall
Sale
Gaoye Blue Light Blocking Glasses
  • BLUE LIGHT BLOCKING GLASSES: Our blue light glasses for women boast UV400 protection, blocking harmful blue rays from computer screens. With these bluelight glasses women, you can enjoy gaming, reading, or watching TV with no worries of eye strain.
  • EYE RELIEF: Our blue light glasses are engineered to reduce blurry vision or strain caused by prolonged screen time. These womens blue light glasses aid in better sleep by preventing blue light from disrupting your natural sleep cycle, helping you fall asleep faster and enjoy a more restful night's sleep, making them the perfect christmas present.
  • COMFORTABLE: Crafted from durable material, our trendy blue light glasses for woman or blue light glasses men ensure long-lasting comfort. The high-transmittance lenses accessories, gamer gifts for men, help keep your eyes relaxed. Perfect stocking stuffers, gifts for gamers men, christmas gifts for men, halloween costumes.
  • LIGHTWEIGHT: These clear blue light glasses feature a lightweight frame, meaning they’ll stay comfy for long-term wear. Perfect as blue blockers glasses for women or mens blue light glasses, these non-prescription fake glasses for men can be worn throughout the day. The blue light blocking glasses women feature polycarbonate HD lenses, which are anti-reflective and restore true color.
  • WORRY-FREE PURCHASE: If you experience any issues, our support team is ready to assist you quickly. These are the perfect costume glasses for cosplay dressing up.

Start the official NGINX container

Use a host port of 8080

Run the image in detached mode:

docker run --name mynginx -p 8080:80 -d nginx

Each option has a specific job:

  • --name mynginx gives the container a stable name for later commands.
  • -p 8080:80 publishes Ubuntu’s port 8080 and forwards it to port 80 inside the container.
  • -d leaves the container running in the background.
  • nginx is the official image name; Docker pulls it automatically if it is not already present.

Confirm that it is running:

docker ps

The listing should show mynginx with a port mapping similar to 0.0.0.0:8080->80/tcp. On the Ubuntu machine itself, open http://localhost:8080. From another device, replace <ubuntu-host> with the machine’s reachable IP address or DNS name:

http://<ubuntu-host>:8080

The default NGINX welcome page confirms that the image started and that the published port reaches the web server.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use port 80 instead

NGINX listens on port 80 inside the image, so you can publish the conventional HTTP port directly:

docker run --name mynginx1 -p 80:80 -d nginx

With this mapping, browse to http://<ubuntu-host> without appending a port. Only one process can occupy a given host port, so remove or stop another service that already uses port 80 before starting this container.

Choose the exposure scope deliberately

Docker’s -p option controls where Ubuntu accepts connections as well as which container port receives them.

Command Reachability Use when
-p 8080:80 Publishes on all host interfaces by default. The site must be reachable from the network.
-p 80:80 Publishes HTTP on all host interfaces by default. You want the standard HTTP URL and have secured the host appropriately.
-p 127.0.0.1:8080:80 Accepts connections only from the Ubuntu host itself. A local reverse proxy, test browser, or development workflow will access NGINX.

An unqualified mapping such as -p 80:80 is not limited to localhost. Treat it as a network exposure decision, not merely a port-number choice.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Ubuntu firewall implications

Docker’s Ubuntu installation guidance warns: “If you use ufw or firewalld to manage firewall settings, be aware that when you expose container ports using Docker, these ports bypass your firewall rules.” Review Docker’s supported iptables paths and your host’s exposure policy before publishing NGINX publicly. A loopback binding is the safer default for a service that should not accept external traffic.

Rank #2
Windex Gaming Cleaning Wipes, Pre-Moistened Wipes to Instantly Clean all Gaming Gear and Electronics, Resealable Packaging for a Freshness Lock, 25 count (Pack of 1)
  • WINDEX GAMING WIPES: effortlessly clean all your gaming gear and gaming electronics in one easy step with our new Windex Gaming Wipes
  • AMMONIA FREE: our ammonia-free formula is safe to use on all of your favorite gaming equipment like consoles, headsets, controllers, screens, keyboards, and more
  • PRE-MOISTENED WIPES: each wipe instantly removes dust, dirt, and stubborn smudges and fingerprints leaving a streak-free shine
  • ANTI-STATIC: great for cleaning and wiping down your gaming gear and electronics leaving behind no visible residue
  • COMPACT RESEALABLE PACKAGE: store your pack of Windex Gaming Wipes anywhere in your home for whenever a quick and easy wipedown is needed

Inspect, test, stop, and remove the container

Check state and logs

List only running containers:

docker ps

Include stopped containers when a start attempt failed:

docker ps -a

Read NGINX’s output and startup errors:

docker logs mynginx

A container that exits immediately is no longer shown by docker ps; docker ps -a and docker logs mynginx reveal why.

Stop and delete it

docker stop mynginx
docker rm mynginx

Stopping preserves the container definition, while removing it deletes that container. Files stored only in the writable container layer should not be treated as durable data; use a bind mount or named volume for anything that must survive replacement.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Resolve a name collision

Container names must be unique. If Docker says that /mynginx is already in use, either reuse the existing container:

docker start mynginx

or stop and remove it before creating a replacement:

docker stop mynginx
docker rm mynginx

Serve your own website files

The official image contains a default document root. Bind-mount your host directory onto that path to replace the default page with your files:

mkdir -p html
printf '%sn' '<h1>My Ubuntu site</h1>' > html/index.html
docker run --name mynginx -p 8080:80 -v "$PWD/html:/usr/share/nginx/html:ro" -d nginx

The left side of -v is the Ubuntu path; the right side is the path inside the container. The :ro suffix makes the mount read-only from NGINX’s perspective, which is appropriate for static content. Replace $PWD/html with an absolute host path when running the command from another directory.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
LISEN Cell Phone Stand Phone Holder for Desk, Office Decor Gadgets for Men
  • Ergonomic & Adjustable Design: Fight "text neck" and improve your posture. Designed for comfort, it offers an adjustable height (7.1in-8.5in) (1.4in adjustable range) and angle (5°-85°) Please note: cell phone stand is only 9 inches tall—the length of a standard pencil
  • Without blocking subtitles Perfect for office desk Tiktok Face Time Zoom video—this is the ultimate kitchen desk essential for phones! Perfect viewing experience while reducing neck back strain, A must-have for gamers streamers desk, and anyone who values both style and support!
  • Sleek & Stable Design fits Otterbox Case: Say goodbye to bent cables, tangles, and tip-overs! LISEN’s stylish adjustable desk phone holder features a built-in charging hole and smart cable management, keeping your phone secure and your workspace clutter-free cool gadgets for men
  • ANTI-SLIP DESIGN free hands: The under 10.00 items easy gadgets back and the bottom of this kitchen essentials for women are fully covered by anti-skid silicone, which LISEN stand up desk decor can provide maximum protection device from any scratches and slides
  • Your Perfect Viewing Companion:If your product arrives damaged or breaks within 365 days, reach out to LISEN for a quick and hassle-free solution. Struggling with assembly, like tightening the screw? We’ve got your back! Your experience matters to us. (Product including: phone Holder stand*1, screw*1, hexagon screwdriver*1, specification*1)

If a container named mynginx already exists, remove it first or choose another name. A bind mount hides the image’s original contents at that path, so make sure the host directory contains every file your site needs.

Verify the mount

  1. Run docker ps and confirm that the container is up.
  2. Open http://localhost:8080 or the host’s network address.
  3. If the old welcome page remains, confirm the host path, container name, and mount syntax. Recreate the container after changing its mounts; mounts are set when the container is created.

Mount a custom nginx.conf

For custom server blocks, prepare and test an NGINX configuration on the host, then mount it read-only at the image’s main configuration path:

docker run --name mynginx -p 8080:80 
  -v "$PWD/html:/usr/share/nginx/html:ro" 
  -v "$PWD/nginx.conf:/etc/nginx/nginx.conf:ro" 
  -d nginx

Adapt the host paths and confirm the configuration path for the image tag you selected. Mounting a file read-only protects it from edits inside the container, but NGINX may write to other locations. The official image documentation notes that, when using read-only mounts, you must also mount every location where NGINX writes. A configuration that expects writable cache, temporary, PID, or log paths can therefore fail even though nginx.conf itself is readable.

Recover from a configuration error

  1. Inspect the startup output with docker logs mynginx.
  2. Check that the host file exists and is readable by Docker.
  3. Validate syntax in a disposable container using the same configuration mount and an NGINX test command before recreating the production container.
  4. Correct the host file, remove the failed container, and run the command again. A file mount is not changed on an existing container.

Keep the configuration that produced a working container under version control or another backed-up host location; the container itself is disposable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep data beyond a container’s lifetime

Use a bind mount when you need files to remain directly visible at a particular Ubuntu path, such as a checked-out website. Use a named volume when Docker should manage the storage location and lifecycle.

Storage choice Best fit Important behavior
Bind mount Site source, hand-edited configuration, or files managed with normal Ubuntu tools. Uses an explicit host path; incorrect paths can result in an empty or missing document root.
Named volume Data that must outlive replacement without tying the setup to a particular directory. Managed by Docker and removed separately with docker volume rm.
Container writable layer Temporary runtime changes only. Do not rely on it for data you need after deleting the container.

Create and inspect a named volume with:

docker volume create nginx-data
docker volume ls

Attach it to an appropriate path when you create the container:

docker run --name mynginx -p 8080:80 -v nginx-data:/some/container/path -d nginx

The correct container path depends on what you are persisting; do not attach a volume over a directory whose image-provided files you still need unless you have populated the volume deliberately. Remove an unused volume only after confirming its contents are no longer required:

Rank #4
Ledagic IC+RGBW USB Powered Led Light Bar with App & Remote Control, 2 Pack
  • Easy Installation: Our gaming lights have been designed to enhance your entertainment experience. Simply position the led light bar next to your TV or game console to create a game room environment with ease. They come with USB power and a 60 inch cord for direct mounting and mobility
  • Audio Syncing & Extensive Mode: The built-in high-sensitivity music sensor allows you to sync the TV led lights to your favorite music. Lights react and move to the beat of music, film and game audio, creating an incredible immersive sound and light experience – and all without the need for an additional hub. You can choose from 64+ patterns, including strobe, illusion, jump, energy, gradient, running, chasing, fluttering mode etc. Each mode can be applied to different scenes
  • 64+ Chasing IC+RGB & W Effect: The independent control design of IC+RGB and white all-in-one multifunctional rgbw led bar lights monitor backlight allows you to easily switch the led lights for desk between IC+RGB and white to enjoy a variety of lighting effects, you can choose white lighting mode with high brightness for better illumination when working, etc. Choose IC+RGB lighting mode when you need to create an entertaining atmosphere for the light show, 16 million colours of Color changing rgb light bar let you dance to the beat and create a dynamic party atmosphere
  • Dynamic and Dimmable Lights: 1%-full brightness free brightness adjustment gives you the flexibility to adjust dimmable led light bar for room decorations or a variety of occasions
  • USB Powered: USB light bars for room can be powered by powernank, usb charger, TV, Monitor, laptop PC mainframe, making behind TV led lighting bar easy to install in any location.TV led lights are powered by the TV usb interface / PC mainframe, can be turned on/off synchronously with TV / PC
docker volume rm nginx-data

Select an image tag and process model

At the time of the documented image listing, available tags included 1.31.6, mainline, 1, 1.31, and latest. A deliberate tag policy matters:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Tag policy Trade-off Practical choice
latest Convenient, but mutable; a later pull can change the image behind the same command. Short-lived experiments where automatic updates are acceptable.
Major or major/minor tag, such as 1 or 1.31 Constrains the update range but still permits newer images within that range. Environments that want controlled updates without pinning one patch.
Patch tag, such as 1.31.6 Most reproducible among the listed choices; updates require an intentional tag change. Deployments where repeatability and change review matter.

Use the selected tag consistently in every command instead of relying on an implicit default, for example:

docker run --name mynginx -p 8080:80 -d nginx:1.31.6

The container’s main process must stay in the foreground so Docker can supervise it. The documented NGINX container pattern is:

CMD ["nginx", "-g", "daemon off;"]

That pattern is shown in NGINX’s container guidance at NGINX’s Docker installation documentation. The official image already applies the required foreground behavior, so an ordinary docker run does not need an extra daemon option.

Understand the main operating choices

Approach Advantages Costs and risks
One docker run command Fastest path for one NGINX container; easy to inspect with standard Docker commands. Options live in shell history and must be repeated accurately when rebuilding.
Bind-mounted files Changes to HTML and configuration are visible from the Ubuntu host; no image rebuild for each edit. Host paths, permissions, and read-only write locations must be managed carefully.
Custom image Packages configuration and content into a repeatable image build. Requires a Dockerfile and an image rebuild when those files change.
Compose workflow The installed Compose plugin can describe a multi-container setup declaratively. More files and concepts than a single-container test; unnecessary for one disposable server.

Choose the simplest approach that matches the lifetime of the service: a single command for a quick check, mounts for host-managed content, a custom image for repeatable packaging, and Compose when NGINX is one part of a larger application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot an unreachable container

The browser reports connection refused

  • Run docker ps. If the container is absent, inspect docker ps -a and docker logs mynginx.
  • Check the URL’s port. A mapping of 8080:80 requires :8080; a mapping of 80:80 uses the default HTTP port.
  • Test locally on Ubuntu with curl http://localhost:8080 or a browser. If local access works but a remote device cannot connect, investigate address binding, routing, and firewall exposure.
  • If you used 127.0.0.1:8080:80, remote clients are intentionally blocked; recreate the container with an appropriate interface binding when network access is required.

The port is already allocated

Another host process or container is using the selected port. Choose a different host port, stop the conflicting container, or stop the host service after confirming it is safe to do so. The container port remains 80; only the host side of the mapping needs to change.

The container exits immediately

Read docker logs mynginx. Common causes in this workflow are a malformed mounted configuration, a missing host file, or a configuration that expects a location to be writable while the relevant mount is read-only. Correct the host files and recreate the container.

Best Value
Sale
HUANUO FlowLift™ Dual Monitor Stand, Fully Adjustable Gaming Monitor Desk Mount for 13–32″ Computer Screens, Full Motion VESA 75x75/100x100 with C-Clamp & Grommet Base, Each Arm Holds 4.4 to 19.8 lbs
  • Compatible with Wide Screens - To ensure compatibility with the dual monitor mount, your each monitor must meet three conditions at the same time: First, computer screens size range: 13 to 32 inches. Second, screen weight range: 4.4 to 19.8 lbs. Third, the back of the monitor screen must have VESA mounting holes with a pitch of 75x75mm or 100x100mm.
  • Regarding the compatibility with desks - Your desk must meet three conditions at the same time: First, desk material: Only wooden desks are recommended, plastic or glass desks cannot be used. Second, desk thickness range: 0.59" - 3.54". Third, the bottom of the desk should not have any cross beams or panels, as this will interfere with installation. We recommend carefully checking that your desk and monitors meets all above conditions before purchasing.
  • Dual C-Clamp Hold - Worried your dual monitors might wobble or slip? Our upgraded base uses a larger platform plus a dual C-clamp structure to lock the dual monitor arm firmly to your desk. Each arm safely keeps your screens steady while you type, click and game—no shaking, no sliding, just a clean and secure setup you can trust every day. It also provides Grommet Mounting installation choice, both options ensure stable and secure fixation for your 0.59" - 3.54" desk.
  • Full-Motion Adjustment For Comfortable View - Pull the screen closer when you’re deep in a spreadsheet, push it back to watch videos, or rotate to portrait for coding — moving everything smoothly with just one hand. The monitor stand offers +85°/-50° tilt, ±90° swivel and 360° rotation. Raise your monitor up to 15.75″ to support a healthy sitting posture. Whether you’re working from home, gaming through the night, or switching between video calls and documents, getting the screens to your natural line of sight helps relieve neck, shoulder and back strain so you can stay focused longer with less fatigue.
  • Keep Your Desk Organized: By lifting both screens off the desktop, this dual monitor stand opens up valuable space for your keyboard, notebook, docking station or a simple, clutter-free work area. Built-in cable management guides wires along the arms, keeping cords out of sight and out of the way. Enjoy a tidy, modern workstation that looks as good as it feels to use.

The custom page is not displayed

Confirm that index.html is inside the host directory actually mounted to /usr/share/nginx/html. Check for a typo in $PWD, verify that the container was recreated after changing the mount, and inspect the logs for configuration errors.

Remote access works unexpectedly

Remember that an unqualified -p host:container publishes on all interfaces. If the service should be local only, recreate it with an explicit loopback binding and review Docker’s firewall behavior before exposing any other ports.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A repeatable clean-room procedure

  1. Install Docker Engine and the documented package set from Docker’s Ubuntu apt repository.
  2. Run sudo systemctl status docker; start it with sudo systemctl start docker if required.
  3. Run sudo docker run hello-world to verify the engine.
  4. Choose an image tag and decide whether the service is network-wide or localhost-only.
  5. Create the container with docker run --name mynginx -p 8080:80 -d nginx:<tag>, adding bind mounts only after their host paths and configuration are ready.
  6. Check docker ps, read docker logs mynginx if needed, and test the exact URL implied by the host-port mapping.
  7. Before making the port public, review Ubuntu firewall and Docker iptables implications.
  8. Use bind mounts or named volumes for files that must survive container replacement; remove disposable containers with docker stop followed by docker rm.

Frequently Asked Questions

Can I run NGINX in Docker without installing NGINX on Ubuntu?

Yes. The official NGINX image contains NGINX; Ubuntu needs Docker Engine, not a separate host NGINX package.

What does -p 8080:80 mean?

It maps host port 8080 to container port 80. Use port 8080 in the browser URL unless you publish host port 80 instead.

How do I make the container accessible only from Ubuntu itself?

Publish it with -p 127.0.0.1:8080:80. An unqualified mapping publishes on all host interfaces by default.

Why did my files disappear after recreating the container?

Files written only to the container’s writable layer are disposable. Store website files in a bind mount or named volume, and keep your configuration on the host or in a custom image.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.