Web design company India | Custom web development company India
open

The 6-Simple Step to Set-up Nginx Container Using Docker?

  • Today we are going to show you the 6 simple steps to set up the nginx container using docker.

    Before stepping into the simple steps, let's have a short intro about the purpose of this feature.

    When multiple application is built parallelly from a local development environment (which we call Host),

    it is hard to switch the environment configurations based on application requirement and build all the applications using the same environment configuration.

    For example, you are working on with an older version of Nginx and needed to start another application which requires an upgraded version of Nginx,

    now this situation will become somewhat difficult.

    Switching between the Nginx version 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.

    Using this you can easily switch the development environment without making any configuration changes on the host machine.

    By the beginning of 2019, almost 90% of Magento Development Companies across the globe use Docker as a platform for their Magento Development Services.

    Setting up Nginx container using Docker

    Docker is mainly used by developers as it is cost-efficient to build and manage your application portfolio in own pace.

    As you have read how Docker compose Nginx, now let's look into the steps involved in setting up Nginx Container using Docker.

    (Step-by-Step)

    Prerequisites

    1. A working version of docker community edition software is installed on your machine. If not, please find the guide on How to install docker
    2. Docker compose is installed on your host machine. Learn How to install docker compose
    3. "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 

    If you see an output like below, then you are ready to proceed. If not, Troubleshoot your docker installation.

    docker-compose version 1.22.0, build f46880f 

    Create your container

    Step 1: Create your docker directory

    Create a directory for adding docker files to your host machine, preferably in your project path itself.

    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.yml

    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.

    If you are not inside "docker" directory, switch to that directory and run below command.

    $ docker-compose up 

    If you encounter any errors while running this command you can Troubleshoot your docker installation

    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/



  • Post Comments

    Submit Comment




    * Required Fields