Marinus Klasen

Marinus Klasen

  • Blog
  • Contact

January 1, 2022

Adding and using WP-CLI in a docker-compose setup

Marinus Klasen

For most of my WordPress projects I use my own Alcedo WordPress workflow. With some extra scripts, creating a new site is as easy as running 'create site.c7'in the terminal. This sets up a full WordPress install with local domains and certificates.

But, in this case i’m using a different setup in which I use the default wordpress:php8.0 docker image. As i’m using the standard setup, I also wanted to use the default wordpress:cli docker image.

How does that work?

As they say, an image says more then a thousands words. But, since text in images can’t be copied, I’ll copy/paste the two services below:

  wp:
    container_name: ${NAME}-wp
    depends_on:
      - db
    image: wordpress:php8.0
    user: 1000:1000
    volumes:
      - ./app/www/content/plugins:/var/www/html/wp-content/plugins
      - ./app/www/content/mu-plugins:/var/www/html/wp-content/mu-plugins
      - ./app/www/content/themes:/var/www/html/wp-content/themes
      - uploads:/var/www/html/wp-content/uploads:rw
      - wordpress:/var/www/html
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: ${NAME}-db:3306
      WORDPRESS_DB_USER: ${DB_USER}
      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
      WORDPRESS_DB_NAME: ${DB_NAME}
      WORDPRESS_DEBUG: 1
      VIRTUAL_HOST: "${SITE_DOMAIN}" # Used for nginx-proxy
      VIRTUAL_PORT: 80 # Used for nginx-proxy
      LETSENCRYPT_HOST: "${SITE_DOMAIN}" # Used for nginx-proxy
      WORDPRESS_CONFIG_EXTRA: |
        /* Multisite */
        define( 'MULTISITE', true );
        define( 'SUBDOMAIN_INSTALL', true );
        define( 'DOMAIN_CURRENT_SITE', 'sitec7' );
        define( 'PATH_CURRENT_SITE', '/' );
        define( 'SITE_ID_CURRENT_SITE', 1 );
        define( 'BLOG_ID_CURRENT_SITE', 1 );
    expose:
      - "80"
  
  wpcli:
    container_name: ${NAME}-cli
    depends_on:
      - wp
    image: wordpress:cli
    user: 1000:1000
    command: tail -f /dev/null
    volumes:
      - wordpress:/var/www/html
    environment:
      WORDPRESS_DB_HOST: ${NAME}-db:3306
      WORDPRESS_DB_USER: ${DB_USER}
      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
      WORDPRESS_DB_NAME: ${DB_NAME}
    profiles:
      - dev

As you see, both images use the same volume and environment setup. This allows you to run commands like:

docker exec site-cli wp --info

and

docker exec site-cli wp search-replace "test" "test1" --dry-run

Want to make it even easier? Set an alias for docker exec site-cli wp so that you can run something like sitewp --info. In my own development environment, my folders are named the same as my container. When you change directory, and the directory has an .env file, an alias is set for wp and links to the correct container.

Happy developing!

About Marinus Klasen

Marinus has been working in software/web development for more than a decade. Since 2020 his attention shifted on sharing knowledge and developing products and tools for sharing knowledge.

Marinus Klasen on Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Ready to take action?

I'm looking forward to discuss your projects and goals!
Feel free to reach out using the contact details below.

Marinus Klasen

[email protected]
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

Have you read?

  • Remove all edit post links in WordPress with a simple filterMay 11, 2022
  • Registering block styles in WordPress with PHPMay 11, 2022
  • Host key verification failed when connecting over SSH?April 20, 2022
  • Run VS Code server (Remote SSH) on AlpineApril 19, 2022
  • Show the errors and notices from WordPress in DockerApril 18, 2022

Copyright © 2022 · Marinus Klasen | Webdesign by Team Rood