Skip to content

Latest commit

 

History

History
80 lines (62 loc) · 3.75 KB

docker_file.md

File metadata and controls

80 lines (62 loc) · 3.75 KB

Dockerfile

Dockerfile is a human-readable text file which tells docker how to build a docker image. Dockerfile most of the time is named as Dockerfile whereas a user can name it anything.

Following is the list of most used commands in a dockerfile:

FROM NAME[:TAG|@DIGEST]

The FROM instruction tells docker-engine which base image is to be used.
Example:

  • FROM ubuntu:latest
  • FROM redis:community-7.0.0-beta

- RUN <COMMAND> (shell form)
- RUN ["EXECUTABLE", "PARAM1", "PARAM2"] (exec form)

The RUN instruction executes a command in docker container's shell. Default executable for linux is /bin/sh. In shell form, the entire command must be specified after the word RUN, whereas in exec format, the same command is specified as a JSON array. Never mix these two forms together.
Example:

  • RUN apt update
  • RUN ["npm", "install"]

EXPOSE <PORT> [<PORT>/PROTOCOL]

EXPOSE informs docker that the container will be listening on the specified port. Note that this does not mean user can access the app through this port, EXPOSE does not open the port to the host machine, it just serves as a means of documentation to the developer(s).
Example:

  • EXPOSE 5000
  • EXPOSE 6622/udp

ENV <KEY>=<VALUE> [<KEY>=<VALUE>...]

Use the ENV command to set environment variables. Environment variables are a great alternative to explicit variable assignment which includes configuration data and sensitive information. = sign in the instruction can be omitted and replaced by space.
Example:

  • ENV DB=Mongodb
  • ENV WORKERS 10

- CMD <COMMAND> (shell form)
- CMD ["EXECUTABLE", "PARAM1", "PARAM2"] (exec form)
- CMD ["PARAM1", "PARAM2"] (entrypoint default args)

CMD command is used to provide default arguments for ENTRYPOINT instruction. It is used by docker only if no additional arguments are provided while running the docker container. The main reason CMD is used, is to provide some default arguments in the case where user does not specify any. If a user specifies multiple CMD instructions, only the last instruction will be executed by docker.
Example:

  • CMD ["echo", "linux is the best"] (executes a command)
  • CMD ["-c", "100", "--nodes", "5"] (provides default arguments to ENTRYPOINT command)

- ENTRYPOINT <COMMAND> (shell form)
- ENTRYPOINT ["EXECUTABLE", "PARAM1", "PARAM2"] (exec form)

When a container is used as an executable there must be one ENTRYPOINT command in the dockerfile. ENTRYPOINT arguments can not be over-ridden by providing command-line arguments to docker run.
Example:

  • ENTRYPOINT ["ps", "aux"] (executes a command)
  • ENTRYPOINT ["gunicorn", "--workers", "2", "test:app"] (start the app and use the container as an executable)

Examples:

Learn about Docker images, start the next lesson

Resources: