3

Running wordpress on docker. Trying to skip install install.php by defining user in compose file. I have already added wordpress user , password and title to skip that page. but not working.

Is there some problem with my code? Is there another way to do it. or help me fix this code.

version: '3.3'

services:
    db:
        image: mysql:5.7
        volumes:
        - ./db-data:/var/lib/mysql
        restart: always
        environment:
        MYSQL_ROOT_PASSWORD: somewordpress
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress

    wordpress:
        depends_on:
        - db
        image: wordpress:latest
        volumes:
        - ./wordpress:/var/www/html
        - ./docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro
        ports:
        - "8000:80"
        restart: always
        environment:
        WORDPRESS_VERSION: 5.1
        WORDPRESS_LOCALE: en_US
        WORDPRESS_DB_HOST: db:3306
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: wordpress
        WORDPRESS_TABLE_PREFIX: "wp_"
        WORDPRESS_DEBUG: 1
        WORDPRESS_DB_NAME: wordpress
        # WORDPRESS_WEBSITE_TITLE: "My blog"
        # WORDPRESS_WEBSITE_URL: "http://example.com"
        # WORDPRESS_WEBSITE_URL_WITHOUT_HTTP: "example.com"
        # WORDPRESS_WEBSITE_URL: "http://http://localhost:8000"
        # WORDPRESS_WEBSITE_URL_WITHOUT_HTTP: "localhost"
        # WORDPRESS_WEBSITE_POST_URL_STRUCTURE: "/%year%/%monthnum%/%day%/%postname%/"
        # WORDPRESS_ADMIN_USER: "admin"
        # WORDPRESS_ADMIN_PASSWORD: "admin"
        # WORDPRESS_ADMIN_EMAIL: "admin@admin.com"

        working_dir: /var/www/html   

    wordpress-cli:
        depends_on:
        - db
        - wordpress
        image: wordpress:cli
        entrypoint: wp
        user: xfs
        command: >
        /bin/sh -c ' sleep 10; 
        wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you@domain.com '

        volumes:
        - ./wordpress:/var/www/html
        - ./docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro


volumes:
    db_data: {}
    wordpress:

Got this error:

C:\DockerProjects\test6>docker-compose up
ERROR: yaml.scanner.ScannerError: while scanning a simple key
  in ".\docker-compose.yml", line 54, column 6
could not find expected ':'
  in ".\docker-compose.yml", line 55, column 6

3 Answers3

1

Problem is in this part of code

 user: xfs
        command: >
        /bin/sh -c ' sleep 10; 
        wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you@domain.com '

It has to be like this

user: xfs
        command: >
            /bin/sh -c ' sleep 10; 
            wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you@domain.com '

after command: > next 2 lines you need to give 1 Tab.

You can check your code with online formatter here.

Edit 1:

Formatted Code looks like this

version: '3.3'
services:
    db:
        image: 'mysql:5.7'
        volumes:
            - './db-data:/var/lib/mysql'
        restart: always
        environment: null
        MYSQL_ROOT_PASSWORD: somewordpress
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress
    wordpress:
        depends_on:
            - db
        image: 'wordpress:latest'
        volumes:
            - './wordpress:/var/www/html'
            - './docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro'
        ports:
            - '8000:80'
        restart: always
        environment: null
        WORDPRESS_VERSION: 5.1
        WORDPRESS_LOCALE: en_US
        WORDPRESS_DB_HOST: 'db:3306'
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: wordpress
        WORDPRESS_TABLE_PREFIX: wp_
        WORDPRESS_DEBUG: 1
        WORDPRESS_DB_NAME: wordpress
        working_dir: /var/www/html
    wordpress-cli:
        depends_on:
            - db
            - wordpress
        image: 'wordpress:cli'
        entrypoint: wp
        user: xfs
        command: "/bin/sh -c ' sleep 10;  wp core install --url=\"http://localhost:8000\" --title=\"Sample Title\" --admin_name=admin --admin_password=admin --admin_email=you@domain.com '\n"
        volumes:
            - './wordpress:/var/www/html'
            - './docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro'
volumes:
    db_data: {}
    wordpress: null
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
  • C:\DockerProjects\test6>docker-compose up ERROR: yaml.scanner.ScannerError: mapping values are not allowed here in ".\docker-compose.yml", line 42, column 14 +++++++++++++++++++++++++++++++++++++++++++++ 41: user: xfs 42: command: > 43: /bin/sh -c ' sleep 10; 44: wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you@domain.com ' –  Jun 11 '19 at 11:01
  • code worked. but install.php page is still there, which i am trying to skip. –  Jun 11 '19 at 11:23
  • This was very helpful, but the problem is if the database is not yet ready, the CLI commands fail... The sleep is not dependable, and making it arbitrarily large like 60 is a hack. Is there a way to make it wait for the db to be ready for connections? – lonix Nov 03 '19 at 05:43
  • You can try adding health check as mentioned here https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y/41854997#41854997 or add depends_on feature – MyTwoCents Nov 03 '19 at 05:47
0

You can install wordpress Cli either in the compose file. or you can copy the wp cli into the image using dockerfile and install wordpress using custom entrypoint.

0

I've found this on dockerhub:

Since March 2021, WordPress images use a customized wp-config.php that pulls the values directly from the environment variables defined above (see wp-config-docker.php in docker-library/wordpress#572 and docker-library/wordpress#577). As a result of reading environment variables directly, the cli container also needs the same set of environment variables to properly evaluate wp-config.php.

So you'd need to set the environment like in your original wordpress, like below (which worked for me, also using traefik..):

version: "3.9"

services:
  db:
    image: mysql:5.7
    volumes:
      - ./db:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    restart: always

    environment: &env
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_CONFIG_EXTRA: |
        define( 'AUTOMATIC_UPDATER_DISABLED', true );
        define( 'FS_METHOD', 'direct' );
    volumes:
      - &html
        ./html:/var/www/html
    labels: &lab
      - "traefik.enable=true"
      - "traefik.port=80"
      - "traefik.frontend.rule=Host:subdomain.domain.com"
    networks:
      - default
      - web

  wordpress-cli:
    image: wordpress:cli
    depends_on:
      - db
      - wordpress
    environment:
      <<: *env
    volumes:
      - *html
    entrypoint: sh
    command: -c 'sleep 10; wp core install --url="https://subdomain.domain.com" --title="greatname" --admin_name=admin --admin_password="mysupersecureadminpw" --admin_email=my@domain.com'

networks:
  web:
    external: true

You can also leave out the entrypoint and command part and just run docker-compose run wordpress-cli core install ... after you checked that everything is running. If you remove the entrypoint part, you can also run any other wp-cli command instead of core install that way :-)

exic
  • 2,220
  • 1
  • 22
  • 29