run batch in wordpress docker container - php

I am trying to run batch in wordpress official container.
In container I want to run very simple batch like below:
./wordpress_batch.php
define('BASEPATH', '/path/to/wordpress');
require_once(BASEPATH . '/wp-load.php');
# batch program start
echo "batch test";
var_dump($wpdb);
But no output is shown.
This code work in wordpress in host machine.
What is wrong in docker container?
Any ideas to run this code?
Thanks.
./docker-compose.yml:
version: "2"
services:
wordpress:
build: containers/wordpress
ports:
- "9000:80"
depends_on:
- db
environment:
WORDPRESS_DB_HOST: "db:3306"
env_file: .env
volumes:
- ./wordpress_batch:/var/batch/
db:
build: containers/db
env_file: .env
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
driver: local
./containers/db/Dockerfile:
FROM mysql:latest
./containers/wordpress/Dockerfile:
FROM wordpress:latest
The way I ran the code:
$ docker-compose run wordpress bash
# root#97658bd14387:/var/batch# php wordpress_batch.php

Related

How can I run two PHP scripts using Docker?

I have an application, that work with rabbitmq. There is 2 php scripts (send and receive messages) but i can run only one script using Dockerfile
CMD ["php", "./send.php"]
But i have to run two scripts. My tutor ask me to do two containers for each script:
version: "3"
services:
rabbit_mq:
image: rabbitmq:3-management-alpine
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- ./docker/rabbitmq/data/:/var/lib/rabbitmq/
- ./docker/rabbitmq/log/:/var/log/rabbitmq
- ./docker/rabbitmq/conf/:/var/conf/rabbitmq
environment:
- API_URL=Api:8000
send:
build:
context: './docker'
dockerfile: Dockerfile
image: php:7.4.cli
container_name: send
ports:
- 8000:8000
volumes:
- ./:/app
depends_on:
- rabbit_mq
receive:
image: php:7.4.cli
# build:
# context: './docker'
# dockerfile: Dockerfile
container_name: receive
ports:
- 8001:8001
volumes:
- ./:/app
depends_on:
- rabbit_mq
What can I do to run 2 scripts using "docker-compose up" command? I serf a lot of web-pages, but couldn't find anything, I really need your help!
you did not specify if those scripts terminate process or not, but to run them, you can make docker-compose like this:
version: "3"
services:
rabbit_mq:
# existing configuration
send:
# existing configuration
command: ["php", "./send.php"]
receive:
# existing configuration
command: ["php", "./receive.php"]
If you would like to run it as part of the docker-compose you can add these lines to certain container blocks in the composer file:
command: php send.php
command: php receive.php
https://docs.docker.com/compose/compose-file/#command
If you need more complicated things like restarting on failure, take a look at using `supervisor'.

Docker how to run node js

This is my docker-compose.yml
version: '3.9'
networks:
bedrock:
services:
web:
container_name: kawa-web
image: nginx:stable-alpine
volumes:
- ./:/var/www/html:delegated
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- 8080:80
depends_on:
- php
- mysql
networks:
- bedrock
php:
container_name: kawa-php
image: nanoninja/php-fpm:8.0
volumes:
- ./:/var/www/html
- ./docker/config/php.ini:/usr/local/etc/php/conf.d/php.ini
ports:
- 9000:9000
networks:
- bedrock
mysql:
container_name: kawa-db
image: mysql:8
volumes:
- ./docker/db:/var/lib/mysql:delegated
ports:
- 3306:3306
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
networks:
- bedrock
node:
container_name: kawa-node
build:
context: .
dockerfile: node.dockerfile
volumes:
- ./:/var/www/html
networks:
- bedrock
Content of node.dockerfile
FROM node:18-alpine
WORKDIR /var/www/html
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
When I run docker compose up -d it shows
failed to solve: error from sender: open /path/to/project/docker/db/#innodb_temp: permission denied
How can I fix this? Or any another way to run nodejs inside PHP container maybe?
When your Compose setup has:
services:
mysql:
volumes:
- ./docker/db:/var/lib/mysql:delegated
node:
build:
context: .
The MySQL data directory is in the ./docker/db directory. That's inside the . build-context directory of the Node application, so docker-compose build among other things sends the entire MySQL data to itself, and if the database is currently running, you could get lock or permission problems like this.
The best approach to work around this is to split your application into separate directories, and have each language component only build its own subdirectory.
$ ls -1F
data/
docker-compose.yml
js/
php/
static/
$ ls -1F js
Dockerfile
index.js
node_modules/
package.json
package-lock.json
# docker-compose.yml
version: '3.8'
services:
...
mysql:
image: mysql:8
volumes:
- ./data/db:/var/lib/mysql:delegated
ports:
- 3306:3306
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
node:
build: ./js
(Note that I've used the short syntax for build: to use the default Dockerfile in the context directory; I've removed unnecessary container_name: and networks: options, using the Compose-provided default network; and I've removed the volumes: that overwrite the image's content. Make sure ./js/Dockerfile has a CMD instruction that says how to start the container.)

Cannot Create container source is not directory. Encountered error while bringing up the project

I have written dockerfile like below:
FROM nginx:1.13.8-alpine
RUN apk add --no-cache curl
COPY app/index.php /app
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
And Docker compose file like below:
version : '3.3'
services:
php:
build: ./php/
volumes:
- 'appvolume:/app'
- '/php/local.ini:/usr/local/etc/php/conf.d/local.ini'
working_dir: /app
container_name: php_cli
#mysql service
mysql:
image: mysql:8.0
container_name: mysql-server-80
command : --default-authentication-plugin=mysql_native_password
working_dir: /app
volumes:
- 'dbdata:/var/lib/mysql'
restart: always
environment:
MYSQL_ROOT_PASSWORD : 'duet365'
MYSQL_DATABASE : 'world'
MYSQL_USER : 'db_user'
MYSQL_PASSWORD : 'mypwd'
SERVICE_NAME: 'mysql'
ports:
- '9000:3306'
expose:
- '3306'
app:
build: ./
volumes:
- 'appvolume:/app'
ports:
- '4000:4000'
- '444:443'
links:
- mysql
depends_on:
- php
- mysql
#Volumes
volumes:
dbdata:
driver: local
appvolume:
My project folder is like this:
After running the command docker-compose up I am getting following error:
ERROR: for 8a0cce0f4dbe_php_docker_app_1 Cannot create container for service app: source is not directory
ERROR: for app Cannot create container for service app: source is not directory
ERROR: Encountered errors while bringing up the project.
As I am new in docker, I am not getting where is the mistake. As far I know COPY command is all right.

Docker connection refused between multiple projects

I set up 2 projects (Admin and API) and try to move into docker on local.
I can access the running web instances on both without any problems, but when the Admin tries to make a Curl requests to the API, I get a cURL error:
cURL error 7: Failed to connect to localhost port 8080
This is my docker-compose.yml file contents:
version: "3.1"
services:
memcached:
image: memcached:alpine
container_name: project-admin-memcached
redis:
image: redis:alpine
container_name: project-admin-redis
mariadb:
image: mariadb:10.1
container_name: project-admin-mariadb
working_dir: /application
volumes:
- ./Projects:/application
environment:
- MYSQL_ROOT_PASSWORD=docker
- MYSQL_DATABASE=db_test
- MYSQL_USER=test
- MYSQL_PASSWORD=test
ports:
- "8083:3306"
# docker-compose exec webserver sh
# docker exec -it project-admin-webserver nginx -s reload
webserver:
image: nginx:alpine
container_name: project-admin-webserver
working_dir: /application
volumes:
- ./Projects/Api:/application/api
- ./Projects/Admin:/application/admin
- ./Docker/nginx:/etc/nginx/conf.d
ports:
- "8080:8080"
- "8090:8090"
# docker-compose exec php-fpm bash
php-fpm:
build: Docker/php-fpm
container_name: project-admin-php-fpm
working_dir: /application
volumes:
- ./Projects:/application
- ./Docker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
I can access both projects from my browser with:
http://localhost:8080/ <= API
http://localhost:8090/ <= Admin
How can I fix this?
inside your docker network (create by default with a compose), you have to use the container name.
So inside a container you have to use http://webserver:8080

Docker - how to set up Apache + PHP in docker-compose.yml

I use this to set up nginx for PHP:
nginx:
image: nginx:latest
ports:
- 8080:80
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
image: php:7-fpm
volumes:
- ./code:/code
But how about Apache? How can I set up Apache + PHP in docker-compose.yml?
Following this guide:
version: '2'
services:
php:
build: php
ports:
- "80:80"
- "443:443"
volumes:
- ./php/www:/var/www/html
Error:
ERROR: In file './docker-compose.yml' service 'version' doesn't have any configuration options. All top level keys in your docker-compose.yml must map to a dictionary of configuration options.
Any ideas? I'm on Xubuntu 16.04.
EDIT:
After managing to upgrade docker-compose to 1.9, I try with this file below:
version: '2'
services:
php:
build: php
expose:
- 9000
volumes:
- ./php/www:/var/www/html
apache2:
image: webdevops/apache:latest
args:
- PHP_SOCKET=php:9000
volumes:
- ./php/www:/var/www/html
ports:
- 80:80
- 443:443
links:
- php
Error:
$ sudo docker-compose up -d
Building php
ERROR: Cannot locate specified Dockerfile: Dockerfile
Docker is such as pain!
Any ideas how to fix this?
I would choose webdevops dockerized apache, because it has simple configuration:
version: '2'
services:
php:
build: php
expose:
- 9000
volumes:
- ./php/www:/var/www/html
apache2:
image: webdevops/apache:latest
args:
- PHP_SOCKET=php:9000
volumes:
- ./php/www:/var/www/html
ports:
- 80:80
- 443:443
links:
- php
Since the example above does not work, here is a different approach:
docker-compose.yml
version: '3.1'
services:
php:
image: php:apache
ports:
- 80:80
volumes:
- ./php/www:/var/www/html/
Launch the server with
docker-compose up
We need to create a new folders /php/www in current path
Create a file under php folder save as "Dockerfile" which contains as below without quote
"FROM php:5.6-apache
RUN docker-php-ext-install mysqli"
Copy your docker-compose.yml file in your current folder where your "php" folder has.
Create a sample file "index.php" under www folder (/php/www/index.php)
Run in command prompt docker-compose up -d
Open your browser type "localhost" you can see your sample file results.
Note: Above steps as per above mentioned docker-compose.yml file.
You can check this question.
If you use build instead of image, then you need "Dockerfile". Dockerfile would be use as configuration file for building image.
You maybe miss part in guide, where you should create file with name "Dockerfile" inside directory "php". Directory "php" must be in the same directory, where your "docker-compose.yml". In "docker-compose.yml" you have this line.
build: php
The line mean, that configuration file (by default: "Dockerfile") is inside of directory "php". So you should create directory "php" and file "Dockerfile" inside of it.
This is "Dockerfile" from your guide.
FROM php:5.6-apache
RUN docker-php-ext-install mysqli
docker-compose.yml reference version 2
Dockerfile reference
I found an elegant way to dynamically configure the ports and other parameters: In apache2's configuration files you can reference environment variables.
#/etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
#APACHE_HTTP_PORT_NUMBER:80
#APACHE_HTTPS_PORT_NUMBER:443
Listen ${APACHE_HTTP_PORT_NUMBER}
<IfModule ssl_module>
Listen ${APACHE_HTTPS_PORT_NUMBER}
</IfModule>
<IfModule mod_gnutls.c>
Listen ${APACHE_HTTPS_PORT_NUMBER}
</IfModule>
you can set the variables in Dockerfile or docker-compose.yml
You can set a directory with diferente Dockerfiles an declare in each service:
...
image: php:custom
build:
context: .
dockerfile: ./dockerfiles/Dockerfile-php
...
I have created a working example of PHP, APACHE, MYSQL, and PHPMYADMIN for PHP developers. You may find it useful if you need the original old-school working style. Please note that I am using port 8080 for my website and port 8081 for PHPMyAdmin. You can change these as you like.
version: '3.8'
services:
php-apache-environment:
container_name: php-apache
image: php:7.4-apache
volumes:
- ./php/src:/var/www/html/
ports:
- 8080:80
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: ezapi
MYSQL_USER: root
MYSQL_PASSWORD: password
ports:
- "6033:3306"
volumes:
- dbdata:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
volumes:
dbdata:

Categories