1

I am new to Docker. I know there are lot of answers out there. I tried this link host to container But I can't solve my issue with that. I am creating a docker for WordPress with WordPress cli image.

Here it is:

version: '3.1'

services:
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - wordpress_files:/var/www/html
     ports:
       - "8080:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: user
       WORDPRESS_DB_PASSWORD: password
       WORDPRESS_DB_NAME: wordpressdb

   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_RANDOM_ROOT_PASSWORD: 1
       MYSQL_DATABASE: wordpressdb
       MYSQL_USER: user
       MYSQL_PASSWORD: password

volumes:
    wordpress_files:
    db_data:

In above code, I am using a official WordPress image which is connected with MySQL and it was created successfully. Next I want to install WordPress cli in that WordPress image. Here are the commands I found to install WordPress cli.

echo "Installing WP-CLI"  
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

I tried in the direct way by putting the above commands in the command section in Docker file. But it failed. So I saved the contents in the install.sh file in the host.

Next I want to transfer the file to WordPress image and the file should be triggered after installing the WordPress image and it should install cli in that image by using the file.

Here the code I modified:

version: '3.1'

services:
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - wordpress_files:/var/www/html
     ports:
       - "8080:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: user
       WORDPRESS_DB_PASSWORD: password
       WORDPRESS_DB_NAME: wordpressdb
     COPY /files/install.sh  /var/www/html/   =>modified
     command:                                 =>modified
       /var/www/html/files/install.sh         =>modified
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_RANDOM_ROOT_PASSWORD: 1
       MYSQL_DATABASE: wordpressdb
       MYSQL_USER: user
       MYSQL_PASSWORD: password

volumes:
    wordpress_files:
    db_data:

Error:

ERROR: yaml.scanner.ScannerError: while scanning a simple key
  in "./word_press_docker_file.yml", line 18, column 6
could not find expected ':'
  in "./word_press_docker_file.yml", line 19, column 6

But it again fails. I tried several Stack Overflow answers but unable to figure it out. I tried the COPY command but it fails.

halfer
  • 19,824
  • 17
  • 99
  • 186
cheeze yes
  • 95
  • 1
  • 8

2 Answers2

1

One way of achieving this is via Dockerfile.

A sample Dockerfile for your case may look like this:

FROM wordpress:latest
RUN cd /tmp && echo "Installing WP-CLI" && 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

Then you can build this as a new Custom Image and use in your YAML File.

Edit:

Use of Local images is no longer permitted. So, you can use the docker tag command to tag the image and then use in YAML file.

You can read more about this here: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

One other way of doing this is mentioned here: https://hub.docker.com/_/wordpress/

This image variant does not contain WordPress itself, but instead contains WP-CLI.

The simplest way to use it with an existing WordPress container would be something similar to the following:

$ docker run -it --rm \
    --volumes-from some-wordpress \
    --network container:some-wordpress \
    wordpress:cli user list

Generally speaking, for WP-CLI to interact with a WordPress install, it needs access to the on-disk files of the WordPress install, and access to the database (and the easiest way to accomplish that such that wp-config.php does not require changes is to simply join the networking context of the existing and presumably working WordPress container, but there are many other ways to accomplish that which will be left as an exercise for the reader).
Punit Narang
  • 46
  • 1
  • 6
  • Use of Local images is no longer permitted. So, you can use the docker tag command to tag the image and then use in YAML file. – Punit Narang Aug 30 '19 at 12:41
  • I have created a `wordpress_cli_image` . I tried adding the image in the yml file. After entering the docker container, it show wp not found. can u show it in the yml format. How to use it in the above image in my yml file which i asked in question. – cheeze yes Aug 30 '19 at 12:58
-1

You can try using the RUN instruction as follows:

RUN echo "Installing WP-CLI" \
&& 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