Use Docker to Run Nginx Web Server in Ubuntu! — Part I: Getting Started with Docker

Jazz Lien
6 min readMay 27, 2024

--

In today’s fast-paced tech world, efficiency and consistency are key to successful software development and deployment. Docker, a leading containerization platform, has revolutionized the way developers build, ship, and run applications.

If you’re new to Docker or looking to solidify your understanding, you’re in the right place.

What is Docker?

Docker is a powerful tool designed to make it easier to create, deploy, and run applications by using containers.

But what exactly is a container? Imagine you have a set of items you need to ship: different pieces of furniture, appliances, and boxes of personal belongings. Instead of shipping each item separately, you pack everything into a single container. This container ensures all your items are kept together, safe, and can be moved easily regardless of where they are shipped.

Similarly, in the world of software, a Docker container packages all the parts needed to run a software application, including the code, libraries, dependencies, and system tools. This container can run on any machine, making it incredibly versatile and efficient. Whether you’re running your application on your local computer, a server, or in the cloud, Docker ensures it runs smoothly every time.

I recommend watching this video for a quick overview of what Docker is:

In this two-part guide, we will start with the basics of setting up Docker and understanding its fundamental commands. By the end, you’ll be ready to deploy and manage an Nginx web server using Docker in Ubuntu, making your development process faster and more reliable.

Let’s dive in!

Install and Enable Docker in Linux

Let’s start by installing Docker on our virtual machine. Run the following command to install Docker:

sudo apt install docker.io

After the installation is complete, enable the Docker service and check the status of the Docker service to ensure it’s running:

sudo systemctl enable docker
sudo systemctl status docke

You should see a status message indicating that Docker is active (running).

Now that Docker is up and running, let’s pull the first quickstart image by executing:

sudo docker run hello-world

If you see the message Hello from Docker!, congratulations!

Docker has been successfully installed and is ready to use.

Add User to Docker Group

Notice that if we try to run Docker commands without sudo, we get a "permission denied" error. This is because the current user is not in the Docker group. To run Docker commands without needing to add sudo every time, we need to add the user to the Docker group.

First, add the docker group if it doesn't already exist:

sudo groupadd docker

Next, add your user (who has root privileges) to the Docker group:

sudo usermod -aG docker $USER

As the current user, we can run groups to see that we are indeed in the docker group, and we can run docker commands without adding sudo.

Docker Commands Introduction

Now that we have Docker up and running, let’s look at how to use it!

There are a lot of commands in Docker. The first place to find them is:

docker --help

Docker Search and Pull

Let’s see how to find the images we need to build our Nginx web server.

Use docker search to find the images:

Let’s run:

docker search enginx

The first one on the list, with almost twenty thousand stars, is exactly what we want for this demo!

Download the image by running:

docker pull nginx

You can also pull other images you like, for example:

docker pull ubuntu/nginx

Now that we have the images, check them with:

docker images

The images we pulled have the “latest” tag, which is used to differentiate image variations.

The default is “latest,” but we can set it to the desired version.

You can specify the version with:

docker pull nginx:1.22.1

And check it with docker images we see that we pull a nginx with tag 1.22.1 successfully.

To remove an image, use: docker rmi nginx:1.22.1

Use docker images , we can see that nginx:1.22.1 is no longer there!

Running Nginx Container

Now that we have the nginx image, let’s run it:

docker run nginx

  • If the terminal gets stuck, fix it by running:

docker run --detach --publish 8080:80 --name mywebserver nginx

The parameters are:

  • -detach: Detach from the container.
  • -publish 8080:80: Redirect network traffic from port 8080 to port 80 of the container.
  • -name mywebserver: Name the container.
  • nginx: The image to use.

If you see a unique identifier, congratulations, you have a running Nginx container!

By following these steps, we have

  1. Successfully installed Docker
  2. Added our user to the Docker group
  3. Pulled the Nginx image
  4. Run our first Nginx container

This setup ensures that your Docker environment is ready and capable of running web servers.

But we’re just getting started.

In Part II, we’ll dive deeper into managing our Nginx container, configuring it to suit our needs, and exploring more advanced Docker commands to optimize our web server setup.

Be sure to check out the next part of this series, where we’ll take our Docker skills to the next level!

Thank you so much for reading my article, before you go, please

  1. Clap 50 times for this story 👏👏👏
  2. Leave a comment telling me your thoughts
  3. Highlight your favourite part of the story

**Thank you! These tiny actions go a long way, and I really appreciate it!**😃

--

--

Jazz Lien

Solution Architect | 5x Google Cloud Certified | 5x Microsoft Azure Certified