I have been using the following docker-compose.yml to build a WordPress environment. Everything has been working fine, except when I attempt to use PHP function easter_date() I receive an undefined function error.
Could anyone explain to me what I need to add to the YML file in order to include the PHP easter_date() extension?
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- ./data:/docker-entrypoint-initdb.d # This will import DB data from an sql file in your /data folder
- ./data/migrate.sql:/docker-entrypoint-initdb.d/migrate.sql # run sql commands in migrate.sql to update site urls in DB
restart: always
ports:
- "3400:3306" # mapping our ports for networking
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
ports:
- "8080:80"
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: wordpress
DOCKER_COMPOSE_YML_LOCATION: wordpress
container_name: wp_phpmyadmin
wordpress:
build:
context: .
depends_on:
- db
image: wordpress:latest
ports:
- "3500:80" # mapping our ports for networking
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DEBUG: 1
WORDPRESS_MEMORY_LIMIT: 256MB
working_dir: /var/www/html
volumes: # this is where we tell Docker what to pay attention to
- ./wp-content:/var/www/html/wp-content
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini # create custom uploads.ini which has an increased upload file size limit, memory etc.
volumes:
db_data: {}
I have attempted to include PHP extensions by inserting the following two lines:
php: #https://stackoverflow.com/questions/46401966/install-packages-from-docker-compose-yml-into-docker-container
build: './docker/php'
And then created a Dockerfile inside ./docker/php with
FROM php:7.1-fpm
RUN docker-php-ext-install calendar && docker-php-ext-configure calendar
But I receive ERROR: Cannot locate specified Dockerfile: Dockerfile when I run docker-compose build
Related
I would like to run my Wordpress site on Docker, and I want to connect the Wordpress database to another container which have only the databases of all my sites.
For doing so, I've created a LAMP container using the following docker-compose.yml:
version: "3"
services:
web:
image: webdevops/php-apache:alpine-php7
ports:
- "4500:80"
volumes:
- ./www:/app
- ./uploads.ini:/opt/docker/etc/php/php.ini
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "8088:80"
as you can see I've installed Apache as service using the webdevops image, this return the following:
Then, I've created a new container which have the Wordpress instance:
version: '3'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./wp/wp-content:/var/www/html/wp-content
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: lamp_db_1:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
volumes:
dbdata:
wp-content:
as you can see I mount the wp-content folder since I already have a Wordpress installation with plugins and media... then I tried to connect this container to lamp_db_1 container but when I run this using:
docker-compose up --build
I get:
MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known
what I did wrong?
How can I connect the wordpress container to the LAMP container?
You can specify a custom network in your LAMP stack and have your Wordpress stack and other containers defined in other Compose files use this network by using network.external parameter in Compose.
Being on the same network, you'll be able to join your lamp_db container using it's container name as hostname.
A complete example:
docker-compose.yml for LAMP stack:
version: '3.5'
services:
db:
# This will be the hostname for your DB container on the network
container_name: lamp_db
image: mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
# Have db container join our db network
networks:
- db_network
# Ensure our custom network is managed by this stack
networks:
db_network:
name: db_network
docker-compose.yml for Wordpress stack:
version: '3.5'
services:
wordpress:
image: wordpress:latest
ports:
- "8000:80"
environment:
# Name of the db container
WORDPRESS_DB_HOST: lamp_db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
# Have wordpress container join our db network
networks:
- db_network
# Declare db_network as external with it's name
# Network won't be created by this stack but must exists before running
# See https://docs.docker.com/compose/compose-file/#external-1
networks:
db_network:
external: true
name: db_network
And a generic example any service that would need to use our database:
version: '3.5'
services:
some_container:
image: some-image:latest
# Have this container join our db network
networks:
- db_network
# Note: make sure to reference db container by it's hostname lamp_db
networks:
db_network:
external: true
name: db_network
Notes:
db_network is managed via LAMP stack. When uping the LAMP stack, Docker Compose will ensure this network is created. Other stacks using this network should declare it as external: true with it's name
You'll need version: '3.5' or more to be able to use network.name config
It seems the reason you are running into this issue is due to the fact that you have two separate services. One contains your LAMP stack and the other contains your wordpress image.
When you run docker-compose it sets up a single network for your app. Thus your wordpress image isn't on the same network as your LAMP stack.
You can add your wordpress container to your LAMP docker-compose.yml file such as
version: "3"
services:
web:
image: webdevops/php-apache:alpine-php7
ports:
- "4500:80"
volumes:
- ./www:/app
- ./uploads.ini:/opt/docker/etc/php/php.ini
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "8088:80"
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./wp/wp-content:/var/www/html/wp-content
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
volumes:
dbdata:
wp-content:
And unless you are doing something specific with your php-apache image you can actually remove it at this point.
Another approach you could take if you do not want everything in the same docker-compose file is that you can manually define the networks and get your containers to join them. The answers on this question show how to do that fairly easily.
I've run a container with docker-compose following this Docker page, that use this docker-compose.yml file:
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
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
The point is, I want to connect to a local database and needs to change the default DB connection from MySQL to a PostgreSQL DB. I've used the docker environment variables, setting the WORDPRESS_DB_HOST/USER/PASSWORD/NAME in wordpress section, but in the end Wordpress states that connection can't be established.
And I want to create my own Wordpress theme but I don't know where the folder is created to put the theme files there.
Any help will be grateful. Thanks in advance.
If you don't already have the Postgres Docker Image running (assuming you're wanting to also run that as a Docker image, and not installed directly on your machine), you'll need to get it all set up first so you know all of it's settings that you'll need.
After that, you'll need to edit your dockerfile to look something to this:
version: '3.3'
services:
db:
image: postgres
volumes:
- db_data:/var/lib/postgres
restart: always
environment:
<the environment vars for postres>
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Regarding where to place your custom theme(s), it should be in your my_wordpress_wordpress_1 volume (or whichever one it automatically created. I don't have WP installed in a docker machine so I don't know firsthand how they place the data in that volume, but typically WP's file tree looks like this: wp-content\themes
I have installed laradoc as per documentation but I am unable to login phpmyadmin.
here is my docker-compose.yaml file:
phpmyadmin:
build: ./phpmyadmin
environment:
- PMA_ARBITRARY=1
- MYSQL_USER=${PMA_USER}
- MYSQL_PASSWORD=${PMA_PASSWORD}
- MYSQL_ROOT_PASSWORD=${PMA_ROOT_PASSWORD}
ports:
- "${PMA_PORT}:80"
depends_on:
- "${PMA_DB_ENGINE}"
networks:
- frontend
- backend
and here is .env file
PMA_DB_ENGINE=mysql
PMA_USER=default
PMA_PASSWORD=secret
PMA_ROOT_PASSWORD=secret
PMA_PORT=8080
Difficult to reproduce the error from the given info but this small setup works so maybe you can find what went wrong?
First bring your existing stack down and delete dangling volumes:
$ docker-compose down
$ docker volume prune
Start the following docker-compose.yaml:
version: '3.1'
services:
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- MYSQL_USER=${PMA_USER}
- MYSQL_PASSWORD=${PMA_PASSWORD}
- MYSQL_ROOT_PASSWORD=${PMA_ROOT_PASSWORD}
ports:
- "${PMA_PORT}:80"
depends_on:
- "${PMA_DB_ENGINE}"
networks:
- frontend
- backend
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: db
MYSQL_USER: default
MYSQL_PASSWORD: secret
networks:
- frontend
- backend
networks:
frontend:
backend:
info (I had to use mysql5.7 otherwise I was facing this bug:
docker-compose up -d
Creating network "test_frontend" with the default driver
Creating network "test_backend" with the default driver
Creating test_mysql_1 ... done
Creating test_phpmyadmin_1 ... done
I know I'm a bit 'messing' with the syntax + you can use the env vars too for mysql but I had to be quick.
I have a Wordpress site on live server and I want to create a LAMP stack locally with Docker to test things.
I pull the images of php:7.0-apache and mysql:5.7, the same versions on live.
I create a MySQL container:
docker run -d --name achi-mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
I create a php & apache container and link it with MySQL:
docker run -d --name achi-php-apache --link achi-mysql:mysql -p 8080:80 -v /home/achi/workspace/web/wordpress-template/:/var/www/html php:7.0-apache
I get the following error on localhost:8080:
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/wp-includes/wp-db.php:1564 [...]
Do I link these two containers the wrong way?
Your problem is not the connection between your containers. The problem is your PHP / Apache container which doesn't support mysqli (or PDO MySQL). WordPress can't find another function to connect with a MySQL database or your MySQL container. Instead WordPress is using a deprecated and removed (since PHP 7.0) mysql_ function per default. You need to install at least mysqli on your PHP container (explained below).
I also recommend to use a docker-compose file to install and run all containers with one command.
To create the containers you want, you can use the following docker-compose.yml file:
version: "3"
services:
achi-php-apache:
build:
context: ./
container_name: achi-php-apache
ports:
- "8080:80"
volumes:
- /home/achi/workspace/web/wordpress-template:/var/www/html:rw
depends_on:
- achi-mysql
networks:
- wp-net
achi-mysql:
image: mysql:5.7
container_name: achi-mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wp-dbname
volumes:
- wp-mysql-data:/var/lib/mysql
networks:
- wp-net
networks:
wp-net:
driver: bridge
volumes:
wp-mysql-data:
You need the following Dockerfile on the same directory as the docker-compose.yml file:
FROM php:7.0-apache
RUN docker-php-ext-install -j$(nproc) mysqli
This Dockerfile installs the missing mysqli extension so WordPress can use it.
You can also use PDO MySQL instead of mysqli. In this case you can use the following Dockerfile:
FROM php:7.0-apache
RUN docker-php-ext-install -j$(nproc) pdo
RUN docker-php-ext-install -j$(nproc) pdo_mysql
Now you can execute the command docker-compose up inside the folder where the docker-compose.yml file is located. After creating the container and running you should be able to access the WordPress site (<ip-or-hostname>:8080).
On the wp-config.php file you need to use the following constants:
define('DB_NAME', 'wp-dbname');
define('DB_USER', 'root');
define('DB_PASSWORD', '123456');
define('DB_HOST', 'achi-mysql');
You can also use the official WordPress image to install WordPress. In this case you can use the following docker-compose.yml file:
version: "3"
services:
achi-php-apache:
image: wordpress:4.9.4-php7.0-apache
container_name: achi-php-apache
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: achi-mysql
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: 123456
WORDPRESS_DB_NAME: wp-dbname
volumes:
- /home/achi/workspace/web/wordpress-template:/var/www/html:rw
depends_on:
- achi-mysql
networks:
- wp-net
achi-mysql:
image: mysql:5.7
container_name: achi-mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wp-dbname
volumes:
- wp-mysql-data:/var/lib/mysql
networks:
- wp-net
networks:
wp-net:
driver: bridge
volumes:
wp-mysql-data:
The simplest way is to use docker-compose to link all your docker instances together rather than linking through the docker command. Here is a sample docker-compose.yml file that should do what you want:
version: '2'
services:
achi-php-apache:
image: php:7.0-apache
ports:
- "8080:80"
volumes:
- /home/achi/workspace/web/wordpress-template/:/var/www/html
links:
- achi-mysql
achi-mysql:
image: mysql:5.7
volumes:
- /var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: someuser
MYSQL_PASSWORD: somepassword
MYSQL_DATABASE: somedefaultdatabase
So I have a trouble with mount new format version 2 docker compose file.
I have the code in folder wordpress inside where is location docker-compose file also I have inside the folder code docker file like this:
FROM debian:jessie
VOLUME /var/www/wordpress
When I used old format like this:
application:
build: code
volumes:
- ./wordpress:/var/www/wordpress
- ./logs/wordpress:/var/www/wordpress/app/logs
tty: true
db:
image: mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: somename
MYSQL_USER: root
MYSQL_PASSWORD: root
php:
build: php-fpm
ports:
- 9001:9001
volumes_from:
- application
links:
- db
nginx:
build: nginx
ports:
- 8080:80
links:
- php
volumes_from:
- application
volumes:
- ./logs/nginx/:/var/log/nginx
elk:
image: willdurand/elk
ports:
- 81:80
volumes:
- ./elk/logstash:/etc/logstash
- ./elk/logstash/patterns:/opt/logstash/patterns
volumes_from:
- application
- php
- nginx
When I started use version '2' the same as code as previous version I got any error so I reformat my compose file and move dockerfile from code folder to insider main folder where is location docker-compose file. My new version docker-compose became looks like as:
version: '2'
services:
web:
build: .
volumes:
- /wordpress:/var/www/wordpress
- /logs/wordpress:/var/www/wordpress/app/logs
tty: true
db:
image: mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: somename
MYSQL_USER: root
MYSQL_PASSWORD: root
php:
build: php-fpm
ports:
- 9001:9001
volumes_from:
- web
links:
- db
nginx:
build: nginx
ports:
- 82:82
links:
- php
volumes_from:
- web
volumes:
- /logs/nginx/:/var/log/nginx
elk:
image: willdurand/elk
ports:
- 81:80
volumes:
- /elk/logstash:/etc/logstash
- /elk/logstash/patterns:/opt/logstash/patterns
volumes_from:
- web
- php
- nginx
Finally after reformat the code docker-compose was successfully build and up but when I open my php and nginx container inside both of them in /var/www/worpdress I have just empty folder app is not my wordpress project.
In which place I was mistake with settings mount project volume?
Thanks in advance.
The problem is with the way you are defining the local directories to be used for the volumes. In your previous version, you were using ./wordpress, while in the new one, you're using just /wordpress.
When referencing local directories for volume mappings, they always have to start with ./ - please try this:
version: '2'
services:
web:
build: .
volumes:
- ./wordpress:/var/www/wordpress
- ./logs/wordpress:/var/www/wordpress/app/logs
One more thing: I recommend to always enclose the volume mappings in double quotes to avoid issues with space characters, e.g.:
version: '2'
services:
web:
build: .
volumes:
- "./wordpress:/var/www/wordpress"
- "./logs/wordpress:/var/www/wordpress/app/logs"