Skip to main content

Docker

References

Dockerfile

  • Build Base Container Image

    • Create a working directory

    • Write the Dockerfile specification

      • Filename: Dockerfile
    • Build the image

  • Dockerfile

    • Text file with the instructions used to build the image

    • Each instruction is executed sequentially

    • Instructions

      • Comments start with #

      • FROM

        • indicates the parent image
      • LABEL

        • Key/value pairs (e.g. LABEL description='A label')
      • RUN

      • MAINTAINER

      • EXPOSE

        • Metadata indicating that a specific port is being exposed
        • This doesn’t actually expose a port. When you run the docker image, you’ll provide a -d arg where you will map a port on the host to the port exposed in the image (e.g. -d 10080:80)
      • ENV

      • ADD

        • Put items from remote locations inside the container
        • You can add items from remote locations and/or untar files
      • COPY

        • Put items from the host into the container
      • USER

        • The commands following a RUN command will be executed as the user specified.
      • ENTRYPOINT

      • CMD

        • Provides commands for the ENTRYPOINT
    • CMD and ENTRYPOINT

      • Defining an ENTRYPOINT in the Dockerfile creates containers that are executable
      • The ENTRYPOINT can be a script that is added to the container with an ADD instruction
      • The default ENTRYPOINT instruction is /bin/sh -c
      • Use CMD to pass in arguments to the ENTRYPOINT instruction
      • The CMD argument can be overwritten when running a container docker run.
    • Image layering

      • Each instruction in a Dockerfile creates a new layer
      • Having too many instructions in a Dockerfile can cause too many layers, which can result in large image sizes
    • Readability

      • Dockerfiles are meant to be read by others
      • Maintain readability while consolidating instructions
      • Put labels and env variables on new lines
    • If you are currently in the directory in a terminal, you can use a '.' to reference the current directory

Samples

Basic Python Container

DockerHub: https://hub.docker.com/_/python

For Reference: https://runnable.com/docker/python/dockerize-your-python-application

In this example, I created a project directory with a single PY file and a Dockerfile. The whole directory will be added to the container environment and the index.py file will be executed.

index.py

print ("Hello World!")

Dockerfile

FROM python
ADD index.py /
CMD ["python", "./index.py"]

Build the Dockerfile

docker build -t python-basic .

Run the Docker image

docker run python-basic

Attachments

PDF File Docker Cheatsheet.pdf