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 September 16, 2020 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. This article walks you through the process of installing one of the most popular container tools on the market, Docker and then deploying the NGINX web server. Installation The installation of Docker on Ubuntu 18.04 is incredibly easy. Open a terminal window (or log into your Ubuntu server via SSH). Before you install Docker, it’s best to update and upgrade your server. Remember, however, that if the upgrade includes the kernel, you need to reboot the server so the changes take effect. 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, if needed), you can then install Docker by issuing the following command: sudo apt-get install docker.io After that command completes, start and enable Docker with the commands: sudo systemctl start dockersudo systemctl enable docker This enables Docker so it automatically starts, should the server be rebooted. Next, add your user to the docker group. Otherwise, you 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 you are appending to the user.The G options instructs usermod you are adding the user to a group.docker is the group you are adding the user to.$USER instructs bash to use the currently logged in user. After running the above command, 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 the goal is to deploy an NGINX container, pull down the official NGINX container. Before you 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 pulls down the image. If you issue the command docker images, you’ll see the newly pulled image listed. Deploying the NGINX Container Now that you have an image with which to base the container, deploy the containerized instance of NGINX onto your network. With NGINX as a container, you can control what port it is deployed on. 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 the 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? You ran the command in attached mode. If you hit the Ctrl+c key combination, the container is killed, and you have the 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 you've already deployed the first container, you’ll see an error. To avoid this, you 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 you remove the container with the command: docker rm f149 The original container is now stopped and has been removed. You can then successfully redeploy the NGINX container in detached mode.