Web design company India | Custom web development company India

Setting up Nginx container using Docker

  • Setting up Nginx container using Docker

    Why nginx on docker?

    When you are building multiple applications in parallel from your local dev environment (host), it is hard to switch the environment configurations based on application requirement and its hard to build all the applications using the same environment configuration too :).

    For example, if you are working on a application which is running on an older version of nginx and you are starting to build another application which will require the latest version of nginx. In this case, switching between nginx version will be a headache and may end up in unexpected issues which will kill your valuable time and resource.

    Using docker we can switch the development environments easily without making any configuration changes on your host machine.


    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 in 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"
                context: ./nginx
                - "80:80"
                - ../:/var/www/myapp
                - ./nginx/config/myapp.conf:/etc/nginx/conf.d/myapp.conf
        driver: bridge
          - subnet:
    Not sure what's meant by the above content? Don't worry you can find the explanation of each keywords used here at Docker compose file reference

    Step 3: Create 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

    Step 3: 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 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 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 as in the above sample or feel free to make necessary changes to the docker-compose.yml file.

    Step 4: Modify your hosts file

    In order to point the domain "www.myapp-local.com" to the nginx container (IP:, you will need to modify your "/etc/hosts" file and add below record. For Mac users
    $	www.myapp-local.com myapp-local.com
    For Other Operating system users
    $	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