Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Beginning DevOps with Docker

You're reading from   Beginning DevOps with Docker Automate the deployment of your environment with the power of the Docker toolchain

Arrow left icon
Product type Paperback
Published in May 2018
Publisher Packt
ISBN-13 9781789532401
Length 96 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Joseph Muli Joseph Muli
Author Profile Icon Joseph Muli
Joseph Muli
Arrow right icon
View More author details
Toc

Dockerfile Syntax

Every Docker image starts from a Dockerfile. To create an image of an application or script, simply create a file called Dockerfile.

Note

It does not have an extension and begins with a capital letter D.

A Dockerfile is a simple text document where all the commands that template a container are written. The Dockerfile always starts with a base image. It contains steps to create the application or to run the script in mind.

Before we build, let's take a quick look at a few best practices on writing Dockerfiles.

Some best practices include, but are not limited to, the following:

  • Separation of concern: Ensure each Dockerfile is, as much as possible, focused on one goal. This will make it so much easier to reuse in multiple applications.
  • Avoid unnecessary installations: This will reduce complexity and make the image and container compact enough.
  • Reuse already built images: There are several built and versioned images on Docker Hub; thus, instead of implementing an already existing image, it's highly advisable to reuse by importing.
  • Have a limited number of layers: A minimal number of layers will allow one to have a compact or smaller build. Memory is a key factor to consider when building images and containers, because this also affects the consumers of the image, or the clients.

We'll start simply with a Python and JavaScript script. The choice of these languages is based on their popularity and ease of demonstration.

Writing Dockerfiles for Python and JavaScript examples

Note

No prior experience is required on the selected languages as theyare meant to give a dynamic view of how any language can adopt containerization.

Python

Before we begin, create a new directory or folder; let's use this as our workspace.

Open the directory and run docker search python. We'll pick the official image: python. The official image has the value [OK] in the OFFICIAL column:

Python

Go to hub.docker.com or store.docker.com and search for python to get the correct tag or at least know what version the Python image with the latest tag is. We will talk more about tags in Topic D.

The image tag should be the number with this syntax that looks like 3.x.x or 3.x.x-rc.

Create a file by the name run.py and enter the first line as follows:

print("Hello Docker - PY")

Create a new file on the same folder level and name it Dockerfile.

Note

We do not have an extension for the Dockerfile.

Add the following in the Dockerfile:

FROM python
ADD . .
RUN ls
CMD python run.py

The FROM command, as alluded to earlier, specifies the base image.

The command can also be used on an inheritance point of view. This means you do not have to include extra package installations in the Dockerfile if there already exists an image with the packages.

The ADD command copies the specified files at source to the destination within the image's filesystem. This means the contents of the script will be copied to the directory specified.

In this case because run.py and Dockerfile are on the same level then run.py is copied to the working directory of the base image's file system that we are building upon.

The RUN command is executed while the image is being built. ls being run here is simply for us to see the contents of the image's filesystem.

The CMD command is used when a container is run based on the image we'll create using this Dockerfile. That means at the end of the Dockerfile execution, we are intending to run a container.

JavaScript

Exit the previous directory and create a new one. This one will be demonstrating a node application.

Add the following line in the script and save:

console.log("Hello Docker - JS")

Run docker search node - we'll pick the official image: node

Remember that the official image has the value [OK] in the OFFICIAL column:

JavaScript

Note that node is the JavaScript runtime based on Google's high performance, open source JavaScript engine, V8.

Go to hub.docker.com and search for node to get the correct tag or at least know what version the node image with the latest tag is.

Create a new Dockerfile and add the following:

This should be on the same file level as the script.

FROM node
ADD . .
RUN ls
CMD node run.js

We'll cover these for now.

Activity 3 — Building the Dockerfile

Ensure you have the Docker CLI running by typing docker on your terminal.

To get you conversant with Dockerfile syntax. The goal of this activity is to help understand and practice working with third-party images and containers. This helps get a bigger picture on how collaboration can still be affected through containerization. This increases product delivery pace by building features or resources that already exist.

You have been asked to write a simple Dockerfile that prints hello-world.

  1. Is Docker up and running? Type docker on the terminal or command-line application.
  2. Create a new directory and create a new Dockerfile.
  3. Write a Dockerfile that includes the following steps:
    FROM ubuntu:xenial 
    RUN apt-get install -y apt-transport-https curl software-properties-common python-software-properties
    RUN curl -fsSL https://apt.dockerproject.org/gpg | apt-key add 
    RUN echo 'deb https://apt.dockerproject.org/repo ubuntu-xenial main' > /etc/apt/sources.list.d/docker.list
    RUN apt-get update
    RUN apt-get install -y python3-pip
    RUN apt-get install -y build-essential libssl-dev libffi-dev python-dev
    
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image