Title: VEXXHOST - How To Run Nginx in a Docker Container on Ubuntu 16.04
1(No Transcript)
2How To Run Nginx in a Docker Container on Ubuntu
16.04
- Nginx is an open source reverse proxy server for
HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as
well as a load balancer, HTTP cache, and a web
server (origin server). - Docker containers enable developers to focus
their efforts on application content by
separating applications from the constraints of
infrastructure. - Dockerized applications are instantly portable to
any infrastructurelaptop, bare-metal server, VM,
or cloudmaking them modular components that can
be readily assembled and re-assembled into fully
featured distributed applications and
continuously innovated on in real time. - In practice, this means we can take one
application or group of applications to wrap them
in a container to make them modular, portable,
compose-able, and lightweight.
3How To Run Nginx in a Docker Container on Ubuntu
16.04
- This portability means you can install the Docker
Engine on a wide variety of operating systems,
and any functional container written by anyone
will run on it. - Docker containers are a popular form of a
relatively old operations practice, that is
containerization. - Containerization differs from virtualization in
that virtualization abstracts away the hardware,
while containerization abstracts away the base
operating system, too. - In this article we will serve a basic web page,
so we can focus on configuring Nginx with a
Docker container.
4How To Run Nginx in a Docker Container on Ubuntu
16.04
- Prerequisites
- Login to your Ubuntu 16.04 LTS server using
your sudo credentials or the best practice is to
login using SSH-key . - Docker requires a 64-bit installation regardless
of your Ubuntu version. - Additionally, your kernel must be 3.10 at
minimum. - The latest 3.10 minor version or a newer
maintained version are also acceptable. - Once you got access to your server, run the
command below to check your kernel version, to
make sure the kernel is at least 3.10 or above. - ubuntu_at_ubuntu-16 uname r
- 4.4.0-24-generic
5How To Run Nginx in a Docker Container on Ubuntu
16.04
- Prerequisites
- Docker 1.11.2 relies on some fairly recent kernel
features, so we using the right kernel version
recommended for Docker 1.11.2 . - But its fairly good to update your system by
flowing the command below before moving to the
next step. - sudo apt-get -y update
6How To Run Nginx in a Docker Container on Ubuntu
16.04
- Installing Docker on Ubuntu 16.04
- Docker hosts a startup script to get Docker up
and running on your machine. We can install it
simply by running the command below. - sudo curl -sSL https//get.docker.com/ sh
- It will take a while and upon successful
installation, youll see the installed version
and some instructions for running as
non-root/without sudo as shown below.
7How To Run Nginx in a Docker Container on Ubuntu
16.04
- Installing Docker on Ubuntu 16.04
- Now verify that docker is installed correctly
on your system by using below command. - sudo docker run hello-world
8How To Run Nginx in a Docker Container on Ubuntu
16.04
- Installing Docker on Ubuntu 16.04
- This command downloads a test image and runs it
in a container. - When the container runs, it prints an
informational message. - Then, it exists.
- As we have installed the Docker Client as part of
our Docker installation, so we can have access to
the command line tool that allows us to interact
with our containers. - Now we will see that how we can run a basic
container and then remove it by using below
commands. - sudo docker ps -a
9How To Run Nginx in a Docker Container on Ubuntu
16.04
- Installing Docker on Ubuntu 16.04
- After execution of above command, you will get
some basic information about the container - Now when we run this container again with the
container then you will see that the container
has run recently as shown below. - sudo docker start angry_bassi
- By default, Docker containers run their assigned
commands and then exit. - But some containers will be set up to run through
a list of tasks and finish, while others will run
indefinitely.
10How To Run Nginx in a Docker Container on Ubuntu
16.04
- Installing Docker on Ubuntu 16.04
- To remove the hello-world image, as we wont be
needing it again run below command containing
your own docker name. - sudo docker rm angry_bassi
11How To Run Nginx in a Docker Container on Ubuntu
16.04
- Downloading Nginx Docker Image
- In this section, well download the Nginx Docker
image and show you how to run the container so
its publicly accessible as a web server. - By default, containers are not accessible from
the Internet, so we need to map the containers
internal port to the Droplets port. - First, run the below command to get the Nginx
image. - sudo docker pull nginx
12How To Run Nginx in a Docker Container on Ubuntu
16.04
- Downloading Nginx Docker Image
- Thats all we need to get Nginx up! Paste the IP
address of your Ubuntu Droplets into a web
browser and you should see Nginxs Welcome to
nginx! page. - Then hit the break shortcut CTRLC to get back to
our shell session. - In order to verify, run below command to check
the status of Nginx container has exited. - sudo docker ps -a
13How To Run Nginx in a Docker Container on Ubuntu
16.04
14How To Run Nginx in a Docker Container on Ubuntu
16.04
- Running Docker Nginx in Detached Mode
- First, remove the old Nginx container and then
create a new, detached Nginx container by using
below commands. - sudo docker rm docker-nginx sudo docker run
--name docker-nginx -p 8080 -d nginx - Whereas the -d is being used to run this
container in the background. - Now you will its status as Up upon execution of
below command. -
- sudo docker ps
15How To Run Nginx in a Docker Container on Ubuntu
16.04
- Running Docker Nginx in Detached Mode
- If you open your servers IP address again in
your browser, we will be able to see the Welcome
to nginx! page again. - Now first stop the docker-nginx container and
then remove it using commands shown below to move
to the next step. - sudo docker stop docker-nginx sudo docker
rm docker-nginx
16How To Run Nginx in a Docker Container on Ubuntu
16.04
- Building a Web Page to Serve on Nginx
- Now well get to the final version of our
container, with a quick stop to generate a custom
website file. - This setup allows us to have persistent website
content thats hosted outside of the (transient)
container. - Create a new directory for your website content
within the home directory, and change your
directory to it, by running the commands shown
below. - mkdir -p /docker-nginx/html cd
/docker-nginx/html
17How To Run Nginx in a Docker Container on Ubuntu
16.04
- Building a Web Page to Serve on Nginx
- Now lets create an HTML file using any of your
command line editor and put some text on your
index page. - vim index.html
- Save the file using wq! you will get a simple
index page to replace the default Nginx landing
page. - Now well start our Nginx container so its
accessible on the Internet over Port 80, and
well connect it to our website content on the
server. Since we want to serve web pages, so we
need to give our container the files to render.
18How To Run Nginx in a Docker Container on Ubuntu
16.04
- Building a Web Page to Serve on Nginx
- The Nginx container is set up by default to look
for an index page at /usr/share/nginx/html, so in
our new Docker container, we need to give it
access to our files at that location. - Lets use the -v flag to map a folder from your
local machine /docker-nginx/html to a relative
path in the container that is /usr/share/nginx/htm
l - This can be accomplished by using the following
command in your ssh terminal. - sudo docker run --name docker-nginx -p 8080
-d -v /docker-nginx/html/usr/share/nginx/html
nginx - Now if you open your web browser followed by the
IP address or hostname of your Ubuntu 16.04
server, you will your index page text.
19How To Run Nginx in a Docker Container on Ubuntu
16.04
20How To Run Nginx in a Docker Container on Ubuntu
16.04
- You can upload more content to the /docker-nginx/
html/ directory, and it will be added to your
live website. - We can build a whole site out of flat HTML files
this way if we wanted to. - For example, if we added an about.html page, we
could access it at http//your_server_ip/about.htm
l without needing to interact with the container.
21How To Run Nginx in a Docker Container on Ubuntu
16.04
- Conclusion
- Congratulations, we have successfully setup Nginx
container serving a custom web page. - NGINX, and Docker work extremely well together.
- Whether you use the open source NGINX image from
the Docker Hub repository or create your own
NGINX image, you can easily spin up new instances
of NGINX in Docker containers.