How to use docker to build a web server with nginx in 2 minutes

, , Leave a comment

Here’s a tutorial on how to use the docker exec command to manage containers running inside of a Docker container. In this example, we will be using an Nginx webserver running on port 8080.

  1. First, start your Nginx container by running the following command:
docker run -d --name my-nginx -p 8080:80 nginx

This will create a new container named «my-nginx» that is running the Nginx image and mapping port 8080 on the host machine to port 80 on the container.

  1. Now that you have started the container, you can check if it’s up and running by running the following command:
docker ps

This should show you a list of all the running containers on your system, including their names, images, ports, and status. You should see the «my-nginx» container listed there.

  1. To access the Nginx webserver running inside the container, you can use the docker exec command to execute commands within the container. For example, to view the default Nginx welcome page at http://localhost/index.html, you could run the following command:
docker exec -it my-nginx bash

This will open a terminal inside the container and give you access to the command line. From there, you can navigate to the root directory of the Nginx configuration files (usually located at /etc/nginx) and then visit the default welcome page by accessing http://localhost/index.html.

  1. Alternatively, you can also use the docker exec command to directly access the Nginx webserver without opening a terminal first. This can be done by running the following command:
docker exec -it my-nginx nginx -p 8080

This will start the Nginx service inside the container and map it to port 8080 on the host machine. Once the service is running, you can access the webserver by visiting http://localhost:8080 in your web browser.

  1. Finally, when you are finished working with the container, don’t forget to exit the container by running the exit command or closing the terminal window. If you leave the container running, it will continue consuming resources until you manually stop it.

That’s it! With these simple steps, you can easily manage and interact with containers running inside of a Docker environment.