WP-CLI inside Docker container

Feb 25, 2018 articles, wordpress

So running wp-cli inside docker gives you a warning that you are running as root. For me I am running docker locally as a development server and running as root isn’t a concern as I am on a private secure network and it’s purely local development. So the default setup for Docker is to run as root, and to have to add the parameter to every wp command is not something I want to do!

Here is a quick trick to get wp-cli running as root on Docker. (inside your dockerfile when creating a container/image)

# wpcli
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

Im basically writing a function to the bashrc profile to include the --allow-root flag for me.