Use Docker to Run Nginx Web Server in Ubuntu! — Part II: Running and Managing Nginx

Jazz Lien
5 min readMay 28, 2024

--

Welcome back to the second part of our series on using Docker to run an Nginx web server in Ubuntu!

This is a two-series articles, in Part I, we covered the basics of Docker, from installation to running your first Nginx container. If you missed it, be sure to check it out:

Now that we have a solid foundation, it’s time to dive deeper. In this part, we’ll explore how to manage and configure our Nginx container, tailor it to our specific needs, and make use of advanced Docker commands.

In the previous article, we had a nginx container running, let’s run docker --help and we can see that in order to list containers, use:

docker ps

If we run docker ps, we only see one container, that’s because it only shows containers that are running, if we want to see all the containers, we add the -a option , run

docker ps -a

we can see the stopped containers are also listed.

To start or stop a container, use:

docker start <image-name>
docker stop <image-name>

Networking and Testing

With mywebserver running, test its connection with the Nginx server:

nc localhost 8080

And typeGET / to send a get request to the web server which should in return a web page and the index web page specifically in text format.

This confirms that our containerized Nginx works and the port is indeed published to our host.

Docker run vs. Docker Start

It can be a bit confusing about what’s the difference between these two commands.

The docker run command builds and starts a container from an image, while the docker start command only starts an existing container.

We can remember this way:

  • If the container doesn’t exist, use docker run to initialize it.
  • If it does, use docker start.

Managing Docker Images and Containers

In the first part, we learned that we could use docker rmi to remove an image. However, sometimes you might encounter conflicts because the image is being used.

To resolve this:

  • stop the container
  • remove the container
  • then remove the image

You can force delete an image with the -f option:

docker rmi -f <image-name>

After running docker images again, you should see that the image has been successfully removed.

Dockerfile and Building Custom Images

Let’s talk about building our own Docker image.

So far, we’ve worked with publicly available container images, like the Nginx image.

But in the real world, these generic images might not be enough, and we’ll need to build our own.

Create a new directory named myimage and a simple index.html file inside it

Next, tell Docker how to build this image.

The instructions should be in a file named Dockerfile (with a capital D).

Add the following text to the Dockerfile:

FROM nginx 
COPY index.html /usr/share/nginx/html/index.html

This tells Docker to use the nginx base image and copy our index.html file to a specific path inside the container, replacing the pre-existing file with our custom one.

To build our image, use:

docker build --tag student/customnginx:1.0 myimage

Here, myimage is the directory where we included our project files and Dockerfile.

After the image is built, run a container based on this image:

docker run --detach --publish 9000:80 --name mywebserver3 student/customnginx:1.0

Test the Nginx container by running:

nc localhost 9000 
GET /

Notice how Nginx returns the content we inserted in our index.html file.

This confirms that our modifications took effect and we successfully built our own image with a custom index page!

Conclusion

This tutorial covered a variety of Docker commands and how to use them in real-world scenarios. From setting up Docker and running basic commands to deploying and managing an Nginx web server, we’ve walked through the steps to containerize your applications. I hope you found this guide useful and feel confident using Docker to streamline your development process.

Happy containerizing!

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

AI | Azure | Cloud | Python | 5x Microsoft Azure Certified