Product docs and API reference are now on Akamai TechDocs.
Search product docs.
Search for “” in product docs.
Search API reference.
Search for “” in API reference.
Search Results
 results matching 
 results
No Results
Filters
How to Deploy Nginx via Docker Container on Linode
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Nginx remains one of the most widely used and adaptable web servers around, frequently used for its reverse proxy and load balancer capabilities. With Docker, you can deploy your own Nginx server with a reduced administrative overhead. The portability and self-contained nature of Docker containers can make running and maintaining an Nginx server easier, and lead into scalable options like Kubernetes.
Follow along with this tutorial to learn about the advantages of running Nginx via a Docker container and how to do that yourself.
Before You Begin
If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.
sudo
. If you’re not familiar with the sudo
command, see the Users and Groups
guide.Getting Started with Docker
Docker is an open source platform for creating standalone, portable application containers. A Docker container operates with its own runtime environment and everything the containerized application needs. It thus provides a self-contained and portable solution that is more efficient than virtual hosting.
Docker containers also have the advantage of container orchestration. Platforms like Kubernetes can leverage the portability and self-contained nature of Docker containers to efficiently deploy applications to clusters.
Read more about Docker, its advantages, and its use cases in our guide When and Why to Use Docker . Learn about using Docker with Kubernetes container orchestration in our guide on how to Manage a Docker Cluster with Kubernetes .
Why Run Nginx in a Docker Container?
Running Nginx via a Docker container can serve a range of use cases. Most of the advantage lies in running Nginx in an easy-to-manage and self-contained environment. Should you need to update your Nginx instance or to install a new version, the process is as simple as replacing your existing container. At the same time, using Docker minimizes the clutter of dependency files and software on your host system.
Beyond this, a Dockerized Nginx instance can be a gateway to more complicated setups. It allows you to test out particular Nginx container configurations before deploying them as part of a wider infrastructure, such as a Kubernetes deployment.
Installing Docker
To install Docker, follow the instructions in our guide on Installing and Using Docker . Use the drop down at the top of the guide to select the appropriate distribution. The present tutorial assumes that you have followed the sections on:
- Installing Docker Engine
- Starting and Testing Docker
- Using Docker with a Non-Root User
Deploying Nginx as a Docker Container
With Docker installed, you’re ready to start up an Nginx container. This section of the tutorial walks through the steps to get a basic Nginx server up and running in a Docker container.
Continue on to the next section to advance your Nginx container instance with custom configurations and content to serve.
Starting the Nginx Container
The standard method for deploying simple Nginx server within a Docker container uses Docker’s run
command. This command pulls the latest Nginx Docker image and starts running it immediately within a new container.
docker run --name nginx-docker -p 80:80 -d nginx
This example command includes several useful options for effectively leveraging your Nginx container:
--name
allows you to name the container, here asnginx-docker
.-p
defines a port mapping, with the container’s port80
mapped to the host machine’s port80
.-d
runs the container in detached mode, meaning that the container continues to run in the background until stopped.
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
52d2b7f179e3: Pull complete
fd9f026c6310: Pull complete
055fa98b4363: Pull complete
96576293dd29: Pull complete
a7c4092be904: Pull complete
e3b6889c8954: Pull complete
da761d9a302b: Pull complete
Digest: sha256:104c7c5c54f2685f0f46f3be607ce60da7085da3eaa5ad22d3d9f01594295e9c
Status: Downloaded newer image for nginx:latest
b39a20f24545fdf66218a1f7afa6b3643fd85a58bc1460f383972004dfd67811
Connecting to the Nginx Server
The Nginx container now runs a working Nginx server on your system. Because the command above mapped the container’s Nginx port to your system’s port 80
, you can see the Nginx output there.
Open a Web browser and navigate to your system’s public IP address. For example, if you IP address is 192.0.2.0
, navigate to http://192.0.2.0
. You should see the Nginx welcome page:
80
/http
. Refer to the links in the firewall section of our Setting Up and Securing a Compute Instance
guide to learn how.Jump ahead to the section on Applying Custom Nginx Configurations to take the next step in running your own website through the Nginx container.
Managing the Nginx Container
The Docker command used above to start up Nginx runs the container in the background. When you are ready to stop the container, you can do so with a Docker stop
command and the container’s name.
Using the name in the example above, the command to stop the container is:
docker stop nginx-docker
This does not remove the container, and you can start the container again using the Docker start
command. Again, here is an example using the container name given above:
docker start nginx-docker
Should you want to remove the container altogether, you can use the Docker rm
command with the container name, as in the following example. Doing so is useful to free resources when you no longer need the container or when you want to start fresh.
docker rm nginx-docker
Applying Custom Nginx Configurations
The above gets Nginx running within a Docker container, but you need to have Nginx serve your content. Not only that, but you may want to run a custom Nginx configuration.
Docker provides a few ways to get the content and configurations you need for your Nginx server. Each of the sections below cover a different method, from the easier and ready-to-go to the more complicated and adaptable.
Before taking up either method, you should stop and remove your existing Nginx Docker container if you created one following the steps above:
docker stop nginx-docker
docker rm nginx-docker
Mounting Configuration and Content Files
Docker includes a -v
option to mount directories from your host system into the container. This provides the simplest solution for customizing Nginx to your needs.
This example walks through creating a simple website and Nginx configuration and deploying them to a new Nginx Docker container.
Make a directory for your Nginx content and configuration, change into that directory:
mkdir ~/example-nginx-site cd ~/example-nginx-site
From there, create an
nginx-default.conf
file:nano nginx-default.conf
You can give the
nginx-default.conf
file whatever contents you want for your Nginx configuration. Below is a bare-bones version of a default Nginx static server configuration:- File: nginx-default.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
server { listen 80; listen [::]:80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
When done, press CTRL+X, followed by Y then Enter to save the file and exit
nano
.Create an
html
subdirectory to use for storing your website content.mkdir html
Add files for your desired website into the
html
directory. Following the configuration above, the directory should have anindex.html
file, so create it:nano html/index.html
This example
index.html
file provides a basic webpage:- File: html/index.html
1 2 3 4 5 6 7 8 9 10 11
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example Nginx Website</title> </head> <body> <h1>An Example Nginx Website</h1> <p>Welcome to your own Nginx website!</p> </body> </html>
When done, press CTRL+X, followed by Y then Enter to save the file and exit
nano
.Start up an Nginx Docker container with additional options to mount the custom configuration file and website content.
docker run --name nginx-docker -p 80:80 -d -v ~/example-nginx-site/nginx-default.conf:/etc/nginx/conf.d/default.conf -v ~/example-nginx-site/html:/usr/share/nginx/html nginx
The
run
command here remains similar to the one demonstrated further above. However, this time two-v
options are included:The first mounts your custom configuration file (
nginx-default.conf
) to the container’s location for the default Nginx configuration file. This overwrites the container’s default Nginx configuration file with your own.The second mounts your custom website content (from the
html
subdirectory) to the directory in the container where Nginx looks for web content (/usr/share/nginx/html
).
Open a Web browser and navigate to your system’s public IP address once again. You should now find your custom webpage:
Custom Image Deployment
The method above for including configurations and content with your Nginx container works well for most use cases. However, you may find yourself needing more control or wanting a solution other than mounting.
At this point, you are probably best served creating your own Dockerfile for a custom Nginx image. All of the examples above use the default Nginx image, pulled from Docker Hub. Using a Dockerfile, you can build around the base Nginx image to create an image customized to your use case.
To begin, this example leverages the custom files created in the previous section. Follow the relevant steps there, and you should have an ~/example-nginx-site
directory with the following structure:
example-nginx-site/
|-html/
| |-index.html
|-nginx-default.conf
From there, follow along with the steps below to create a simple Nginx Docker image. Essentially, this image behaves the same as the setup in the previous section. Following that model provides a relatively easy-to-read example and provides an effective basis for more complicated images.
Stop and remove any Docker container you created from the previous sections of the tutorial:
docker stop nginx-docker docker rm nginx-docker
Change into the
example-nginx-site
directory if you are not already in it and create a file namedDockerfile
:cd ~/example-nginx-site nano Dockerfile
Give the
Dockerfile
the contents shown here:- File: Dockerfile
1 2 3 4
FROM nginx:latest RUN rm /etc/nginx/conf.d/default.conf COPY nginx-default.conf /etc/nginx/conf.d/default.conf COPY html /usr/share/nginx/html
When done, press CTRL+X, followed by Y then Enter to save the file and exit
nano
.Use the Docker
build
command to build an image from the Dockerfile. This example gives the image the namenginx-docker-image
.docker build -t nginx-docker-image .
[+] Building 0.3s (9/9) FINISHED docker:default => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 182B 0.0s => [internal] load metadata for docker.io/library/nginx:latest 0.0s => [1/4] FROM docker.io/library/nginx:latest 0.0s => [internal] load build context 0.0s => => transferring context: 618B 0.0s => [2/4] RUN rm /etc/nginx/conf.d/default.conf 0.2s => [3/4] COPY nginx-default.conf /etc/nginx/conf.d/default.conf 0.0s => [4/4] COPY html /usr/share/nginx/html 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:ceaa49bca8f20e1ea684a286ada3819cc69150eb40ef3 0.0s => => naming to docker.io/library/nginx-docker-image 0.0s
Run the new Docker image. The command for doing so is almost exactly the same command for running the default Nginx image, except here you provide your custom image’s name rather than
nginx
.docker run --name nginx-docker -p 80:80 -d nginx-docker-image
Once again, you should be able to navigate to your system’s public IP address in a Web browser to see your custom website.
To move ahead with more advanced Dockerfile setups, take a look at our guide How to Use a Dockerfile to Build a Docker Image . There, you can find a thorough overview of Dockerfile creation and usage.
Conclusion
Nginx pairs perfectly with Docker’s containerization. You now have your own setup, ready for configuration and deployment to meet your server needs. Take a look at the resources linked throughout this tutorial to expand your learning and get even more out of your new setup.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on