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

Configuring Containerized Services

July 10, 2017
Tomas Tomecek
Related topics:
Containers
Related products:
Red Hat OpenShift

    I’m assuming you’ve already tried to run some example of a multi-container application. Let’s say we have an application composed of the following:

    • Web service
    • Database
    • Key-value store
    • Worker

    Let’s focus on the database now. Container images for databases usually come with an easy way to configure them via environment variables. The great thing about this approach is how easy to use it is, e.g. let’s take our RHSCL PostgreSQL 9.5 container image. If you try to run it just like that, it refuses to run and instead guides you how you should run the container:

    $ docker run registry.access.redhat.com/rhscl/postgresql-95-rhel7:latest
    You must either specify the following environment variables:
      POSTGRESQL_USER (regex: '^[a-zA-Z_][a-zA-Z0-9_]*$')
      POSTGRESQL_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:]+$')
      POSTGRESQL_DATABASE (regex: '^[a-zA-Z_][a-zA-Z0-9_]*$')
    Or the following environment variable:
      POSTGRESQL_ADMIN_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:]+$')
    Or both.
    Optional settings:
      POSTGRESQL_MAX_CONNECTIONS (default: 100)
      POSTGRESQL_MAX_PREPARED_TRANSACTIONS (default: 0)
      POSTGRESQL_SHARED_BUFFERS (default: 32MB)
    
    For more information see /usr/share/container-scripts/postgresql/README.md
    within the container or visit https://github.com/openshift/postgresql.

    Neat! So let’s pass the environment variables:

    $ docker run --rm \
        -e POSTGRESQL_USER=test_user \
        -e POSTGRESQL_PASSWORD=secret \
        -e POSTGRESQL_DATABASE=test_database \
        --name=pg \
        registry.access.redhat.com/rhscl/postgresql-95-rhel7:latest
    The files belonging to this database system will be owned by user "postgres".
    This user must also own the server process.
    
    The database cluster will be initialized with locale "en_US.utf8".
    The default database encoding has accordingly been set to "UTF8".
    The default text search configuration will be set to "english".
    
    Data page checksums are disabled.
    
    fixing permissions on existing directory /var/lib/pgsql/data/userdata ... ok
    creating subdirectories ... ok
    selecting default max_connections ... 100
    selecting default shared_buffers ... 128MB
    selecting dynamic shared memory implementation ... posix
    creating configuration files ... ok
    creating template1 database in /var/lib/pgsql/data/userdata/base/1 ... ok
    initializing pg_authid ... ok
    initializing dependencies ... ok
    creating system views ... ok
    loading system objects' descriptions ... ok
    creating collations ... ok
    creating conversions ... ok
    creating dictionaries ... ok
    setting privileges on built-in objects ... ok
    creating information schema ... ok
    loading PL/pgSQL server-side language ... ok
    vacuuming database template1 ... ok
    copying template1 to template0 ... ok
    copying template1 to postgres ... ok
    syncing data to disk ... ok
    
    output shortened
    
    LOG: redirecting log output to logging collector process
    HINT: Future log output will appear in directory "pg_log".

    And it’s running!

    If we want to start configuring the service, we may do so using a bunch of environment variables, such as POSTGRESQL_SHARED_BUFFERS. And this is where we may hit the wall: how we configure other parameters?

    The first thing to note is that it would be awesome if images clearly documented how you can change the configuration of the containerized service running inside.

    In the meantime, let’s have a look at what are the best ways to configure a containerized service.

    Overlaying configuration

    In this method, you should create a new container image by applying your configuration changes on top of an existing container image. Let’s try this with the PostgreSQL container image mentioned before.

    The first thing to do is to copy the existing configuration file template,openshift-custom-postgresql.conf.template from the running container:

    $ docker cp pg:/usr/share/container-scripts/postgresql/openshift-custom-postgresql.conf.template ./openshift-custom-postgresql.conf.template

    We should edit the template file now and produce our custom postgresql container image.

    #
    # Custom OpenShift configuration.
    #
    # NOTE: This file is rewritten every time the container is started!
    # Changes to this file will be overwritten.
    #
    
    # Listen on all interfaces.
    listen_addresses = '*'
    
    # Determines the maximum number of concurrent connections to the database server. Default: 100
    max_connections = ${POSTGRESQL_MAX_CONNECTIONS}
    
    # Allow each connection to use a prepared transaction.
    max_prepared_transactions = ${POSTGRESQL_MAX_PREPARED_TRANSACTIONS}
    
    # Sets the amount of memory the database server uses for shared memory buffers. Default: 32MB
    shared_buffers = ${POSTGRESQL_SHARED_BUFFERS}
    
    # Sets the planner's assumption about the effective size of the disk cache that is available to a single query.
    effective_cache_size = ${POSTGRESQL_EFFECTIVE_CACHE_SIZE}
    
    # Let’s increase work_mem so most operations are performed in memory
    work_mem = 64MB
    

    We added last two lines to the template.

    Now is the time to build the image, we will use this dockerfile: (we update labels so it’s clear that the newly built image is not the official RHSCL image, rather a variant based on it)

    FROM registry.access.redhat.com/rhscl/postgresql-95-rhel7:latest
    LABEL name="tomastomecek/postgresql" \
          vendor="Tomas Tomecek"
    COPY ./openshift-custom-postgresql.conf.template /usr/share/container-scripts/postgresql/
    

    Let’s build...

    $ docker build --tag=tomastomecek/postgresql .

    and run...

    $ docker run --rm \
        -e POSTGRESQL_USER=pg_test \
        -e POSTGRESQL_PASSWORD=secret \
        -e POSTGRESQL_DATABASE=test_db \
        --name pg tomastomecek/postgresql

    And check if the work_mem attribute was changed:

    $ docker exec pg bash -c 'psql --command "show work_mem;"'
    work_mem
    ----------
    64MB
    (1 row)

    It’s updated!

    We can even go one step further and change the value dynamically. Let’s update our template from

    work_mem = 64MB
    

    to

    work_mem = ${POSTGRESQL_WORK_MEM}
    

    We need to rebuild our image first

    $ docker build --tag=tomastomecek/postgresql .

    and then let’s set the work_mem attribute to 128MB via the environment variable:

    $ docker run --rm \
        -e POSTGRESQL_USER=pg_test \
        -e POSTGRESQL_PASSWORD=secret \
        -e POSTGRESQL_DATABASE=test_db \
        -e POSTGRESQL_WORK_MEM=128MB \
        --name pg tomastomecek/postgresql

    We were able to define a new environment variable because the container’s startup script is using the envsubst command. Obviously, this is just an implementation detail and it should be clearly documented how one can define new variables (if possible).

    Let’s verify now that work_mem is set to 128MB.

    $ docker exec pg bash -c 'psql --command "show work_mem;"'
    work_mem
    ----------
    128MB
    (1 row)

    Pros

    • Portable — the container will work the same way in any environment.
    • Easy to test and audit.

    Cons

    • Building and distributing a large amount of new images can be complicated.
    • Requires image to be built - which needs additional automation (git repository for dockerfile, build a pipeline, image naming conventions, registry).
    • It’s tricky to figure out without documentation — which may lead to undefined behavior.

    Injecting configuration

    In OpenShift, we can take advantage of ConfigMaps and inject configuration inside pods using them. Christoph Görn wrote a blog post on best practices for configuration inside OpenShift.

    Let’s do the example from “Overlaying configuration” section above without building a new image.

    Instead of building a new image, we will bind mount the template inside RHSCL postgresql container.

    $ docker run --rm \
        -e POSTGRESQL_USER=pg_test \
        -e POSTGRESQL_PASSWORD=secret \
        -e POSTGRESQL_DATABASE=test_db \
        -v openshift-custom-postgresql.conf.template:/usr/share/container-scripts/postgresql/ \
        -e POSTGRESQL_WORK_MEM=128MB \
        --name pg \
        registry.access.redhat.com/rhscl/postgresql-95-rhel7:latest

    This was a lot easier. All we had to do was to:

    1. Get the template file.
    2. Update it.
    3. Mount the template file inside the container.

    Let’s verify the work_mem attribute is set correctly.

    $ docker exec pg bash -c 'psql --command "show work_mem;"'
    work_mem
    ----------
    128MB
    (1 row)

    Pros

    • Decoupling configuration from the immutable image — you can configure the containerized service independently.
    • No need to create new images.

    Cons

    • The container is no longer portable and requires configuration.

    Conclusion

    As you can see, there is no silver bullet when it comes to the configuration of containerized services. There are multiple options and it’s up to you to pick the one, which suits you best.


    Click here to learn about Red Hat Openshift Container Platform, allowing you to provision, manage, and scale container-based applications.

    Last updated: March 23, 2023

    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

    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.