0

I need to execute some commands from inside a php/php-7.1 container as a special (standard) user and group aka «www-data:www-data» used by php-fpm daemon.

www-data exists as both a user and a group on the host machine and entries exist in :

  • /etc/passwd www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
  • /etc/group www-data:x:33:

Anyway, while the container is running, I firstly get the www-data uid and gid , as follows:

// con_php is the running container's name
echo $(docker exec -it con_php bash -c 'echo "$(id -u www-data)"''":"''"$(id -g www-data)"')
// outputs 33:33, great !

Then I use this (as a command line substitution) to feed the --user option supplied to the actual command I need to run inside the container. I now have:

docker exec \
    --user $(docker exec -it con_php bash -c 'echo "$(id -u www-data)"''":"''"$(id -g www-data)"') \
    con_php \
    bash -c 'cd /var/www/project ; touch test.txt'

Unfortunately, when executed, Docker complains and outputs an error:

: no matching entries in group file


The following approach (a bash script with a temporary variable) leads to the same issue:

#! /bin/bash

usr=$(docker exec -it con_php bash -c 'echo "$(id -u www-data)"''":"''"$(id -g www-data)"')
echo "» $usr"
docker exec --user $usr con_php bash -c 'cd /var/www/project ; touch test.txt'

» 33:33
: no matching entries in group file

Thank you for your help.

1 Answer 1

0

Retrieving data from within a docker container sometimes comes in handy.
Beware if you use echo to gather output as a word (or a single line). Make sure to use -n option which prevent a trailing newline character to be prepended.

$ docker exec -it my_container bash -c 'echo -n "$(…)"'

Otherwise, you may encounter failure/misbehavior when consuming the output.
Like as an argument in a Bash process command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .