Dockerfile 101

Luis Hernandez

Developer

lhernandez@nearsoft.com

Docker

Is a tool designed to make it easier to create, deploy, and run applications by using containers.

Images & Containers

In your images you will define the schema to run your containers, if you have OOP background, a reference could be that an image would be a class and a container would be the instance of the class.

Container 1

Container 3

Container 2

Image

Define your configs.

Dockerfile

 text document that contains all the commands a user could call on the command line to assemble an image

FROM

initializes a new build stage and sets the base image for subsequent instructions

run

  • RUN <command> (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
  • RUN ["executable", "param1", "param2"]

cmd

  • CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  • CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)

cmd USage

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as w

expose

  • EXPOSE <port> [<port>/<protocol>...]

 add & copy

  • ADD/COPY <src>... <dest>
  • ADD/COPY ["<src>",... "<dest>"] (this form is required for paths containing whitespace)

The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>

entrypoint

Allows you to configure a container that will run as an executable.

  • ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
  • ENTRYPOINT command param1 param2 (shell form)

VOLUME

The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers

 

VOLUME ["/data"]

WORKDIR

The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile

WORKDIR /path/to/workdir

 

tHANKS!

deck

By Luis Hernandez