Docker: A primer for PHP Developers

               A file called Dockerfile (no file extension) is a file which gives instructions to Docker on how to build a container image.

               Dockerfiles typically start with the FROM command (also called instruction argument) followed by the name of the base

               image.

               Before we look at an example of a Dockerfile, we mention commands typically found in Dockerfiles.

          Dockerfile commands/arguments


               #
Comments begin with '#'.


               ADD
copies new files, directories, or remote file URLs from into the filesystem of the container


               CMD
allowed only once in a Dockerfile. If multiple CMD commands appear in a Dockerfile, the image will only call the last CMD command. The CMD command does not run during 'build' time.


               COPY
copies files or directories from a source into the filesystem of the container

example:  COPY readme.txt   /home/kb

               ENTRYPOINT
lets you define a container which runs as an executable (examples:"java","-jar","myapp.jar") constantly running. Mainly used to define primary application processes.


               ENV
sets environment variables

example:  ENV CONF_VALUE=app.conf  STACK_SIZE=3  

               EXPOSE
notifies the runtime container that it has to listen to the specified network ports at runtime

examples:  EXPOSE 5050
                  EXPOSE 5150 5151

               FROM
sets the base image (ubuntu, openjdk:11, alpine, etc.)

               LABEL
adds metadata (a non-executable instruction)

               MAINTAINER
sets the author field of the generated image

               RUN
- executes commands in a new layer on top of the current image and commit the results.
- runs during 'build' time.
- recommendation: use '&&' to separate commands:

example:  RUN apt-get update && update apt-get install –y php

               USER
sets the username or UID to use when running the image and commands

example:  USER Kami  

               VOLUME
creates a mount point (path) to external volumes on the native host or other containers

               WORKDIR
- sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD commands
- if it is a relative path, it is relative to the previous WORKDIR.

example:  WORKDIR /home/kbarut  
          ---> WORKDIR foo # results in "/home/kbarut/foo"

               ARG
defines a variable that users can pass at build-time to the builder using --build-arg

               ONBUILD

adds an instruction to be executed later, when the image is used as the base for another build
               STOPSIGNAL

sets the system call signal that will be sent to the container to exit

              Dockerfile - Example:

                 We want to create a wget application using Dockerfile:

                 A wget application is used to download files.

                 When the URL of a webpage is entered, it downloads the full web page file.

                 (wget is installed by default in Linux but for Windows, we need to install it.)

                 Steps:

                 1. Set the base image

                 2. Pull the lightweight OS Alpine using apk, the package manager for Alpine

                 3. Remove apk cache

                 4. Set the working directory

                 5. Remove apk cache

                 6. Define the entry point

                 7. Define the CMD command (optional)

FROM alpine

LABEL VERSION=0.1 \
      AUTHOR=KBARUT \
      EMAIL=kamibarut@yahoo.com

RUN apk update \
     && apk add wget \
     && rm -rf /var/cache/apk/*

WORKDIR /root/

ENTRYPOINT [ "get" ]

CMD [ "--help" ]

                 The next step is to build this image from the Dockerfile using the following generic command:

$ docker build -t [username/]< image-name >[:tag] < dockerfile-path >
								

                 In our case:

$ docker build -t  kbarut/app:latest -f docker/app/Dockerfile docker/app
								

  • kbarut/app: the namespaced image and an optional tag (default value is "latest")
  • docker/app/Dockerfile: the location of the Dockerfile. Placed after the -f file flag.
  • docker/app: the context, i.e.: the set of files that the build can access
Kâmi Barut-Wanayo © 2024 - 2025