Can i use wp cli in my host for wordpress

Hello, I set up a WordPress environment. I installed WordPress, Nginx, and MariaDB images.

Now, I want to use WP-CLI from my host because I don’t want to go inside the container to run my WP-CLI commands; it’s too slow.

That’s why I want to use WP-CLI from my host while creating a WordPress volume for local access.

No, you can’t. But what you can do is to run a docker compose exec statement followed by the wp CLI command. The command will still be fired in the container but you will have the feeling you’re still on your host.

1 Like

To make it feel like it would be a host command, you could create an alias (works on Mac, Linux, and even Windows PowerShell) and hide the actual command behind the alias.

A short google search should show how aliases can be created for the shell you are using.

1 Like

services:
  nginx:
    container_name: nginx
    image: nginx:1.27.4-alpine
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./nginx:/etc/nginx/conf.d:ro
      - ./wordpress:/var/www/html
    depends_on:
      - wordpress
    networks:
      - internal

  mariadb:
    container_name: mariadb
    image: mariadb:10.6
    restart: unless-stopped
    environment:
      MARIADB_DATABASE: ${MYSQL_DATABASE}
      MARIADB_USER: ${MYSQL_USER}
      MARIADB_PASSWORD: ${MYSQL_PASSWORD}
      MARIADB_ROOT_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - db:/var/lib/mysql
    networks:
      - internal

  wordpress:
    container_name: wordpress
    depends_on:
      - mariadb
    image: wordpress:fpm
    restart: unless-stopped
    environment:
      WORDPRESS_DB_HOST: mariadb
      WORDPRESS_DB_USER: ${WORDPRESS_DB_USER}
      WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
      WORDPRESS_DB_NAME: ${WORDPRESS_DB_NAME}
    volumes:
      - ./wordpress:/var/www/html/
    networks:
      - internal

  wpcli:
    container_name: wp-cli
    image: wordpress:cli
    depends_on:
      - mariadb
      - wordpress
    environment:
      WORDPRESS_DB_HOST: mariadb
      WORDPRESS_DB_USER: ${WORDPRESS_DB_USER}
      WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
      WORDPRESS_DB_NAME: ${WORDPRESS_DB_NAME}
    volumes:
      - ./wordpress:/var/www/html/
    working_dir: /var/www/html
    tty: true
    entrypoint: ["wp"]
    networks:
      - internal

  adminer:
    container_name: adminer
    image: adminer
    restart: unless-stopped
    ports:
      - "8081:8080"
    depends_on:
      - mariadb
    networks:
      - internal

networks:
  internal:
    name: internal
    driver: bridge

volumes:

  db:
    name: db

I have this now, but I can’t use the wp-cli command. it says wp-cli not running

You don’t give any details…perhaps can you find something useful here Using wp-cli with Docker. Earlier this week, I posted a tutorial… | by A. Tate Barber | Medium

2 Likes

The shared link gives the right ideas.

The only thing I would do different probably is to use the official wordpress:cli image, instead of the custom image:

Update: I missed that you already use the wordpress:cli image. The container you create from it is the one you need to exec into.

1 Like
wp-cli plugin install woocommerce --activate
Installing WooCommerce (9.6.2)
Downloading installation package from https://downloads.wordpress.org/plugin/woocommerce.9.6.2.zip...
Using cached file '/home/www-data/.wp-cli/cache/plugin/woocommerce-9.6.2.zip'...
Unpacking the package...
Warning: Could not create directory. "/var/www/html/wp-content/upgrade"
Warning: The 'woocommerce' plugin could not be found.
Error: No plugins installed.

I have this issue now
i try to run “wp-cli plugin install woocommerce --activate”
But it’s sayes “Warning: Could not create directory. “/var/www/html/wp-content/upgrade”
Warning: The ‘woocommerce’ plugin could not be found.
Error: No plugins installed.”

That’s most likely to be caused by this: www-data user mismatch cli / wordpress images · Issue #256 · docker-library/wordpress · GitHub. The wpcli image requires the user to be set to the same userid and gid as wordpress uses.

Add user: '33:33' to the wpcli service definition, to make sure the wpcli is executed with the uid:gid that grants it access to the data in the volume.

1 Like
 wp-cli:
    container_name: wp-cli
    image: wordpress:cli
    user: "33:33"
    depends_on:
      - mariadb
      - wordpress
    environment:
      WORDPRESS_DB_HOST: mariadb
      WORDPRESS_DB_USER: ${WORDPRESS_DB_USER}
      WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
      WORDPRESS_DB_NAME: ${WORDPRESS_DB_NAME}
    volumes:
      - ./wordpress:/var/www/html/
    working_dir: /var/www/html
    tty: true
    entrypoint: ["wp"]
    networks:
      - internal

like this ?

If the uid:gid is correct, then yes :slight_smile:

I read in the image description that this might be necessary, with a pointer to the link I shared in my previous post.

Thank you for your help. It really means a lot to me; now Semmes is working. However, I get a weird error when I run my code.

 
PHP Warning:  foreach() argument must be of type array|object, string given in /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-countries.php on line 308
Warning: foreach() argument must be of type array|object, 
> string given in /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-countries.php on line 308

 

Great that it’s working so far. Though, I can’t really say anything regarding wp or wp-cli, as I don’t use them.

I cleaned up all containers, images, and volumes, and now it’s working. :raised_hands:

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.