NGINX Modern Apps > Class 3 - NGINX Dataplane Scripting Source | Edit on
Getting Started¶
Using NGINX with Docker¶
Docker has an official image of NGINX on the Docker Hub. We will use this image to build our containers for the lab. Lets start a simple NGINX container and test it out:
This command first downloads the nginx image (if we don’t already have it) boots a container using the image, maps port 80 on our system to the container, and returns us to the prompt.
Now let’s test our new NGINX container:
You just successfully deployed an NGINX web server running on port 80. Congrats!
We’re done with this NGINX instance so let’s get rid of it:
Note: Since we used the “–rm” option when we started this container, it is automatically removed once we stop it.
This NGINX Docker image also includes njs. Let’s check it out by running the following command:
The njs tool allows you to try out Javascript code outside of NGINX right from your terminal.
The njs tool provides a prompt “>>” for its Javascript console. Let’s try some commands:
Hit <Ctrl-D> to exit.
Downloading Lab Files¶
The files we will use for the labs are made available in a Github repo. Use the following commands to download them:
You will notice that there are two directories, njs and conf. The njs directory contains the JavaScript source code for each lab. The conf directory contains the nginx configuration file needed to bring the JavaScript into our NGINX server. Inside these directories are three subdirectories, http, misc, and stream. For example, the lab named “hello” has a file in the njs/http directory called hello.js and a file in the conf/http directory called hello.conf.
You will find a lab’s name surrounded by [square brackets] at the top of each lab page.
Lab Guidance¶
Make sure you stay in the njs-examples directory (as shown above) for all of your labs while running Docker commands.
Using Docker to start each lab requires many command line options. We will make things easier my using an environment variable to select the lab we will work on so we only have to type the docker command in once and then reuse it from command line history. For example, to start the lab named hello:
Notice how we use Docker “volume mounts” to replace the container’s default configuration with our own.
If you are using a Windows Command Prompt, use percent signs for your environment variables like so:
When you’re done with a lab, remove the running NGINX container before moving to the next one:
We are now ready to start the first lab.