Static website on Docker with the NGINX server
Tags: docker, webdev, nginx, serverless
Published: 2021-04-30 06:32:29

Docker is a free and open framework for creating, delivering, and running software. Docker allows you to isolate your programs/application from the hardware, allowing you to deliver functionality easily regardless of the operating system used by the host or end-user. Docker allows you to handle your technology in the same manner as you manage your apps.

NGINX is open-source software that can be used for web serving, reverse proxying, routing, load balancing, video streaming, and other purposes. It began as a web server intended for high performance and reliability. In addition to HTTP server functionality, NGINX will function as an email proxy server (IMAP, POP3, and SMTP) as well as a reverse proxy and load balancer for HTTP, TCP, and UDP servers.
In this article, you must understand the fundamentals of Docker as well as how images and containers are created. As I am studying containers and deploying at higher levels and achieving higher-level comprehension, I will do my best to describe it in as clear terms as possible.(you can use any cloud service to deploy the container)

A **Docker Image** is a read-only template containing instructions for building a container that can run on the Docker platform. It offers a simple way to package up software and preconfigured cloud environments for private use or public sharing with other Docker users. The CLI can be used to download images from the official Docker hub. Official and custom-built photographs posted by users are among the images that can be retrieved. You may use the `docker search <name of image>` command to look for it in the command line. See below (the red box is the image's name, which can change based on what you're looking for... tens of thousands)

## What happen in the world that is revolving around microservice using the container?
A container is a common unit of software that wraps up code and all its dependencies so that the programme can be moved from one programming environment to another easily and efficiently... Secure: Applications are safer in containers, and Docker has the industry's best default isolation capability.

Now that we have a clear understanding of Docker and its applications, let's look at a situation in which you can host a static website from a Docker container. At a higher level, you can install application code inside this container and use port mapping to open the container to the outside world for entry or to other end users (securely, of course...). In this situation, though, we are deploying it to our localhost.
Note: You can choose to run your container on any port that you wish to run from the option of 50,000+ ports.

## Dockerfile
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

```dockerfile
FROM ubuntu

RUN apt-get update

RUN apt-get install nginx -y

COPY index.html /var/www/html/

EXPOSE 80

CMD [“nginx”,”-g”,”daemon off;”]
```

`FROM` — is where we are pulling our official image from ubuntu is an official image provided by Docker
WORKDIR- you guessed it…it is setting the current location (directory) of where are files are located (not used here but an option for your future use)

`RUN` — provides an instruction to download and acquire all packages updates from the internet

the next run `RUN`- instruction install nginx web server on our container

`COPY` (not to be confused with the `ADD` command)- is taking our index.thml file from out local directory were are working in and moves it into the /var/www/html root directory(of NGINX) in our container. (Note: with the ADD command also close in its function, you are able to reference a URL and other things) make sure your index.html file is in your working directory.

`EXPOSE`- is a documentation instruction letting the container know we are exposing the standard port 80 (TCP)
CMD- provide defaults for an executing container
Next, let’s build our docker container using the docker build command which will pull from our Docker File instructions.

`docker build -t <name of tag of container I used dockernginx> .` ← **.** is to symbolize current directory.

You should now be able to see your phone. Run each phase of the docker file, including the download and pull of the Ubuntu image from the official repo, apt-get install updates, and nginx installation. You can see the following results at the start.

You will see that the container was successfully installed and that all of the steps were completed.
Your picture ID is 7283b7999d05, so you'll have a different ID# than me.
You also see where that -t option when used in our command tagged the container as `dockernginx:latest`.
Now, let's run our container, and I'll go over the options quickly. I would highly recommend checking the official Docker website for commands and descriptions, since there are just too many to recall.
Here we are going to run: `docker container run -t -d -p 8000:80 dockernginx:latest`

`-d` = detached mode — means that a Docker container runs in the background of your terminal. If you run containers in the background, you can find out their details using docker ps and then reattach your terminal to its input and output.
`-p` = port mapping [hostname — you] : [port we exposed in the Dockerfile also the port that nginx runs on]

NOT USED HERE BUT YOU MAY NEED THIS TO LOG IN TO YOUR CONTAINER IN THE FUTURE:
`-i` & `-t` or else even `-it` = interactive mode which will give us the option to log in and run commands to the inside of the container if we so choose to.

— NAME <CONTAINER NAME IT WILL CHANGE FROM RANDOM CHARACTERS TO THE SPECIFIC NAME YOU CHOOSE>

This long number you see under the first command is your running container ID. Here you can also see the `-t` (or tag) we used when we built or original image. The next command `docker ps` shows all of your `RUNNING` containers.
Finally, let's check to see if our static website is operational, as shown by the index.html format. We will use the hostname we defined in the port mapping step, localhost:8000. The same web page will be shown if you use your `local IP address:8000`.Finally, let's check to see if our static website is operational, as shown by the index.html format. We will use the hostname we defined in the port mapping step, localhost:8000. The same web page will be shown if you use your local IP address:8000.

### Now Lets:
* __Stop all running containers :__ `docker stop $(docker ps -aq)`- to stop this one container → after stop [name of container-name or container-id]

## this is optional step for making a best use of the memory that is avaialble to you.

* __Remove all containers :__. `docker rm $(docker ps -aq)` — to delete this one after rm → after `rm` [name of container-name or container-id]

* __Remove all images :__ `docker rmi $(docker images -q)`- to remove this one image after rmi → after `rmi` [name of image in local system or id of image from 'docker images' command]

That's it you need not have to give any reason for not deploying the html files.