The following 12 best practices are critical in helping you create and maintain secure container images:
Use minimal base image: This reduces image size and also reduces the number of libraries/utilities that may have vulnerabilities or may be used for malicious purposes.
Use base image from trusted source and check signatures/digests: What level of confidence do you have that the container is what it says it is and is safe?
Reference base image by a specific version tag, not "latest": You need to know the specific version you are using, for component tracking and rebuilding of images. The "latest" image is a moving target, so the orchestrator could pull a new image with unintended consequences.
Install the minimum required packages: This reduces image size and also reduces the number of packages/components that may have vulnerabilities or may be used for malicious purposes.
Don't perform "apt upgrade" or "yum upgrade" equivalents: These impact the creation of immutable, consistent builds.
Specify a user other than root to run the container (ideally as USER ${UID} for Red Hat OpenShift mustRunAsNonRoot compatibility): Aim for minimal privilege. Containers run as root by default, and this is a risk, especially if the container is run with privilege. Use a non-root user to avoid this.
Include a healthcheck: An important security control is that of availability. Adding the HEALTHCHECK instruction to your container image ensures that the container engine periodically checks the running container instances against that instruction to ensure that the containers are still operational. (Note: Kubernetes can be used to perform the healthcheck.)
Don't write any secrets, passwords, or keys into Dockerfiles (including not as ARG or ENV): You’ll be storing sensitive information in plain text. Consider who will be able to see these details and potentially use them.
Use the COPY command (rather than ADD) where possible:ADD automatically extracts archives that could have malicious content (plus other reasons).
Don't open port 22 or include SSH unless specifically required: This provides a potential attack vector, and SSH isn’t required to monitor the container. (For more information, see If you run SSHD in your Docker containers, you're doing it wrong!). Docker and Kubernetes/OpenShift enable you to exec into a container.
Remove (where possible) any suid or sgid permissions: These are two special permissions that can be set on executable files: set user ID (suid) and set group ID (sgid). These permissions allow the file being executed to be executed with the privileges of the owner or the group.
Configure containers to work under OpenShift restricted SCC: For compatibility, ensure that any required directories have chmod g=u & chgrp -R 0 applied and that the HOME environment variable is supplied.
Also, bear in mind...
Container images should be scanned during development to ensure that container configuration is as secure as possible and in line with best practices.
In addition to checking that the Dockerfile/containerfile is configured in line with best practices, you should perform the following checks against the image filesystem:
Check for OS and non-OS packages with vulnerabilities (and also check the age of the vulnerability feeds).
Are there any unknown (non-official) application dependency feeds (such as NPM or Ruby) being used?
Is the distribution used for the container not one of a defined list (such as: alpine, busybox, centos, ubuntu, debian, fedora, or ol)? If so, there could be issues with provenance.
Finally, images that are being used within the environment should be continuously scanned against updated vulnerability databases to ensure that new vulnerabilities are managed.
About cookies on this siteOur websites require some cookies to function properly (required). In addition, other cookies may be used with your consent to analyze site usage, improve the user experience and for advertising.For more information, please review your cookie preferences options. By visiting our website, you agree to our processing of information as described in IBM’sprivacy statement. To provide a smooth navigation, your cookie preferences will be shared across the IBM web domains listed here.