I have a wordpress docker-compose, which contains 3 services.
1 – php,apache
2 – mysql
3 – phpmyadmin
what I want to do is install wordpress core and plugins at build time,
and the reason is obvious I don’t want everytime I restart my containers I goes to all steps all over again and install plugins and … . so I need connection to database but It seems that build time I can’t access my mysql container.
I read somewhere that I need to specify network on build stage but I couldn’t make it work.
and here is my docker-compose file :
version: '3.8'
volumes:
mhndev_systems_mysql_data:
mhndev_systems_wp_uploads:
networks:
mhndev_network:
services:
## --------------------------------------------
## | 1: WordPress
## --------------------------------------------
mhndev_systems_wp:
build:
context: .
dockerfile: docker/Dockerfile
args:
WP_VERSION: ${WP_VERSION}
MYSQL_HOST: ${MYSQL_HOST}
MYSQL_PORT: ${MYSQL_PORT}
MYSQL_DATABASE_NAME: ${MYSQL_DATABASE_NAME}
MYSQL_USERNAME: ${MYSQL_USERNAME}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
SITE_URL: ${SITE_URL}
DB_TABLE_PREFIX: ${DB_TABLE_PREFIX}
ENV: ${ENV}
UID: ${UID}
GID: ${GID}
SITE_TITLE: ${SITE_TITLE}
SITE_ADMIN_USERNAME: ${SITE_ADMIN_USERNAME}
SITE_ADMIN_PASSWORD: ${SITE_ADMIN_PASSWORD}
SITE_ADMIN_EMAIL: ${SITE_ADMIN_EMAIL}
network: "mhndev_network"
ports:
- 8191:80
env_file:
- .env
volumes:
- ./themes/dt-the7-child:/var/www/html/wp-content/themes/dt-the7-child
- ./plugins/teamcity:/var/www/html/wp-content/plugins/teamcity
- mhndev_systems_wp_uploads:/var/www/html/wp-content/uploads
depends_on:
- mhndev_systems_mysql
networks:
- mhndev_network
## --------------------------------------------
## | 2: Mysql
## --------------------------------------------
mhndev_systems_mysql:
image: mysql:5.7.21
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE_NAME}
MYSQL_USER: ${MYSQL_USERNAME}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- mhndev_systems_mysql_data:/var/lib/mysql
networks:
- mhndev_network
## --------------------------------------------
## | 3: PhpMyAdmin
## --------------------------------------------
mhndev_systems_phpmyadmin:
image: phpmyadmin/phpmyadmin:5.0.2
depends_on:
- mhndev_systems_mysql
ports:
- "7191:80"
environment:
PMA_HOST: mhndev_systems_mysql
networks:
- mhndev_network
here is my Dockerfile :
FROM php:7.4-apache
ARG WP_VERSION=5.5.1
RUN apt-get update && apt-get install -y
sendmail
libpng-dev
libjpeg-dev
libfreetype6-dev
netcat
gnupg
libzip-dev
zip
&& docker-php-ext-configure gd --with-freetype --with-jpeg
&& docker-php-ext-install gd pdo_mysql zip
&& docker-php-ext-install mysqli && docker-php-ext-enable mysqli
ADD ./docker/apache.conf /etc/apache2/sites-enabled/000-default.conf
RUN
printf "nServerName localhost" >> /etc/apache2/apache2.conf &&
a2enmod rewrite expires
### install wp cli
RUN 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
WORKDIR /var/www/html
RUN wp core download --allow-root
### copy plugins and themes and php config files to container
COPY ["./plugins/*.zip", "/docker/plugins/"]
COPY ["./themes/*.zip", "/docker/themes/"]
COPY ./themes/dt-the7-child /var/www/html/wp-content/themes/dt-the7-child
COPY ./plugins/teamcity /var/www/html/wp-content/plugins/teamcity
COPY ./docker/php.ini /usr/local/etc/php/php.ini
COPY ./docker/wp-config.php /var/www/html/wp-config.php
COPY ["./plugins/plugins_*.txt", "/docker/plugins/"]
COPY ["./uploads/", "/docker/uploads/"]
COPY ["./docker/commands/*.sh", "/docker/bin/"]
RUN chmod a+x /docker/bin/*.sh
RUN /bin/bash -c "source /docker/bin/setup-theme-plugins.sh"
RUN chown -R www-data:www-data /var/www/html/
CMD apachectl -D FOREGROUND
and here is a bash file (setup-theme-plugins.sh), which is responsible for installing wordpress core and plugins.
#!/bin/bash
echo '-------------------whoami--------------------'
echo $(whoami)
echo '---------------------------------------------'
printf "