Install Docker in Ubuntu 20.04 and Run Nginx Container - Step by Step Process ?

Docker is an Open source tool designed to make it easier to create, build and run applications using containers. Docker containers act as a runtime and possess all the required dependencies and libraries so that the same applications can run in other OS also. 

Nowadays containers based applications are being widely used.

To save time and effort, to run hassle-free applications in all the OS platforms, Docker containers are widely adopted. 

Nginx is a fast, open-source, and more reliable web server that is used for server-side application development. Nginx server application support to run on many different operating systems. Nginx is very useful for development tasks. Therefore, the Docker container provides support for the Nginx server.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Nginx and Docker related queries.

In this context, we shall look into how to install Docker in Ubuntu 20.04 and Run Nginx container.


How to install Docker in Ubuntu ?

To begin, ensure that the the user account have Sudo privileges in order to install packages. Also, We need to update the Docker repository in Ubuntu, get the GPG key, and install docker packages and dependencies. You can install the latest version of Docker using the official docker repository in Ubuntu 20.04. 

For this, you need to add the GPG key for the official Docker repository to your system and add the repository configuration to the APT source.

To make this procedure clearer, let's break this into steps.


1.  Download Docker GPG Key

Run the following command to add GPG key:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -


2. Add Docker GPG Key to System Repository

Add and configure the official docker repository in your system:

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"


3. Update System Repository

Now, update the APT package to include new Docker packages using the following command:

$ sudo apt update


4. Install Docker

Now you can install docker packages using the following command:

$ sudo apt install docker-ce

During the docker packages installation, the installer package triggers systemd to automatically enable and start the docker server. 

Use the following command to check docker service status.


5. Check Docker Status

To check docker service is active or not, run the following command:

$ sudo systemctl is-active docker

To check if the docker service is enabled or not, run the following command:

$ sudo systemctl is-enabled docker

To check the docker service status, run the following command:

$ sudo systemctl status docker


6. Stop, Start or Restart Docker

There are other systemctl commands available to control the docker services which are as follows:

$ sudo systemctl stop docker # stop docker service
$ sudo systemctl start docker # start docker service
$ sudo systemctl restart docker # restart docker service


7. Check Docker Version

To check the version of docker run the following command:

$ docker version


How to Run Nginx Container Using Docker ?

Running Nginx in docker containers is very simple and easy. You just need to pull an Nginx image from the docker hub and create an Nginx container that serves as a web server for static files. 

To pull the latest Nginx image, run the following command:

$ sudo docker pull nginx

To list the docker images, run the command:

$ sudo docker images

To run a container from a pulled image, run the following command:

$ sudo docker run -d --name nginx-server -p 80:80 nginx

where,

d=run the container in detached mode

name= name of the container to be created

p= port the container will be mapped with host


You will have output similar as :

$ 4ef30a6599d0a7f9618883441fdd2a683e7205287d09f92dcd3b63f4892551e8

The output shows the container id created using the Nginx image.


To list out the running container, run the command:

$ sudo docker ps -a

You can find containers with status in your terminal with the above command. 


Nginx is running in docker containers with port 80. 

You can verify Nginx installation by navigating to the URL http://your-server-ip in your browser.


How to Create Docker Volume for Nginx ?

The containers we have just created are wrapping all the Nginx configuration and static files in the container itself. If we need to change anything or replace files, we need to access docker containers every time. Similarly in case if we delete the container, all the files and configuration files also get removed. 

To mitigate this issue, we need to create a docker volume in the host and map it with a container to protect configuration and web files. 

Here, we have taken nginx-data as a volume name. You can have your own assumption.

To create a docker volume run the following command:

$ sudo docker volume create nginx-data

Get the docker volume information by running the command:

$ sudo docker volume inspect nginx-data

For easy access, you can create a symlink of the docker volume directory.

To create symlink run the following command:

$ ln -s /var/lib/docker/volumes/nginx-data/_data /nginx

Now start the Nginx container with persistent data storage:

$ sudo docker run -d --name nginx-server -p 80:80 -v nginx-data:/usr/share/nginx/html nginx

Where,

d= run container in detached mode

name= name of the container to be created

p= port to be mapped with host

v= name of docker volume


Container has been started with persistent data storage. You will get container id as output as:

$ 9067684b1133a2c7e36381574ea9af3ebbb79dd2504f63ae3569bb059b74d901

Now verify the contents available in the data persistent directory:

$ ls /var/lib/docker/volumes/nginx-data/_Data

Let's change the content of index.html file located at /var/lib/docker/volumes/nginx-data/_data:

$ sudo vi /var/lib/docker/volumes/nginx-data/_data/index.html

Change some HTML code and save the file. Navigate the URL in your browser and you will find your Nginx contents changed.


[Need urgent assistance in fixing Nginx related errors? We can help you. ]

This article covers how to install docker, pull docker images from docker hub and run an application in a container. Also, you will learn how to create persistent data storage and map with docker containers.

The open-source Docker platform contains a docker engine, a runtime environment that is used to execute, builds, and orchestrates containers. 


Facts about Docker Compose configurations file:

1. version: Compose file version which is compatible with the Docker Engine. You can check compatibility here.

2. image: We use latest Nginx and Certbot images available in Docker hub.

volumes:

3. public: we have configured this directory to be synced with the directory we wish to use as the web root inside the container.

4. conf.d: here we will place the Nginx configuration file to be synced with the default Nginx conf.d folder inside the container.

5. certbot/conf: this is where we will receive the SSL certificate and this will be synced with the folder we wish to inside the container.

6. ports: configure the container to listen upon the listed ports.

7. command: the command used to receive the SSL certificate.


To Start Docker Containers:

You need to pass the -d flag which starts the container in background and leaves them running.

$ docker-compose up -d

Related Posts