Dockerfile cheat-sheet

3 Min. Read
Jan 14, 2019

 Docker Containerization Cheatsheet Dockerfile
  • .dockerignore file: its like .gitignore file. This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY.

    1
    2
    3
    4
    
    # comment
    */temp*
    */*/temp*
    temp?
    

  • FROM: A valid Dockerfile must start with a FROM instruction sets the Base Image for subsequent instruction

    1
    
    FROM <image> [AS <name>]
    

    Or

    1
    
    FROM <image>[:<tag>] [AS <name>]
    

    Or

    1
    
    FROM <image>[@<digest>] [AS <name>]
    

  • RUN:

    RUN has 2 forms:

    • (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)

      1
      2
      3
      4
      
      RUN <command>
      RUN /bin/bash -c 'source $HOME/.bashrc; \
        echo $HOME'
      RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME'
      
    • Exec form

      1
      
      RUN ["executable", "param1", "param2"]
      

  • CMD: The CMD instruction has three forms:

    • 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)

    Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.


  • LABEL: bash LABEL <key>=<value> <key>=<value> <key>=<value> ... The LABEL instruction adds metadata to an image.

    1
    2
    3
    4
    5
    
      LABEL "com.example.vendor"="ACME Incorporated"
      LABEL com.example.label-with-value="foo"
      LABEL version="1.0"
      LABEL description="This text illustrates \
      that label-values can span multiple lines."
    

    To view an image’s labels, use the docker inspect command.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    "Labels": {
        "com.example.vendor": "ACME Incorporated"
        "com.example.label-with-value": "foo",
        "version": "1.0",
        "description": "This text illustrates that label-values can span multiple lines.",
        "multi.label1": "value1",
        "multi.label2": "value2",
        "other": "value3"
    },
    

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

    The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.

    1
    2
    3
    4
    
    To expose on both TCP and UDP, include two lines:
    
    EXPOSE 80/tcp
    EXPOSE 80/udp
    

    using docker network you can make communication from one container to another without exposing any port.


  • ENV

    1
    2
    
    ENV <key> <value>
    ENV <key>=<value> ...
    

    The ENV instruction sets the environment variable <key> to the value <value>. This value will be in the environment for all subsequent instructions in the build stage and can be replaced inline in many as well.

    1
    2
    3
    4
    
    # Example
    ENV myName John Doe
    ENV myDog Rex The Dog
    ENV myCat fluffy
    

  • ADD: ADD has two forms:

    • ADD [--chown=<user>:<group>] <src>... <dest>
    • ADD [--chown=<user>:<group>] ["<src>",... "<dest>"] (this form is required for paths containing whitespace)

    Note: The --chown feature is only supported on Dockerfiles used to build Linux containers, and will not work on Windows containers. Since user and group ownership concepts do not translate between Linux and Windows, the use of /etc/passwd and /etc/group for translating user and group names to IDs restricts this feature to only be viable for Linux OS-based containers. “`


  • COPY has two forms:

    1
    2
    
    COPY [--chown=<user>:<group>] <src>... <dest>
    COPY [--chown=<user>:<group>] ["<src>",... "<dest>"] (this form is required for paths containing whitespace)
    

    Example:-

    1
    2
    
      COPY hom* /mydir/        # adds all files starting with "hom"
      COPY hom?.txt /mydir/    # ? is replaced with any single character, e.g., "home.txt"
    

  • ENTRYPOINT ENTRYPOINT has two forms:

    • ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
    • ENTRYPOINT command param1 param2 (shell form)
    1
    2
    3
    
      FROM ubuntu
      ENTRYPOINT ["top", "-b"]
      CMD ["-c"]
    

  • WORKDIR

    1
    
    WORKDIR /path/to/workdir
    
    1
    2
    3
    4
    
    WORKDIR /a
    WORKDIR b
    WORKDIR c
    RUN pwd
    

    The output of the final pwd command in this Dockerfile would be /a/b/c.

References

https://docs.docker.com/engine/reference/builder/#from

You might also like

Learn to move your existing Rails 5/6 projects to Docker using Docker Compose.
Read More..

Summary of the post.
Read More..

In Computer Science, Containerization is a concept of running a process in a small container isolated from other processes running in the system with its own operating environment. Docker facilitates developers to build systems in modular fashion that can run in a separate container so that the behaviour does'nt vary by machines and environments.
Read More..


Write To Me

Hire me shiva bhusal
We'll never share your email with anyone else.
I'll never share your email with anyone else.