Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

10 tips for writing secure, maintainable Dockerfiles

March 23, 2023
Anthony Gimei
Related topics:
ContainersSecurity
Related products:
Red Hat Enterprise Linux

Share:

    This article provides tips and best practices for creating secure Dockerfiles that are highly maintainable. Like code, Dockerfiles change over time and, therefore, should be written in such a way that makes them easy to update in the future. It is also important that the images that they create are secure and do not contain unnecessary vulnerabilities that increase the attack surface for your application. The image produced should be as small as possible because the image(s) must be stored remotely and transported in the network. Also, they must not be blotted. Finally, the Dockerfile, like any well-written code, should be easy to understand and use.

    10 Tips and best practices for Dockerfiles

    The following list describes tips and best practices for creating secure Dockerfiles that are highly maintainable.

    1. Use the current release base upstream image

    Always use the most current release base upstream image to provide security. Red Hat recommends:

    • Use the latest release of a base image. This release should contain the latest security patches available when the base image is built. When a new release of the base image is available, rebuild the application image to incorporate the base image's latest release because that release contains the latest fixes.
    • Conduct vulnerability scanning. Scan a base or application image to confirm that it doesn't contain any known security vulnerabilities.

    2. Use a specific image tag or version

    Use a specific tag or version for your image, not "latest". This gives your image traceability. When troubleshooting the running container, the exact image will be obvious.

    Examples:

    • Do this:  nginx:1.23.1
    • Don't do this:  nginx:latest

    3. Run images as USER

    For security purposes, always ensure that your images run as non-root by defining USER in your Dockerfile. Additionally, set the permissions for the files and directories to the user. Because the Docker daemon runs as root, the Docker images run as root by default. This means if a process in the container goes rogue or gets hijacked and accesses the host, it will run with root access. This is certainly not secure.

    However, Podman is daemonless and rootless by design and, therefore, more secure.

    The following is an example.

    • Add USER to your Dockerfile.
    • Skipped configurations are indicated by:  ...
    ...
    
    USER 1001
    
    RUN chown -R 1001:0 /some/directory
    
    chmod -R g=u /some/directory
    
    ...
    
    

    4. Choose base images without the full OS

    Always choose the smallest base images that do not contain the complete or full-blown OS with system utilities installed. You can install the specific tools and utilities needed for your application in the Dockerfile build. This will reduce possible vulnerabilities and the attack surface of your image.

    5. Use multi-stage Dockerfiles

    Build images using multi-stage Dockerfiles to keep the image small. For example, for a Java application running in Open Liberty, use one stage to do the compile and build, and another stage to copy the binary artifact(s) and dependencies into the image, discarding all nonessential artifacts. Another example is, for an Angular application, run the npm install and build in one stage and copy the built artifacts in the next stage.

    • Example: Open Liberty Java application
    FROM registry.access.redhat.com/ubi8/openjdk-8:latest as builder
    
    USER 0
    
    WORKDIR /tmp/app
    
    COPY src/ src/
    
    COPY pom.xml pom.xml
    
    RUN mvn clean package
    
    ...
    
    FROM quay.io/ohthree/open-liberty:22.0.0.4
    
    ...
    
    COPY --from=builder /tmp/app/src/main/liberty/config/server.xml /config/
    
    COPY --from=builder /tmp/app/target/*.war /config/apps/
    
    RUN \
    
        chown -R 1001:0 /config && \
    
        chmod -R g=u /config
    
    
    # Run as non-root user
    
    USER 1001
    
    EXPOSE 9081
    
    

    6. Use Docker ignore file

    Use a .dockerignore file to ignore files that do not need to be added to the image.

    7. Scan for vulnerabilities

    Scan your images for known vulnerabilities.

    • Podman integrates with multiple open-source scanning tools. For example, you can use Synk or Trivy.
    • Docker integrates with its own plugin local machine. Install the plugin, then run the following command: 

    $ docker scan myappimage:1.0

    8. Automate scans

    Automated scanning tools should also be implemented in the CI pipeline and on the enterprise registry. We also recommend deploying runtime scanning on applications in case a vulnerability is uncovered in the future.

    9. Organize your Docker commands

    Organize your Docker commands, especially the COPY command, in such a way that the files that change most frequently are at the bottom. This will speed up the build process. The reason for this is to take advantage of the Docker build process and speed up future builds.

    Each Docker build command creates a layer that is cached to be reused in the next build, designed to speed up subsequent builds. The caveat is that, in the subsequent build, if a command encounters a change, all commands after that will run and recreate new layers and cached, replacing the old ones even if they did not contain any changes. Having the most volatile COPY statements later in the Dockerfile maximize build caching.

    10. Concatenate RUN commands

    Concatenate RUN commands to make your Dockerfile more readable and create fewer layers. Fewer layers mean a smaller container image. As mentioned previously, each RUN statement in the Dockerfile creates a layer that gets cached. Concatenating reduces the number of layers.

    The following are examples of what to do and not to do.

    • Don't do this:
    ...
    
    RUN yum --disablerepo=* --enablerepo=”rhel-7-server-rpms”
    
    RUN yum update
    
    RUN yum install -yl httpd
    
    ...
    • Do this instead:
    ...
    
    RUN yum --disablerepo=* --enablerepo=”rhel-7-server-rpms” && yum update && yum install -yl httpd
    
    ...
    • Even better, do this for readability:
    ...
    
    RUN yum --disablerepo=* --enablerepo=”rhel-7-server-rpms” && \
    
        yum update && \
    
        yum install -yl httpd
    
    ...

    Find more resources

    We hope that these tips will help you build more secure Dockerfiles. Visit the Docker website for more information. See what we are doing on the Red Hat Developers Site. You can learn more about containerizing applications at Red Hat DO 180 training. If you have a question, feel free to comment below. We welcome your feedback.

    Related Posts

    • Useful Dockerfiles for the RHEL-ecosystem

    • Dockerfiles now available for Red Hat Software Collections

    • Planning your containerization strategy on Red Hat OpenShift

    • Build your first application using PHP with Red Hat Container Development Kit (CDK)

    Recent Posts

    • Meet the Red Hat Node.js team at PowerUP 2025

    • How to use pipelines for AI/ML automation at the edge

    • What's new in network observability 1.8

    • LLM Compressor: Optimize LLMs for low-latency deployments

    • How to set up NVIDIA NIM on Red Hat OpenShift AI

    What’s up next?

    cheat sheet cover image

    Download our Podman Basics Containers Cheat Sheet. It includes all the commands you need to get started with containers, images, and container resources.

    Get the cheat sheet
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue