Container Best Practices
Updated Mar 11, 2022 ·
Writing Dockerfiles
To learn more, check out Best practices for writing Dockerfiles.
-
Use Alpine as base image. It's small and still a full Linux distribution.
FROM alpine -
Add
MAINTAINERto let others know who to contact.MAINTAINER John Smith <john.smith@abc.com> -
Run multiple commands in one
RUNstatement, connected by &&.RUN apt-get update && apt-get install -y subversion -
Split long
RUNstatements on multiple lines to keep them readable.RUN apt-get update && apt-get install -y \bzr \cvs \git \mercurial \subversion \&& rm -rf /var/lib/apt/lists/* -
Use
ENVto set environment variables for entrypoint applications.ENV PG_MAJOR=9.3ENV PG_VERSION=9.3.4RUN curl -SL https://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgres && …ENV PATH=/usr/local/postgres-$PG_MAJOR/bin:$PATH -
Use
COPYnotADDto copy files onto the containersCOPY requirements.txt /tmp/RUN pip install --requirement /tmp/requirements.txtCOPY . /tmp/ -
Use
USERto run as a non-root userUSER Johnsmith
Container Security
When it comes to container security, keep these tips in mind:
- Include as little as possible in the container images.
- Run rootless containers.
- Specify which user account to use in an image.
- Use verified images.
- For custom images, sign your images.
- Use access control on container registries.
- Run containers on isolated networks.
- Use Kubernetes for implementing Role-based Access Control (RBAC).