Dockerfile

ALWAYS FOLLOWS THIS FORMAT: INSTRUCTION ARGUMENT

FROM

  • Inherits and sets Base Image (like Base class) or OS

  • eg. python:3.6

    • 3.6 is the tag/version

RUN apt-get update

  • Runs this linux command and downloads all OS package related updates in the container

COPY ./<your-directory> /<your-directory>

  • Copies local folder into the container's folder

    • i.e. ./app in local folder copies to container's /app

  • For local folder, the . is the same directory as the Dockerfile

WORKDIR

  • Starts container in thie workdir directory

    • i.e.

      • mkdir workdir

      • cd workdir

Note: These 2 ways are equivalent:

ENV

  • Sets environment variable (global variable in shell) in the container

  • Will do export MYSQL_USER=ml

CMD

  • Command at startup of container (if there is another command in docker run..., this command replaces the one in the Dockerfile)

ENTRYPOINT

  • Command at startup of container (if there is another command in docker run..., this command is APPENDED to the one in the Dockerfile)

Using both CMD and ENTRYPOINT

You do this when you want a default command, but the argument can be changed

So here, if no parameter is specified in docker run... , then sleep 5 will run. Otherwise, sleep <argument> will run.

colon :

  • This means port

  • If you do google:80, just goes to google

    • 80 is the default port for websites

  • 22 is the default port for ssh

gunicorn

  • -w # Tells you number of CPU cores/threads (1 CPU core handles 1 request at a time)

  • -b :#: bind to port # of container

  • -t #: timeout in seconds

Formatting

These are all equivalent: Array: RUN/CMD/... [a, b, c, d] Shell: RUN/CMD/... a b c d

Running commands inside a Docker container

Caching

Caching when building Docker images can be very useful as it saves a lot of time when installing dependencies.

In what cases do Docker commands not use their cache?

  • If the contents that the command is applying itself on changes.

    • e.g. COPY ./example /copy . If ./example is changed in any way, this line in the Dockerfile will not use the cache.

  • If anything before the command is installed/changed that wasn't installed/changed before.

Running another program before starting container

Last updated

Was this helpful?