Install Docker on Ubuntu 18.04 and Deploy an NGINX Container

Use containers to easily host a website

Inside view of a typical data center

Erik Isakson / Getty Images

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.


The installation of Docker on Ubuntu 18.04 is incredibly easy. Open up a terminal window (or log into your Ubuntu server via SSH).

  1. 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 update
    sudo apt-get upgrade
  2. 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

    Installing Docker, via the command line, on Ubuntu Server
  3. Once that command completes, start and enable Docker with the commands:

    sudo systemctl start docker
    sudo systemctl enable docker

    We enable Docker so it will automatically start, should the server be rebooted.

  4. 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.
  5. 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).

  1. 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 
    Listing Docker images via the command line

    The output of that command should show no current images to be found.

  2. Pull down the NGINX image with the command:

    docker pull nginx
  3. The above command will pull down the image. If you issue the command docker images, you’ll see the newly pulled image listed.

    The NGINX image now appears in our Docker image list

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.

  1. 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 command
    • run 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.
  2. 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.

    The NGINX welcome screen shows our container has been successfully deployed
  3. 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.

  4. 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
    Docker cannot deploy a duplicate container with the same name
  5. 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
  6. 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.