4

When using WP CLI in docker, I need to execute it as root. I need to add the flag --allow-root directly in .bashrc and I am trying to figure out why it doesn't work.

FROM webdevops/php-dev:7.3

# configure postfix to use mailhog
RUN postconf -e "relayhost = mail:1025"

# install wp cli
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
    chmod +x wp-cli.phar && \
    mv wp-cli.phar /usr/local/bin/wp && \
    echo 'wp() {' >> ~/.bashrc && \
    echo '/usr/local/bin/wp "$@" --allow-root' >> ~/.bashrc && \
    echo '}' >> ~/.bashrc

WORKDIR /var/www/html/

my .bashrc

# ~/.bashrc: executed by bash(1) for non-login shells.

# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ '
# umask 022

# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval "`dircolors`"
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
wp() {
/usr/local/bin/wp "$@" --allow-root
}

when I try to execute any wp command I get this error:

Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under.

If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.

If you'd like to continue as root, please run this again, adding this flag:  --allow-root

If you'd like to run it as the user that this site is under, you can run the following to become the respective user:

    sudo -u USER -i -- wp <command>

It looks like that command line doesn't consider what I input into .bashrc

Guys, do you have any suggestion how to fix this problem?

fromthestone
  • 1,217
  • 2
  • 13
  • 16

3 Answers3

5

You are struggling with the classic conundrum: What goes in bashrc and what in bash_profile and which one is loaded when?

The extreme short version is:

$HOME/.bash_profile: read at login shells. Should always source $HOME/.bashrc. Should only contain environmental variables that can be passed on to other functions.

$HOME/.bashrc: read only for interactive shells that are not login (eg. opening a terminal in X). Should only contain aliases and functions

How does this help the OP?

The OP executes the following line:

$ sudo -u USER -i -- wp <command>

The flag -i of the sudo-command initiates a login-shell

-i, --login: Run the shell specified by the target user's password database entry as a login shell. This means that login-specific resource files such as .profile, .bash_profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution via the shell's -c option. If no command is specified, an interactive shell is executed.

So the OP initiates a login-shell which only reads the .bash_profile. The way to solve the problem is now to source the .bashrc file in there as is strongly recommended.

# .bash_profile
if [ -n "$BASH" ] && [ -r ~/.bashrc ]; then
    . ~/.bashrc
fi

more info on dot-files:

related posts:

kvantour
  • 25,269
  • 4
  • 47
  • 72
0

I recently had the same problem. In my Dockerfile, I was running:

RUN wp core download && wp plugin install woocommerce --activate --allow-root

I looked at the error message, and thought that from the way it was worded, the --allow-root gets ignored the first time you use it. So I added it to the first wp command, and It worked.

RUN wp core download --allow-root && wp plugin install woocommerce --activate --allow-root
James Douglas
  • 3,328
  • 2
  • 22
  • 43
-1

The problem is that ~/.bashrc is not being sourced. It will only be sourced in an interactive Bash shell.

You might get better results doing it via executables. Something like this:

# install wp cli
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
    chmod +x wp-cli.phar && \
    mv wp-cli.phar /usr/local/bin/wp-cli.phar && \
    echo '#!/bin/sh' >> /usr/local/bin/wp && \
    echo 'wp-cli.phar "$@" --allow-root' >> /usr/local/bin/wp && \
    chmod +x /usr/local/bin/wp
aude
  • 1,372
  • 16
  • 20
  • executing this resulted in `PHP Fatal error: Uncaught PharException: phar "/usr/local/bin/wp" has a broken signature in /usr/local/bin/wp:3` – Alexander Kucheryuk Nov 23 '20 at 18:52
  • I just did a [test](https://gist.github.com/aude/590aa6f8363f237cd5fe2d1791bf5be5) to see if I got the same problem, I sadly did not. It looks like `wp-cli.phar` might have ended up at `/usr/local/bin/wp` instead of the shell script. Could that be have happened? – aude Nov 23 '20 at 23:47