Bootstrap FreeKB - Nginx (Web Server) - Install Nginx on Docker
Nginx (Web Server) - Install Nginx on Docker

Updated:   |  Nginx (Web Server) articles

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).


The docker pull command can be used to pull down the latest nginx image.

~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
b380bbd43752: Pull complete
fca7e12d1754: Pull complete
745ab57616cb: Pull complete
a4723e260b6f: Pull complete
1c84ebdff681: Pull complete
858292fd2e56: Pull complete
Digest: sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36
Status: Downloaded newer image for nginx:latest


Or you could create Dockerfile so that the Dockerfile contains something like this.

FROM nginx:latest


Then use the docker build command to create the image, running this command in the same directory as the Dockerfile.

docker build . --tag nginx:latest


The docker images command can be used to display the nginx image.

~]# docker images
nginx        latest       87a94228f133   3 weeks ago   133MB


The following command can then be used to create and start the ngninx container. Let's break down this command.

  • The docker run command is used to create and start the nginx container.
  • The --detach flag is used to run the container in the background.
  • The --publish option is used both the Docker server and nginx container to listen on HTTP port 80, which adds a rule to iptables to allow connections between the Docker system and container on port 80.
  • The --name option is used to name the container nginx.
  • The --restart unless-stopped option is used so that the container is started if the Docker server is restarted
  • The --env option is use to set the container to use a specific time zone instead of UTC so that entries in the logs match the local time zone. This of course is optional.
  • The nginx image is used.
docker run --detach --publish 80:80 --name nginx --env TZ=Americas/Chicago --restart unless-stopped nginx


The docker container ls command can be used to ensure the container is running.

~]# docker container ls -a
CONTAINER ID   IMAGE  COMMAND                 CREATED         STATUS        PORTS                                      
3afc316a7c2c   nginx  "/docker-entrypoint.…"   5 seconds ago  Up 3 seconds>80/tcp, :::80->80/tcp  nginx


The docker logs command should return something like this.

~]# docker logs nginx
/ /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/ Looking for shell scripts in /docker-entrypoint.d/
/ Launching /docker-entrypoint.d/ info: Getting the checksum of /etc/nginx/conf.d/default.conf info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/ Launching /docker-entrypoint.d/
/ Launching /docker-entrypoint.d/
/ Configuration complete; ready for start up
2021/11/08 08:02:00 [notice] 1#1: using the "epoll" event method
2021/11/08 08:02:00 [notice] 1#1: nginx/1.21.3
2021/11/08 08:02:00 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/11/08 08:02:00 [notice] 1#1: OS: Linux 5.11.12-300.fc34.x86_64
2021/11/08 08:02:00 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1024:1024
2021/11/08 08:02:00 [notice] 1#1: start worker processes
2021/11/08 08:02:00 [notice] 1#1: start worker process 33


If running Docker on a Linux system, ensure connections are allowed in firewalld or iptables on ports 80 and 443.

firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload


If running Docker on an Amazon Web Services (AWS) EC2 Instance, ensure the Security Group Inbound Rules allow connections on ports 80 and 443.


You should then be able to access the default Nginx welcome page at http://<hostname or IP address of your Docker system>. Something like this should be displayed.


Almost always, you are going to want to use your own default.conf file. The --volume option can be used.

docker run --detach --publish 80:80 --publish 443:443 --name nginx --restart unless-stopped --volume /path/to/default.conf:/etc/nginx/conf.d/default.conf nginx


Then in the /etc/nginx/conf.d/default.conf file, you could define server_name.

server {
    listen       80;


Assuming the server name is being resolved by DNS to the IP address of your Docker system, you should now be able to access the Nginx welcome page using the server name.


And you should also be able to display the 50x.html page.


Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

Please enter 328e42 in the box below so that we can be sure you are a human.