Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

10 tips for writing secure, maintainable Dockerfiles

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

    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

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    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

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.