InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Docker tutorials

  • Docker quick start guide on Mac
  • Docker quick start guide on Ubuntu
  • Docker how to add a user to group docker on Linux
  • Docker container - handy commands
  • How to get docker container IP
  • Run nginx in docker container
  • Ubuntu docker - storage driver aufs not supported error
  • How to find docker host ip on Mac
  • Docker - how to rerun a container with different flags
  • Docker managing images - command line cheat sheet
  • Install nginx and php in docker ubuntu container
 
  • Home
  • > Tutorials
  • > Docker

Run nginx in docker container

on Feb 5, 2016

Docker can be used to run nginx in a container. We’ll use Ubuntu Linux (14.04.2) for the purpose of this tutorial. This tutorial assumes that you have installed docker on your Ubuntu machine.

  1. Download latest nginx image

    $ docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    ...
    Status: Downloaded newer image for nginx:latest
    
  2. Run nginx server with name nginx1 and port 8081 mapped to contaner port 80.

    $ docker run --name nginx1 -p 8081:80 -d  nginx
    3cad33cde70b1a75ae37725fd35dea01cfe306d39991ba1d8ce730c4d92fa00a
    docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                           NAMES
    e301d1d599f0        nginx               "nginx -g 'daemon off"   2 seconds ago       Up 2 seconds        443/tcp, 0.0.0.0:8081->80/tcp   nginx1
    
  3. Check if nginx is listening on port 8081.
    $ curl http://localhost:8081/
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
  4. Link nginx document root to local directory.
    $ sudo mkdir -p /opt/tmp/nginx1/html
    $ sudo bash -c 'echo "foo" > /opt/tmp/nginx1/html/index.html'
    
    ## in case old container is running
    $ docker kill nginx1
    $ docker rm nginx1
    
    ##
    $ docker run --name nginx1 -p 8081:80 -d -v /opt/tmp/nginx1/log:/var/log/nginx  -v /opt/tmp/nginx1/html:/usr/share/nginx/html nginx
    $ curl http://localhost:8081/
    foo
    $ cat /opt/tmp/nginx1/log/access.log 
    172.17.0.1 - - [05/Feb/2016:05:05:41 +0000] "GET / HTTP/1.1" 200 4 "-" "curl/7.35.0" "-"
    
  5. Get an interactive bash shell in running nginx container
    $ docker exec -it nginx1 /bin/bash
    

Suggested posts:

  1. Chrome extension tutorial – access active page dom
  2. Docker container – handy commands
  3. Docker how to add a user to group docker on Linux
  4. node – how to find source file location of a module
  5. Mac kubernetes – create two container pod with shared volume
  6. LXC (Linux Containers) – quick start tutorial on Ubuntu
  7. Mac – create a one container pod and get shell to it
  8. How to use wildcard in robots.txt
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Docker, Linux, Linux/Unix Command Line, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress