-allow-root在docker容器中运行wp-cli不起作用

问题描述 投票:0回答:2

[在Docker中使用WP CLI时,我需要以root用户身份执行。我需要直接在.bashrc中添加标志--allow-root,我试图弄清为什么它不起作用。

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/

我的.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
}

当我尝试执行任何wp命令时,出现此错误:

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>

似乎该命令行没有考虑我在.bashrc中输入的内容

你们,您对如何解决此问题有任何建议吗?

wordpress bash docker alpine wp-cli
2个回答
0
投票

您需要像这样运行它:

FROM webdevops/php-dev:7.3
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
RUN /bin/bash -c "source ~/.bashrc && wp cli info"

或类似这样:

FROM webdevops/php-dev:7.3
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 '#!/bin/bash\n/usr/local/bin/wp "$@" --allow-root' > /usr/bin/my_wp \
    && chmod +x /usr/bin/my_wp

RUN  my_wp cli info

0
投票

问题是~/.bashrc没有被获取。它只会在交互式Bash Shell中提供。

您可能通过可执行文件获得更好的结果。像这样的东西:

# 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
© www.soinside.com 2019 - 2024. All rights reserved.