Docker Basics : Running a Nginx in a Container


To run a nginx web server in a container on Mac OS.


  1. Install VirtualBox
  2. Install Docker and boot2docker
  3. Initialize boot2docker. This is done once by running:
$boot2docker init


Step 1

Start boot2docker

$boot2docker up

You will see:

Waiting for VM and Docker daemon to start...
Writing /Users/zepho/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/zepho/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/zepho/.boot2docker/certs/boot2docker-vm/key.pem

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://
    export DOCKER_CERT_PATH=/Users/zepho/.boot2docker/certs/boot2docker-vm
    export DOCKER_TLS_VERIFY=1

Step 2

Set environment variables:

$ export DOCKER_HOST=tcp://
$ export DOCKER_CERT_PATH=/Users/zepho/.boot2docker/certs/boot2docker-vm

Step 3

$ docker info

You will see:

Containers: 6
Images: 23
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 35
 Dirperm1 Supported: true
Execution Driver: native-0.2
Kernel Version: 4.0.3-boot2docker
Operating System: Boot2Docker 1.6.2 (TCL 5.4); master : 4534e65 - Wed May 13 21:24:28 UTC 2015
CPUs: 8
Total Memory: 997.3 MiB
Name: boot2docker
Debug mode (server): true
Debug mode (client): false
Fds: 12
Goroutines: 17
System Time: Sat Jun 13 23:37:26 UTC 2015
EventsListeners: 0
Init SHA1: 7f9c6798b022e64f04d2aff8c75cbf38a2779493
Init Path: /usr/local/bin/docker
Docker Root Dir: /mnt/sda1/var/lib/docker
$ docker run -d -P --name web nginx
FATA[0000] Error response from daemon: Conflict. The name "web" is already in use by container 603f6666054b. You have to delete (or rename) that container to be able to reuse that name. 
$ docker run -d -P --name web2 nginx

Step 4

$docker ps

You will see:

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                           NAMES
4e7ef0695ace        nginx:latest        "nginx -g 'daemon of   8 seconds ago       Up 7 seconds>80/tcp,>443/tcp   web2                

Step 5

Looking at the above output, if we try:


We get:

curl: (7) couldn't connect to host

Step 6

So, let's find the IP address:

$boot2docker ip

You will see:

Step 7

Run :


You will see:

<!DOCTYPE html>
<title>Welcome to nginx!</title>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
<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=""></a>.<br/>
Commercial support is available at
<a href=""></a>.</p>

<p><em>Thank you for using nginx.</em></p>

Browse to You will the Welcome to nginx! static page.

Step 8

Stop the container:

$docker stop web2

You will see: web2

Remove the container:

$docker rm web2

You will see: web2

Note that since I already have a web container called 'web' running, I had to use web2 for this article.


How to Use Docker on OS X: The Missing Guide

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.