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

That app you love, part 4: Designing a config-and-run container

 

October 6, 2016
N. Harrison Ripps
Related topics:
ContainersDeveloper ToolsKubernetesMicroservices
Related products:
Red Hat OpenShift Container Platform

Share:

    Welcome to the fourth installment of That App You Love, a blog series in which I show you how to you can make almost any app into a first-class cloud citizen. If you want to start from the beginning, jump back and check out Part 1: Making a Connection.

    In Part 3, we looked at how to customize the configuration of ZNC using an expect script and environment variables. But forget ZNC, because we’re really talking about That App You Love, and the way you configure it is up to you - as long as you use environment variables to expose the flexibility that you need. And now it’s time to containerize it by combining three distinct jobs into a single container image:

    • Install the app
    • Configure the app
    • Run the app

    Build-Time vs. Run-Time

    When we think about creating a container image, we have two opportunities to effect change: a) when the container image is first built and b) when the container is actually running. In general, we want to reduce the start time on our containers, and ideally this means we do as much at build-time as possible. But I’m here to advocate for sacrificing a very small amount of run-time startup for the opportunity to customize each deployment of That App You Love. And after all - isn’t that kind of love worth a few microseconds?

    Here’s all ten lines of the Dockerfile for znc-zluster-app:

    FROM fedora:24
    MAINTAINER N. Harrison Ripps
    
    RUN dnf install -y procps-ng expect znc && mkdir /opt/znc-env && mkdir /opt/znc-run
    COPY znc_* /opt/znc-run/
    RUN chown -R 1001:0 /opt/znc-env /opt/znc-run && chmod -R ug+rwx /opt/znc-env /opt/znc-run
    USER 1001
    
    EXPOSE 6697
    ENTRYPOINT ["/opt/znc-run/znc_runner.sh"]

    In line #3, I tackle the first job - installing my app. In line #10, I tackle the final job - running my app. But wait a minute - what about the configuration job we spent all of the last blog post talking about? And - bonus question - why are we changing user IDs midway through this build?

    Let’s answer the bonus question first. If you are reading this post on a Linux device, are you currently logged in as the root user? Probably not, because you enjoy not accidentally nuking your system with a single errant command. So why on earth would we want to run That App You Love as root? Good ol’ user 1001 doesn’t have root privileges and it can run our app just fine. High five, user 1001.

    Now, about the other question - when do we actually configure our app? If you recall from our last post, the command to run ZNC is “znc”, not “znc_runner.sh” - so to answer your question, let’s take a closer look at that znc_runner.sh file.

    The Config-and-Run Script

    Here’s a pared down look at the contents of znc_runner.sh. This script brings together app configuration and app running under a single executable process:

    # Handle environment variables
    ...
    
    # Check if the ZNC config exists
    if [ ! -d ${ZNC_DATADIR}/configs ]; then
      ${ZNC_EXECDIR}/znc_expect.exp
    fi
    
    # Run ZNC
    /usr/bin/znc -d $ZNC_DATADIR --foreground
    

    The logic here is pretty simple:

    1. Check the container filesystem for an indication that the app is configured
    2. Configure it if necessary (I do this by invoking my expect script)
    3. Run the app as a foreground process

    Because app image containers are inherently stateless, the configuration is going to run every single time we invoke this container - unless somehow the configuration that was written by one container is available to subsequent containers. Then, the config check for those later generations will skip configuration and go straight to running the app. We’re going to leverage that later on in this blog series.

    I Love It When A Plan Comes Together

    To see how the Dockerfile, the config-and-run-script, and environment variables come together for That App You Love, let’s run znc-cluster-app one more time:

    1. If you haven’t already, install and start docker. Shortcut for Fedora users:
      sudo dnf install -y docker && sudo systemctl start docker
      
    2. Next, copy my app image if you don’t have it:
      sudo docker pull nhripps/znc-cluster-app:latest
    3. Run the image, replacing 9999 with a free port on your system if you need to, and with a few sample env variables set:
      sudo docker run -p 9999:6697 -d -e ZNC_USER=ronaldinho -e ZNC_PASS=futbol --name=znc2 nhripps/znc-cluster-app
      
    4. Take a look at the container logs:
      sudo docker logs znc2

    The log output will look a lot like the output of my expect script from Part 3. And now we know what is happening: a) the container starts, b) there is no existing config, c) a configuration is created based on the environment variables that we passed in, d) the app is now running.

    If my laptop were always on and always connected to the internet, I could wrap up this series by showing you how to mount a directory from my laptop into my running container. That would add statefulness, yes, but not robustness. And let’s not sell ourselves short - That App You Love belongs in the cloud!

    We’ll turn our eyes to the skies when we start looking at cloud deployments in Part 5: Upping Our (Cloud) Game.

    This series will run every Tuesday and Thursday until we've accomplished our goals, so stay tuned in, subscribe, and thanks for reading!

    Title Date
    That app you love, part 1: Making a connection 2016/09/27
    That app you love, part 2: Immutable but flexible – What settings matter? 2016/09/29
    That app you love, part 3: Every setting in its place 2016/10/04
    That app you love, part 4: Designing a config-and-run container 2016/10/06
    That app you love, part 5: Upping our (cloud) game 2016/10/11
    That app you love, part 6: Container, meet cloud 2016/10/13
    That app you love, part 7: Wired for sound 2016/10/18
    That app you love, part 8: A blueprint for “that app you love” 2016/10/20
    That app you love, part 9: Storage and statefulness 2016/10/25
    That app you love, part 10: Long live “that app you love” 2016/10/27

    About the Author

    Hi there! My name is N. Harrison Ripps, and I am an engineer and people manager on the Containers team at Red Hat. Together with the greater open source community, our team has taken the combination of the docker container format and Google’s Kubernetes orchestration system, and then extended this framework with a number of administrator- and developer-friendly features that we call the OpenShift Container Platform.

    Last updated: March 20, 2023

    Recent Posts

    • Container starting and termination order in a pod

    • More Essential AI tutorials for Node.js Developers

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    What’s up next?

     

    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