Ubuntu Docker Nginx

This is a note for setting up a Docker, Nginx and Let's Encrypt environment on Ubuntu 20.04 LTS.

  1. I had tried updating docker file with. #RUN sudo apt-get install -y nginx-module-njs. But get similar message that connate find package. Key here is I am starting with ubuntu image. I am interested in how to setup docker to load the 'dynamic' module with nginx. Docker ubuntu nginx dockerfile nginx-config.
  2. Docker NGINX Tutorial – We shall learn to run NGINX in a Docker Container on Ubuntu. Following is a step by step guide to dockerize NGINX: 1. Install Docker Engine. Docker is the prerequisite. Follow the tutorial, Install Docker on Ubuntu, to install docker on your computer with Ubuntu. Pull NGINX from Docker Hub.

Create a Ubuntu 20.04 LTS instance¶

Install Docker using the convenience script¶

Manage Docker as a non-root user¶

If you don't want to preface the docker command with sudo, create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group.

Docker

To create the docker group and add your user:

  1. Create the docker group.

Setup Docker/Ngnix and Let's Encrypt on Ubuntu¶ This is a note for setting up a Docker, Nginx and Let's Encrypt environment on Ubuntu 20.04 LTS. Create a Ubuntu 20.04 LTS instance¶ Install Docker using the convenience script¶. Default Use default settings; do not prompt user for input -openssl-fips-cdrom-path Path for the volume (CD-ROM) with the source -openssl-fips-version Version of the source to install -ssl-cert-path Path for SSL certificate(s) and key(s) -nginx-conf-path Path for Nginx conf directory to use in deployment -image-tag Tag for the Docker image to be built -run Attempt to run the Docker.

  1. Add your user to the docker group.
  1. Log out and log back in so that your group membership is re-evaluated.

    On Linux, you can also run the following command to activate the changes to groups:

Configure Docker to start on boot¶

To disable this behavior, use disable instead.

Install Docker Compose¶

On Linux, you can download the Docker Compose binary from the Compose repository release page on GitHub. Follow the instructions from the link, which involve running the curl command in your terminal to download the binaries. These step-by-step instructions are also included below.

  1. Run this command to download the current stable release of Docker Compose:
Nginx

Note

To install a different version of Compose, substitute 1.25.5 with the version of Compose you want to use.

  1. Apply executable permissions to the binary:

Note

If the command docker-compose fails after installation, check your path. You can also create a symbolic link to /usr/bin or any other directory in your path. For example:

Set up Nginx-Proxy¶

Create a unique network for nginx-proxy and other Docker containers to communicate through.

Create a directory nginx-proxy for the compose file.

Nginx

In the nginx-proxy directory, create a new file named docker-compose.yml and paste in the following text:

example docker-compose.yml for nginx-proxy

Inside of the nginx-proxy directory, use the following curl command to copy the developer’s sample nginx.tmpl file to your VPS.

Increase upload file size

To increase the maximum upload size, for example, add client_max_body_size 100M; to the server{} section in the nginx.tmpl template file. For WordPress,

Running nginx-proxy.

Add a WordPress container¶

Create a directory for the docker-compose.yml with:

example docker-compose.yml for WordPress container

To create a second WordPress container, add MYSQL_TCP_PORT environment variable and set it to a different port.

Increase maximum WordPress upload file size¶

Enter the bash of the WordPress container.

Docker Ubuntu Nginx Server

Move inside your /var/www/html directory (already there if you’re using the standard Docker Compose image). Run the following command to insert the values.

Note

To restore the values, run $ sed -i '11,12d' .htaccess

Comments

Ubuntu

Deploy NGINX and NGINX Plus as the Docker container.

NGINX Plus, the high‑performance application delivery platform, load balancer, and web server, is available as the Docker container.

Prerequisites

  • Docker installation
  • Docker Hub account (NGINX Open Source)
  • nginx-repo.crt and nginx-repo.key files, Dockerfile for Docker image creation (NGINX Plus)

Running NGINX Open Source in a Docker Container

You can create an NGINX instance in a Docker container using the NGINX Open Source image from the Docker Hub.

  1. Launch an instance of NGINX running in a container and using the default NGINX configuration with the following command:

    where:

    • mynginx1 is the name of the created container based on the NGINX image

    • the -d option specifies that the container runs in detached mode: the container continues to run until stopped but does not respond to commands run on the command line.

    • the -p option tells Docker to map the ports exposed in the container by the NGINX image (port 80) to the specified port on the Docker host. The first parameter specifies the port in the Docker host, the second parameter is mapped to the port exposed in the container

    The command returns the long form of the container ID: fcd1fb01b14557c7c9d991238f2558ae2704d129cf9fb97bb4fadf673a58580d. This form of ID is used in the name of log files.

  2. Verify that the container was created and is running with the docker ps command:

This command also allows viewing the port mappings set in the previous step: the PORTS field in the output reports that port 80 on the Docker host is mapped to port 80 in the container.

Running NGINX Plus in a Docker Container

Docker can also be used with NGINX Plus. The difference between using Docker with NGINX Open Source is that you first need to create an NGINX Plus image, because as a commercial offering NGINX Plus is not available at Docker Hub.

Note: Never upload your NGINX Plus images to a public repository such as Docker Hub. Doing so violates your license agreement.

Creating NGINX Plus Docker Image

To generate an NGINX Plus image:

  1. Create the Docker build context, or a Dockerfile:

  2. As with NGINX Open Source, default NGINX Plus image has the same default settings:

    • access and error logs are linked to the Docker log collector
    • no volumes are specified: a Dockerfile can be used to create base images from which you can create new images with volumes specified, or volumes can be specified manually:
    • no files are copied from the Docker host as a container is created: you can add COPY definitions to each Dockerfile, or the image you create can be used as the basis for another image
  3. Log in to MyF5 Customer Portal and download your nginx-repo.crt and nginx-repo.key files. For a trial of NGINX Plus, the files are provided with your trial package.

  4. Copy the files to the directory where the Dockerfile is located.

  5. Create a Docker image, for example, nginxplus (note the final period in the command).

    The --no-cache option tells Docker to build the image from scratch and ensures the installation of the latest version of NGINX Plus. If the Dockerfile was previously used to build an image without the --no-cache option, the new image uses the version of NGINX Plus from the previously built image from the Docker cache.

  6. Verify that the nginxplus image was created successfully with the docker images command:

  7. Create a container based on this image, for example, mynginxplus container:

  8. Verify that the mynginxplus container is up and running with the docker ps command:

NGINX Plus containers are controlled and managed in the same way as NGINX Open Source containers.

Managing Content and Configuration Files

Content served by NGINX and NGINX configuration files can be managed in several ways:

  • files are maintained on the Docker host
  • files are copied from the Docker host to a container
  • files are maintained in the container

Maintaining Content and Configuration Files on the Docker Host

When the container is created, you can mount a local directory on the Docker host to a directory in the container. The NGINX image uses the default NGINX configuration, which uses /usr/share/nginx/html as the container’s root directory and puts configuration files in /etc/nginx. For a Docker host with content in the local directory /var/www and configuration files in /var/nginx/conf, run the command:

Any change made to the files in the local directories /var/www and /var/nginx/conf on the Docker host are reflected in the directories /usr/share/nginx/html and /etc/nginx in the container. The readonly option means these directories can be changed only on the Docker host, not from within the container.

Copying Content and Configuration Files from the Docker Host

Docker can copy the content and configuration files from a local directory on the Docker host during container creation. Once a container is created, the files are maintained by creating a new container when files change or by modifying the files in the container.

A simple way to copy the files is to create a Dockerfile with commands that are run during generation of a new Docker image based on the NGINX image. For the file‑copy (COPY) commands in the Dockerfile, the local directory path is relative to the build context where the Dockerfile is located.

Let’s assume that the content directory is content and the directory for configuration files is conf, both subdirectories of the directory where the Dockerfile is located. The NGINX image has the default NGINX configuration files, including default.conf, in the /etc/nginx/conf.d directory. To use the configuration files from the Docker host only, delete the default files with the RUN command:

Create NGINX image by running the command from the directory where the Dockerfile is located. The period (“.”) at the end of the command defines the current directory as the build context, which contains the Dockerfile and the directories to be copied:

Create a container mynginx3 based on the mynginx_image1 image:

Docker Ubuntu Nginx Php Mysql

To make changes to the files in the container, use a helper container as described in the next section.

Maintaining Content and Configuration Files in the Container

As SSH cannot be used to access the NGINX container, to edit the content or configuration files directly you need to create a helper container that has shell access. For the helper container to have access to the files, create a new image that has the proper Docker data volumes defined for the image:

  1. Copy nginx content and configuration files and define the volume for the image with the Dockerfile:

  2. Create the new NGINX image by running the following command:

  3. Create an NGINX container mynginx4 based on the mynginx_image2 image:

  4. Start a helper container mynginx4_files that has a shell, providing access the content and configuration directories of the mynginx4 container we just created:

    where:

    • the new mynginx4_files helper container runs in the foreground with a persistent standard input (the -i option) and a tty (the -t option). All volumes defined in mynginx4 are mounted as local directories in the helper container.
    • the debian argument means that the helper container uses the Debian image from Docker Hub. Because the NGINX image also uses Debian, it is most efficient to use Debian for the helper container, rather than having Docker load another operating system
    • the /bin/bash argument means that the bash shell runs in the helper container, presenting a shell prompt that you can use to modify files as needed

To start and stop the container, run the commands:

To exit the shell but leave the container running, press Ctrl+p followed by Ctrl+q. To regain shell access to a running container, run this command:

To exit the shell and terminate the container, run the exit command.

Managing Logging

You can use default logging or customize logging.

Using Default Logging

By default, the NGINX image is configured to send NGINX access log and error log to the Docker log collector. This is done by linking them to stdout and stderr: all messages from both logs are then written to the file /var/lib/docker/containers/container-ID/container-ID-json.log on the Docker host. The container‑ID is the long‑form ID returned when you create a container. To display the long form ID, run the command:

You can use both the Docker command line and the Docker Engine API to extract the log messages.

To extract log messages from the command line, run the command:

To extract log messages using the Docker Remote API, send a GET request using the Docker Unix sock:

To include only access log messages in the output, include only stdout=1. To limit the output to error log messages, include only stderr=1. For other available options, see Get container logs section of the Docker Engine API documentation.

Using Customized Logging

If you want to configure logging differently for certain configuration blocks (such as server {} and location {}), define a Docker volume for the directory in which to store the log files in the container, create a helper container to access the log files, and use any logging tools. To implement this, create a new image that contains the volume or volumes for the logging files.

For example, to configure NGINX to store log files in /var/log/nginx/log, add a VOLUME definition for this directory to the Dockerfile (provided that content and configuration Files are managed in the container):

Then you can create an image and use it to create an NGINX container and a helper container that have access to the logging directory. The helper container can have any desired logging tools installed.

Controlling NGINX

Since there is no direct access to the command line of the NGINX container, NGINX commands cannot be sent to a container directly. Instead, signals can be sent to a container via Docker kill command.

To reload the NGINX configuration, send the HUP signal to Docker:

To restart NGINX, run this command to restart the container: