Run an Nginx container
In this step, we will learn how to run a simple Nginx web server inside a Docker container. Docker allows us to package applications and their dependencies into containers, ensuring they run consistently across different environments.
First, we need to make sure the Nginx image is available on our system. We can pull the official Nginx image from Docker Hub using the docker pull
command. This command downloads the image to your local machine.
docker pull nginx:latest
You should see output indicating that the image is being downloaded. Once the download is complete, you can verify that the image is available by listing the images on your system:
docker images
You should see nginx
listed in the output.
Now, let's run the Nginx container. We will use the docker run
command. The -d
flag runs the container in detached mode (in the background), and the -p 80:80
flag maps port 80 on our host machine to port 80 inside the container. This allows us to access the Nginx web server from our host browser. We also give the container a name using --name my-nginx-container
for easier identification.
docker run -d -p 80:80 --name my-nginx-container nginx
After running the command, Docker will output a long string, which is the container ID. This indicates that the container has started successfully in the background.
To verify that the container is running, you can use the docker ps
command, which lists all running containers:
docker ps
You should see my-nginx-container
listed with a status of Up
.
Finally, let's access the Nginx web server from our host machine. Since we mapped port 80, we can use curl
to make an HTTP request to localhost
on port 80.
curl localhost
You should see the default Nginx welcome page HTML in the output. This confirms that the Nginx container is running and accessible.