How to setup Nginx container using Docker?

By using docker you can run the Nginx container easily by switching the development environment without making any configuration changes on the host machine.
When multiple application is built parallelly from a local development environment, it is hard to switch the environment configurations based on application requirement and build all the applications using the same environment configuration.
Docker nginx example, if you are working on with an older version of Nginx and needed to start another application which requires an upgraded version of Nginx, at that point of time you need to Switch the Nginx version, it is not that too easy and unexpected issues may also occur, which can draw back your resource and time.
Now Docker helps to come up with this scenario.
Almost 90% of Magento Development Company across the globe use Docker as a platform for their Magento Development Services

Nginx docker-compose is mainly used by developers as it is cost-efficient to build and manage your application portfolio in own pace.
now let's look into the steps involved in setting up Nginx Container using Docker.

Prerequisites

A working version of docker community edition software is installed on your machine. If not, please find the guide on How to install docker Docker-compose is installed on your host machine. Learn How to install docker-compose "docker" command should be able to run without "sudo". If not follow learn how to configure Docker to run without sudo

Readiness check

Run below command on your host machine's terminal:

docker-compose -v

Once you complete the run command
It shows the result like this " docker-compose version 1.22.0, build f46880f"
If you are troubled in readiness check please troubleshoot the docker installation.
Let's start creating a container using the simple steps
Step 1:
Now we are going to create a directory for adding docker files to your host machine.
Note: we request you to preferably try in your project path itself,
also please make sure that you are updating your .gitignore file to avoid accident commits.

$ cd /path/to/your/project/ $ mkdir docker $ cd docker

Step 2:
Create "docker-compose.xml" file the docker directory. Add below content to the yml file.

version: "3" services: nginx_myapp: build: context: ./nginx ports: - "80:80" volumes: - ../:/var/www/myapp - ./nginx/config/myapp.conf:/etc/nginx/conf.d/myapp.conf networks: myappnetwork: ipv4_address: 172.25.0.1 networks: myappnetwork: driver: bridge ipam: config: - subnet: 172.25.0.0/24

Not sure what's meant by the above content? Don't worry you can find the explanation of each keyword used here at Docker compose file reference
Step 3: Create a Docker file
You will need to create a new directory "nginx" inside the docker directory.

$ mkdir nginx

Now navigate to nginx directory and create a new file "Dockerfile" - as you see without any file extension.

$ touch Dockerfile

Add below content to the Dockerfile.

FROM nginx:1.9

Write nginx configuration
Now you will need to write the vhost configuration to make your website visible with a dummy domain name (something like www.myapp-local.com).
In order to write nginx configuration, create "my app.conf" file in "docker/nginx/config/" directory.

$ mkdir config $ cd config $ touch myapp.conf

Then add below vhost configuration to a myapp.conf file using your favorite editor.

server { listen 80; server_name www.myapp-local.com; root /var/www/myapp; }

Make sure that the file names you used are in the above sample or feel free to make necessary changes to the docker-compose.yml file.
Step 4: Modify your host file
In order to point the domain "www.myapp-local.com" to the nginx container (IP: 172.25.0.1), you will need to modify your "/etc/hosts" file and add below the record. For Mac users

$ 127.0.0.1 www.myapp-local.com myapp-local.com

For Other Operating system users

$ 172.25.0.1 www.myapp-local.com myapp-local.com

Step 5: Write an HTML file
Switch to your project directory (assuming that you created "docker" in your project directory) and then create "index.html".

$ cd /path/to/your/project/ $ touch index.html

Add below content to index.html
My Nginx Container!
Step 6: Execute docker-compose
Now you are all set to run the docker-compose command to build your nginx container.
Execute below commands.
Make sure If you are into the "docker" directory, else switch to that directory and run below command.

$ docker-compose up

As I mentioned above if you are facing any errors while running the command, Please do a troubleshoot
You can now check your domain to see whether the request is reaching the right container.
Open your favorite browser and access http://www.myapp-local.com/

From this tutorial you have learned how to setup Nginx Container using Docker if you have any doubt place your comments.

Leave a Reply

Your email address will not be published. Required fields are marked *