Welcome to the Linux Foundation Forum!

NGINX container command: option

Anyone tried to initialize index page of NGINX container to populate with IP address of the container using command: and args: option, something like as below to replace Welcome message on the index page? I am not able to get it to work. Container image is crashing when the command runs.
....
spec:
containers:
- name: webserver
image: nginx
command: ["/bin/bash/sed"]
args: [" -i 's/Welcome/Welcome $(hostname -i)/g' /usr/share/nginx/html/index.html "]
....
The idea behind this is to display the ip address of the container to see how various backend containers behind a service responding to client requests and to visualize the load balancing.

Comments

  • Hi @pbbhaskar,

    The command and args provided through the PodSpec modify the container upon creation, running it as an executable instead of the originally intended nginx webserver container.

    As a result, your container is not crashing, but it is doing only what you asked it to do and then it completes. Meaning that it runs sed and it completes almost instantaneously.

    In order to preserve your nginx container, you can either use a sidecar container to setup the index.html file of nginx, or manually edit each nginx index.html file in your pods to display the desired information.

    Regards,
    -Chris

  • pbbhaskar
    pbbhaskar Posts: 15

    I'm able to get it to work as shown below

    apiVersion: v1
    kind: Pod
    metadata:
    name: command-demo
    spec:
    containers:
    - name: command-demo-container
    image: nginx
    lifecycle:
    postStart:
    exec:
    command: ["/bin/sh","-c", 'sed -ie "s|Welcome|Welcome $(printenv NGINX_SERVICE_HOST)|g" /usr/share/nginx/html/index.html' ]

Categories

Upcoming Training