{"id":4677,"date":"2021-02-19T04:37:21","date_gmt":"2021-02-19T04:37:21","guid":{"rendered":"https:\/\/linuxways.net\/?p=4677"},"modified":"2021-02-19T13:40:40","modified_gmt":"2021-02-19T13:40:40","slug":"how-to-install-docker-in-ubuntu-20-04-and-run-nginx-container","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-install-docker-in-ubuntu-20-04-and-run-nginx-container\/","title":{"rendered":"How to Install Docker in Ubuntu 20.04 and Run Nginx Container"},"content":{"rendered":"<p>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. In this article, we are going to learn how to install Docker in Ubuntu 20.04 and Run Nginx container.<\/p>\n<p>Installing Docker in Ubuntu 20.04 is a simple and straightforward process. We need to update the Docker repository in Ubuntu, get the GPG key, and install docker packages and dependencies.<\/p>\n<h2>Prerequisites<\/h2>\n<ol>\n<li>Freshly installed Ubuntu 20.04<\/li>\n<li>Sudo privileged accounts to install packages.<\/li>\n<\/ol>\n<h2>Install Docker in Ubuntu<\/h2>\n<p>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.<\/p>\n<h3>Download Docker GPG Key<\/h3>\n<p>Run the following command to add GPG key.<\/p>\n<pre>$ curl -fsSL https:\/\/download.docker.com\/linux\/ubuntu\/gpg | sudo apt-key add -<\/pre>\n<h3>Add Docker GPG Key to System Repository<\/h3>\n<p>Add and configure the official docker repository in your system.<\/p>\n<pre>$ sudo add-apt-repository \"deb [arch=amd64] https:\/\/download.docker.com\/linux\/ubuntu focal stable\"<\/pre>\n<h3>Update System Repository<\/h3>\n<p>Now, update the APT package to include new Docker packages using the following command:<\/p>\n<pre>$ sudo apt update<\/pre>\n<h3>Install Docker<\/h3>\n<p>Now you can install docker packages using the following command:<\/p>\n<pre>$ sudo apt install docker-ce<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1023\" height=\"441\" class=\"wp-image-4678\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-339.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-339.png 1023w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-339-300x129.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-339-768x331.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-339-1020x441.png 1020w\" sizes=\"auto, (max-width: 1023px) 100vw, 1023px\" \/><\/p>\n<p>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.<\/p>\n<h3>Check Docker Status<\/h3>\n<p>To check docker service is active or not, run the following command:<\/p>\n<pre>$ sudo systemctl is-active docker<\/pre>\n<p>To check if the docker service is enabled or not, run the following command.<\/p>\n<pre>$ sudo systemctl is-enabled docker<\/pre>\n<p>To check the docker service status, run the following command:<\/p>\n<pre>$ sudo systemctl status docker<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1289\" height=\"401\" class=\"wp-image-4679\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-340.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-340.png 1289w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-340-300x93.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-340-1024x319.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-340-768x239.png 768w\" sizes=\"auto, (max-width: 1289px) 100vw, 1289px\" \/><\/p>\n<h3>Stop, Start or Restart Docker<\/h3>\n<p>There are other systemctl commands available to control the docker services which are as follows<\/p>\n<pre>$ sudo systemctl stop docker # stop docker service<\/pre>\n<pre>$ sudo systemctl start docker # start docker service<\/pre>\n<pre>$ sudo systemctl restart docker # restart docker service<\/pre>\n<h3>Check Docker Version<\/h3>\n<p>To check the version of docker run the following command:<\/p>\n<pre>$ docker version<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1288\" height=\"250\" class=\"wp-image-4680\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-341.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-341.png 1288w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-341-300x58.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-341-1024x199.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-341-768x149.png 768w\" sizes=\"auto, (max-width: 1288px) 100vw, 1288px\" \/><\/p>\n<h2>Run Nginx Container Using Docker<\/h2>\n<p>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.<\/p>\n<pre>$ sudo docker pull nginx<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1150\" height=\"163\" class=\"wp-image-4681\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-342.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-342.png 1150w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-342-300x43.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-342-1024x145.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-342-768x109.png 768w\" sizes=\"auto, (max-width: 1150px) 100vw, 1150px\" \/><\/p>\n<p>To list the docker images, run the command:<\/p>\n<pre>$ sudo docker images<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"895\" height=\"107\" class=\"wp-image-4682\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-343.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-343.png 895w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-343-300x36.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-343-768x92.png 768w\" sizes=\"auto, (max-width: 895px) 100vw, 895px\" \/><\/p>\n<p>To run a container from a pulled image, run the following command:<\/p>\n<pre>$ sudo docker run -d --name nginx-server -p 80:80 nginx<\/pre>\n<p>where,<\/p>\n<p>d=run the container in detached mode<\/p>\n<p>name= name of the container to be created<\/p>\n<p>p= port the container will be mapped with host<\/p>\n<p>You will have output similar as :<\/p>\n<pre>$ 7ef30a6599d0a7f9618883441fdd2a683e7205287d09f92dcd3b63f4892551e7<\/pre>\n<p>The output shows the container id created using the Nginx image.<\/p>\n<p>To list out the running container, run the command:<\/p>\n<pre>$ sudo docker ps -a<\/pre>\n<p>You can find containers with status in your terminal as:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1119\" height=\"130\" class=\"wp-image-4683\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-344.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-344.png 1119w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-344-300x35.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-344-1024x119.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-344-768x89.png 768w\" sizes=\"auto, (max-width: 1119px) 100vw, 1119px\" \/><\/p>\n<p>Nginx is running in docker containers with port 80. You can verify Nginx installation by navigating to the URL <a href=\"http:\/\/your-server-ip\">http:\/\/your-server-ip<\/a> in your browser.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1095\" height=\"371\" class=\"wp-image-4684\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-345.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-345.png 1095w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-345-300x102.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-345-1024x347.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-345-768x260.png 768w\" sizes=\"auto, (max-width: 1095px) 100vw, 1095px\" \/><\/p>\n<h2>Create Docker Volume for Nginx<\/h2>\n<p>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. In this example, I have taken nginx-data as a volume name. You can have your own assumption.<\/p>\n<p>To create a docker volume run the following command:<\/p>\n<pre>$ sudo docker volume create nginx-data<\/pre>\n<p>Get the docker volume information by running the command:<\/p>\n<pre>$ sudo docker volume inspect nginx-data<\/pre>\n<p>You will get the output similar as:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1075\" height=\"310\" class=\"wp-image-4685\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-346.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-346.png 1075w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-346-300x87.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-346-1024x295.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-346-768x221.png 768w\" sizes=\"auto, (max-width: 1075px) 100vw, 1075px\" \/><\/p>\n<p>For easy access, you can create a symlink of the docker volume directory. To create symlink run the following command:<\/p>\n<pre>$ ln -s \/var\/lib\/docker\/volumes\/nginx-data\/_data \/nginx<\/pre>\n<p>Now start the Nginx container with persistent data storage.<\/p>\n<pre>$ sudo docker run -d --name nginx-server -p 80:80 -v nginx-data:\/usr\/share\/nginx\/html nginx<\/pre>\n<p>Where,<\/p>\n<p>d= run container in detached mode<\/p>\n<p>name= name of the container to be created<\/p>\n<p>p= port to be mapped with host<\/p>\n<p>v= name of docker volume<\/p>\n<p>Container has been started with persistent data storage. You will get container id as output as:<\/p>\n<pre>$ 3067684b1133a2c7e36381574ea9af3ebbb79dd2504f63ae3569bb059b74d905<\/pre>\n<p>Now verify the contents available in the data persistent directory.<\/p>\n<pre>$ ls \/var\/lib\/docker\/volumes\/nginx-data\/_Data<\/pre>\n<p>The following output will be displayed in your terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"857\" height=\"75\" class=\"wp-image-4686\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-347.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-347.png 857w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-347-300x26.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-347-768x67.png 768w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/p>\n<p>Let&#8217;s change the content of index.html file located at \/var\/lib\/docker\/volumes\/nginx-data\/_data<\/p>\n<pre>$ sudo vi \/var\/lib\/docker\/volumes\/nginx-data\/_data\/index.html<\/pre>\n<p>Change some HTML code and save the file. Navigate the URL in your browser and you will find your Nginx contents changed as:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"923\" height=\"340\" class=\"wp-image-4687\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-348.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-348.png 923w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-348-300x111.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-348-768x283.png 768w\" sizes=\"auto, (max-width: 923px) 100vw, 923px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, you have learned how to install docker, pull docker images from docker hub and run an application in a container. Also, you have learned how to create persistent data storage and map with docker containers.<\/p>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>","protected":false},"author":31,"featured_media":4696,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[160,206,100],"class_list":["post-4677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-docker","tag-nginx-container","tag-ubuntu-20-04"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=4677"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4677\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/4696"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=4677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=4677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=4677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}