Dockerfiles
Creating portable images
Dockerfile defines what goes on in the environment inside your container. Access to resources like networking interfaces and disk drives is virtualized inside this environment, which is isolated from the rest of your system, so you need to map ports to the outside world, and be specific about what files you want to "copy in" to that environment. However, after doing that, you can expect that the build of your app defined in thisDockerfilebehaves exactly the same wherever it runs.
Docker build command
docker build -t friendlyhello .
Best Practices for writing Dockerfiles
- Create ephemeral containers
- Understand build context
- Pipe Dockerfile through stdin
- Exclude with .dockerignore
- Use multi-stage builds
- Don't install unnecessary packages
- Decouple applications
- Minimize the number of layers
- Sort multi-line arguments
- Leverage build cache
Dockerfile instructions
-
FROM
FROM python:3.7.2-slim
-
LABEL
-
RUN
- APT-GET
- USING PIPES
-
CMD
CMD [ "python", "logger.py" ]
-
EXPOSE
-
ARG
The ARG instruction defines a variable that users can pass at build-time to the builder with the
docker build
command using the--build-arg <varname>=<value>
flag. -
ENV
-
ADD or COPY
ADD logger.py logger.py
COPY
takes in asrcanddestination. It only lets you copy in a local file or directory from your host (the machine building the Docker image) into the Docker image itselfADD
lets you do that too, but it also supports 2 other sources. First, you can use a URL instead of a local file / directory. Secondly, you can extract a tar file from the source directly into the destination -
ENTRYPOINT
-
VOLUME
-
USER
-
WORKDIR
-
ONBUILD
-
RUN executes command(s) in a new layer and creates a new image. E.g., it is often used for installing software packages.
-
CMD sets default command and/or parameters, which can be overwritten from command line when docker container runs.
-
ENTRYPOINT configures a container that will run as an executable.
-
Shell & Exec form
http://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint
https://docs.docker.com/develop/develop-images/dockerfile_best-practices
https://www.docker.com/blog/intro-guide-to-dockerfile-best-practices