August 12, 2017

Building an Nginx Image

Let's see how we created our Nginx image and saved to Docker Hub under the Shipping Docker namespace.

We cover how I built the Nginx image we used in our Docker setup.

Note that the video, as usual, is more comprehensive than these notes!

The Dockerfile

We use a Dockerfile to define a base image, and the steps to take to use that base image and create our image from it. We can then use that built image and upload it to a container Registry, such as Docker Hub. Or we can use it locally. Either way, the image can be used to spin up new containers.

Here is the Dockerfile for our Nginx container:

FROM ubuntu:16.04

MAINTAINER Chris Fidao

RUN apt-get update \
    && apt-get install -y nginx \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    && echo "daemon off;" >> /etc/nginx/nginx.conf

ADD default /etc/nginx/sites-available/default

EXPOSE 80
CMD ["nginx"]

Build and Push

To build this image, we run the following from the same directory that the Dockerfile resides in. This will the image once, and create (or overwrite!) two new tags:

  • latest tag (by convention, points to the latest build, and is the default tag Docker will look for)
  • 1.0 tag (any other arbitrary tag you'd like to use)

Here's the command to build an image and apply two tags to that build:

docker build -t shippingdocker/nginx:1.0 -t shippingdocker/nginx:latest

To push it to Docker Hub, we can login to our account (made previously) and push our images up to it:

# Log into Docker Hub from our CLI client
docker login

# Push up each tagged image
docker push shippingdocker/nginx:1.0
docker push shippingdocker/nginx:latest

Looking for a deeper dive into Docker?

Sign up here to get a preview of the Shipping Docker course! Learn how to integrate Docker into your applications and develop a workflow to make using Docker a breeze!

All Topics