Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

--allow-root doesn't work running wp-cli in docker container

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?

like image 600
fromthestone Avatar asked Sep 13 '25 09:09

fromthestone


1 Answers

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:

  • http://mywiki.wooledge.org/DotFiles
  • man bash
  • What's the difference between .bashrc, .bash_profile, and .environment?
  • About .bash_profile, .bashrc, and where should alias be written in?

related posts:

like image 185
kvantour Avatar answered Sep 16 '25 01:09

kvantour