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.
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
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 :
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
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:
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.