PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 22, 2022

[FIXED] How to update environment variable for different value for each different users in docker container for container is runnig

 August 22, 2022     docker-compose, docker-container, environment-variables, linux     No comments   

Issue

First of all, if I need to talk about the situation I want to do, I have docker containers that I set up with the docker-compose.yml file. Inside this docker image, there are 2 more users ("user1" and "user2"), one of which is the "root" user. (I created them in Dockerfile, everything is fine so far). There is one enviroment variable defined for these users. (echo $env_val). I want this variable to have a separate value for each user and I want to manage this value dynamically. In other words, I want to manage the "environment" variable used in "docker-compose.yml" on a user basis.

version: '3'
services:
   app1:
      image: image1
      enviromnet:
         - env_val=34052    # this value default 34034 but for user1 value is assing 34052
      entrypoint: /bin/sh/ start_container.sh

   app2:
      image: image1
      enviromnet:
         - env_val=34028    # this value default 34028 but for user2 value is assing 34028
      entrypoint: /bin/sh/ start_container.sh

My docker-compose.yml file is like this. The env_val variable here is assigned the value I specified only for the root user. But I want the value I gave to be assigned only for the user I selected in the image. I didn't see any solution for this. Do you have any advice for me to meet this need?

Note: I tried these solution

  • I was able to assign the value I wanted to this enviroment with a script I wrote, but then when I logged into this container with docker exec -it container_name bash and logged in with the user I chose su - user1, I could not see the value I assigned. Default value was set. My command: docker exec -u 0 container_name /bin/bash -c "su - user1; source ~/.bash_profile; export env_val=34052"

  • Setting environment variable for docker container at runtime

  • This solution decides on the relevant environemt during the image creation phase. In my case, I want to change the value of this variable when the image files are created, while running the image. Docker different environment variables for the same service

thank you in advance for your help :)


Solution

Even though it's not a solution that suits me, I found a way. I wrote a new script. This script takes the value of the environmet variable that I will change as a parameter. I'm sending this command to the docker container standing on the host machine.

#!/bin/bash

docker exec -u 0 container_name /bin/bash -c "echo export env_val=$1 >> /home/user1/.bash_profile; su - user1; source ~/.bash_profile; <run_another_command>"

This command appends the command I need to the end of the .bash_profile of the user I want inside the running container. It also logs in with the user I want and sources the .bash_profile file. Afterwards, I can start the application that I will run in the configuration I want.

Thus, when I log in with this user, the configuration I want is done. Finally, when I stop or delete the container, my image is not affected by this change.



Answered By - dincer.unal
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing