Software & Apps Linux Install Docker on Ubuntu 18.04 and Deploy an NGINX Container Use containers to easily host a website by Jack Wallen Writer Jack Wallen is a former Lifewire writer, an award-winning writer for TechRepublic and Linux.com, and the voice of The Android Expert. our editorial process LinkedIn Jack Wallen Updated on February 23, 2020 Erik Isakson / Getty Images Linux Switching from Windows Tweet Share Email Containers are a great way to deploy isolated instances of services and apps. With these containerized tools, you can easily move them from one server to another, update them quickly, and even deploy them onto a cluster for large scalability. We’re going to walk you through the process of installing one of the most popular container tools on the market, Docker, and then deploy the NGINX web server. Installation The installation of Docker on Ubuntu 18.04 is incredibly easy. Open up a terminal window (or log into your Ubuntu server via SSH). Before you install Docker, it’s best to update and upgrade your server. Do remember, however, that if the upgrade includes the kernel, you’ll need to reboot the server so the changes will take affect. Because of this, run the update/upgrade commands during a time when a server reboot is possible. The commands for updating and upgrading the Ubuntu Server are: sudo apt-get updatesudo apt-get upgrade When those two commands complete (and you’ve rebooted, should it be needed), you can then install Docker by issuing the following command: sudo apt-get install docker.io Once that command completes, start and enable Docker with the commands: sudo systemctl start dockersudo systemctl enable docker We enable Docker so it will automatically start, should the server be rebooted. Next you’ll want to add your user to the docker group (otherwise, you’ll have to run docker commands with sudo privileges, which can lead to security issues). This can be taken care of with a single command: sudo usermod -aG docker $USER In case you’re curious, the following explains the above command: usermod is the actual command to modify a user.The a option instructs usermod we are appending to the user.The G options instructs usermod we are adding the user to a group.docker is the group we are adding the user to.$USER instructs bash to use the currently logged in user. After running the above command, you must log out and log back in for the changes to take effect. You are now ready to start working with Docker. Pulling Images From DockerHub DockerHub is a centralized repository that houses images that can be used as the basis for your containers. DockerHub contains thousands of images, some of which serve very specific (and even niche-y purposes). Since our goal is to deploy an NGINX container, we’re going to pull down the official NGINX container. Before we do that, issue the command: docker images The output of that command should show no current images to be found. Pull down the NGINX image with the command: docker pull nginx The above command will pull down the image. If you issue the command docker images, you’ll see the newly pulled image listed. Deploying the NGINX Container Now that we have an image with which to base our container, we’re going to deploy the containerized instance of NGINX onto our network. With NGINX as a container, you can control what port it is deployed on. We’ll deploy NGINX on the standard port 80. To make this happen, the command looks like: docker run --name docker-nginx -p 80:80 nginx A little explanation of the above command: docker is the actual commandrun instructs the command that what follows is to be run with the command.--name gives our container a human-readable name.-p instructs Docker what ports will be used.80:80 - The first 80 is the external port (as used on your network) and the second 80 is the internal port (as used by Docker).nginx is the image to be used for the container. After you deploy that container, you should be able to point a browser to http://SERVER_IP (where Server IP is the IP address of the server hosting Docker) and see the NGINX welcome page. You will also notice that you don’t get your command prompt back. Why? We ran the command in attached mode. If you hit the Ctrl+c key combination, the container will be killed and you’ll have your prompt back. If you want to keep the container running, and get your prompt back, you must deploy the container in detached mode, like so: docker run --name docker-nginx -p 80:80 -d nginx However, since we’ve already deployed the first container, you’ll see an error. To avoid this, we must stop the first container with the docker stop command. In order to do this, you must take note of the container name (it’ll be printed out in the error), which is a long string of random characters. To run this command, you only have to use the first four characters of the container, like so: docker stop f149 Next we have to remove the container with the command: docker rm f149 The original container is now stopped and has been removed. You can then successfully re-deploy the NGINX container in detached mode.