Simplifying Docker
Deploying Nginx within a docker container
This article gives an overview of Docker and explains how to create docker containers and deploy an nginx image as a docker container.
What are containers?
Containers help us to pack all the project code, configuration settings and dependencies into an individual unit so that it can run easily and quickly in any environment. One of the popular tools used to build containers is Docker.
Docker is a tool that makes it easier to create, deploy and run applications by using containers. It is a lightweight alternative to virtual machines. With Docker, you can containerize your application and create a docker image. A docker image is a portable, read-only, executable file that tells docker how to create a container. You can run the docker image to create as many containers as you want.
When working with container images, you need a place to store and access the docker images. Docker Hub makes this possible. Docker images can be shared publicly or privately on Docker Hub which is a container registry that lets you store and distribute container images.
Getting started with Docker
- Installing Docker Desktop
To install Docker desktop for windows, download Docker Desktop Installer here. Follow the instructions in the wizard and proceed with the installation. Once complete, open Docker Desktop. - Open the terminal and execute the following command
docker info
If docker is installed successfully you will see a lot of information about docker now. - Pulling the image
Now we will pull the Nginx image from docker hub. In the terminal typedocker pull nginx
. It will download the latest version of the Nginx image if it is not locally available. - Running the container
Now to run the Nginx image and expose the docker container port to the network port we will use the following commanddocker run --name nginx-image -p 8082:80 -d nginx
In the above command,run
instructs docker to run the image as container,--name
specifies that the name of our container is nginx-image,-p 8082:80
instructs docker how we want to expose the port in the format host port: container port (expose port 8082 on host to port 80 on container),-d
stands for detached mode (runs container in the background),nginx
tells docker to use the Nginx image.
Now we have an instance of Nginx running locally as a docker container. Go to localhost:8082 in your web browser and you will see the “Welcome to Nginx” page displayed on your browser.
5. To list all the docker containers you have running, execute the following command docker ps -a
. You will see the Nginx container listed with container ID, image, status, port, etc.
Alternatively, you will be able to see a running container on Docker Desktop.
6. We can use the following commands to stop and remove a docker container docker stop <container_name>
and docker rm <container_name>
. The container will be stopped first and then removed.
You can execute docker ps -a
and see that our container is no longer listed because it has been removed.
Summing up
In this article, we have gone through the basics of Docker and deployed an Nginx image within a docker container.
Hoping this article was helpful for you. Any corrections or improvements to this article are welcome.
The next part of this article will cover deployment in a Kubernetes environment using Docker Desktop.
References
1. https://docs.docker.com/get-started/overview/
2. https://www.techrepublic.com/article/how-to-run-nginx-as-a-docker-container/