I'm configuring CakePHP application with Docker.
I'm new to Docker and this is my first docker-compose.
Dockerfile
FROM php:7.2-apache
LABEL maintainer="Anuj Sharma <contact#anujsh.in>"
# Enable Apache Rewrite + Expires Module
RUN a2enmod rewrite expires
# Install dependencies
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libyaml-dev \
zlib1g-dev \
libicu-dev \
libpq-dev \
libmcrypt-dev \
mysql-client \
g++ \
git \
libzip-dev \
zip \
unzip \
&& rm -r /var/lib/apt/lists/* \
&& pecl install mcrypt-1.0.1 \
&& docker-php-ext-install opcache \
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-configure zip --with-libzip \
&& docker-php-ext-install zip \
&& docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install mbstring pcntl pdo_mysql pdo_pgsql pgsql \
&& docker-php-ext-enable mcrypt \
&& docker-php-ext-enable intl
# Install composer
#RUN curl -sS https://getcomposer.org/installer | php -- --install -dir=/usr/bin/ --filename=composer
RUN curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer
# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN { \
echo 'opcache.memory_consumption=128'; \
echo 'opcache.interned_strings_buffer=8'; \
echo 'opcache.max_accelerated_files=4000'; \
echo 'opcache.revalidate_freq=2'; \
echo 'opcache.fast_shutdown=1'; \
echo 'opcache.enable_cli=1'; \
echo 'upload_max_filesize=128M'; \
echo 'post_max_size=128M'; \
echo 'extension=intl.so'; \
} > /usr/local/etc/php/conf.d/php-recommended.ini
RUN pecl install apcu \
&& pecl install yaml \
&& docker-php-ext-enable apcu yaml
# Set our application folder as an environment variable
ENV APP_HOME /var/www/html
# Change uid and gid of apache to docker user uid/gid
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data
# Change the web_root to cakephp /var/www/html/webroot folder
RUN sed -i -e "s/html/html\/webroot/g" /etc/apache2/sites-enabled/000-default.conf
# Copy source files and run composer
COPY . $APP_HOME
# Install all PHP dependencies
RUN composer install --no-interaction
# Change ownership of our applications
RUN chown -R www-data:www-data $APP_HOME
NOTE: I have copied commands from different sources to generate above Dockerfile.
docker-compose.yml
version: "3.1"
# Define all services
services:
# Our service is called CakePHP ;-)
cakephp:
# We want to use the image which is build from our Dockerfile
build: .
# Apache is running on port 80 but we want to expose this to port 4000 on our local machine
ports:
- "4000:80"
# We depending on the mysql backend
depends_on:
- mysql
volumes:
- .:/var/www/html/
environment:
- SECURITY_SALT=070q78he40qfw475q0q7v0wt7vfqw7vw87qw8dpowe7rfpwq437
- DATABASE_URL=mysql
- MYSQL_USERNAME=root
- MYSQL_PASSWORD=root
- MYSQL_DATABASE=cakephp
mysql:
# We use the mysql base image, version 5.7
image: mysql:5.7
# We mount a datavolume to make sure we don't loose data
volumes:
- cap_mysql_data:/var/lib/mysql
# Setting some env vars to create the DB
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=cakephp
volumes:
cap_mysql_data:
When I run
docker-compose build
It all goes well. But when I run to migrate the migrations
docker-compose run cakephp bin/cake migrations migrate
It gives error as
Exception: The DSN string 'mysql' could not be parsed. in [/var/www/html/vendor/cakephp/cakephp/src/Core/StaticConfigTrait.php, line 292]
The app.php file of CakePHP have configuration for datasource
'username' => env('MYSQL_USERNAME', 'root'),
'password' => env('MYSQL_PASSWORD', ''),
'database' => env('MYSQL_DATABASE', 'cakephp'),
'url' => env('DATABASE_URL', null),
The DATABASE_URL environment variable is expected to hold a DSN (data source name) string, mysql isn't such a string, this is what a DSN looks like:
mysql://user:pass#localhost:3306/database?encoding=utf8&timezone=UTC&cacheMetadata=true
The parts are basically as follows:
<driver>://<username>:<password>#<host>:<port>/<database>?<options>
If you don't actually want to use a DSN, then do not specify the DATABASE_URL variable.
See also
Cookbook > Database Access & ORM > Database Basics > Configuration
API > \Cake\Datasource\ConnectionManager::parseDsn()
Related
I keep getting this error every time I execute docker-compose up -d command in Windows CLI. Please help me solve this problem. Docker-compose.yml file which I'm using is part of Laminas skeleton application and was prewritten there.
Here is docker-compose.yml code:
laminas:
build: .
dockerfile: Dockerfile
ports:
- "8081:80"
volumes:
- .:/var/www
Here is Dockerfile code:
FROM php:7.3-apache
LABEL maintainer="getlaminas.org" \
org.label-schema.docker.dockerfile="/Dockerfile" \
org.label-schema.name="Laminas MVC Skeleton" \
org.label-schema.url="https://docs.getlaminas.org/mvc/" \
org.label-schema.vcs-url="https://github.com/laminas/laminas-mvc-skeleton"
## Update package information
RUN apt-get update
## Configure Apache
RUN a2enmod rewrite \
&& sed -i 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/000-default.conf \
&& mv /var/www/html /var/www/public
## Install Composer
RUN curl -sS https://getcomposer.org/installer \
| php -- --install-dir=/usr/local/bin --filename=composer
###
## PHP Extensisons
###
## Install zip libraries and extension
RUN apt-get install --yes git zlib1g-dev libzip-dev \
&& docker-php-ext-install zip
## Install intl library and extension
RUN apt-get install --yes libicu-dev \
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl
WORKDIR /var/www
I'm creating my Typo3 develop instance with docker containers. And in order to understand what I'm doing so I can understand completely my workflow I'm trying to keep it simple and go step by step to a more advanced instance.
This is my docker-compose file.
This mounted volumes are fine, they are being mounter, their permissions are for root.
What I do is to go inside of the container and set users and groups as www-data in order to access these files from the browser and then on the host set my user as the owner so I can edit them from the host.
But is this right? is this the correct approach? Because I have seen some scripts where all you need to do is to set www-data inside of the container, so I wonder why the permissions on my case are root.
version: "3"
services:
typo3:
build: .
ports:
- "80:80"
volumes:
- ./fileadmin:/var/www/html/fileadmin
- ./typo3conf:/var/www/html/typo3conf
- ./uploads:/var/www/html/uploads
networks:
- backend
database:
image: mysql:8.0
command:
- --character-set-server=utf8
- --collation-server=utf8_unicode_ci
environment:
- "MYSQL_USER=${MYSQL_USER}"
- "MYSQL_PASSWORD=${MYSQL_PASSWORD}"
- "MYSQL_DATABASE=${MYSQL_DATABASE}"
- "MYSQL_RANDOM_ROOT_PASSWORD=${MYSQL_ROOT}"
networks:
- backend
volumes:
database:
fileadmin:
typo3conf:
uploads:
networks:
backend:
Dockerfile:
# Docker image for TYPO3 CMS
# Copyright (C) 2016-2020 Martin Helmich <martin#helmich.me>
# and contributors <https://github.com/martin-helmich/docker-typo3/graphs/contributors>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
FROM php:7.4-apache-buster
LABEL maintainer="Martin Helmich <typo3#martin-helmich.de>"
# Install TYPO3
RUN apt-get update && \
apt-get install -y --no-install-recommends \
wget \
# Configure PHP
libxml2-dev libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
libpq-dev \
libzip-dev \
zlib1g-dev \
# Install required 3rd party tools
graphicsmagick && \
# Configure extensions
docker-php-ext-configure gd --with-libdir=/usr/include/ --with-jpeg --with-freetype && \
docker-php-ext-install -j$(nproc) mysqli soap gd zip opcache intl pgsql pdo_pgsql && \
echo 'always_populate_raw_post_data = -1\nmax_execution_time = 240\nmax_input_vars = 1500\nupload_max_filesize = 32M\npost_max_size = 32M' > /usr/local/etc/php/conf.d/typo3.ini && \
# Configure Apache as needed
a2enmod rewrite && \
apt-get clean && \
apt-get -y purge \
libxml2-dev libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
libzip-dev \
zlib1g-dev && \
rm -rf /var/lib/apt/lists/* /usr/src/*
RUN cd /var/www/html && \
wget -O download.tar.gz https://get.typo3.org/10.4.19 && \
echo "8eb40d02954ffe431c8a41420e4d73d9e0a702714960ecca89162d87ca0e4118 download.tar.gz" > download.tar.gz.sum && \
sha256sum -c download.tar.gz.sum && \
tar -xzf download.tar.gz && \
rm download.* && \
ln -s typo3_src-* typo3_src && \
ln -s typo3_src/index.php && \
ln -s typo3_src/typo3 && \
cp typo3/sysext/install/Resources/Private/FolderStructureTemplateFiles/root-htaccess .htaccess && \
mkdir typo3temp && \
mkdir typo3conf && \
mkdir fileadmin && \
mkdir uploads && \
touch FIRST_INSTALL && \
chown -R www-data. .
# Configure volumes
VOLUME /var/www/html/fileadmin
VOLUME /var/www/html/typo3conf
VOLUME /var/www/html/typo3temp
VOLUME /var/www/html/uploads
There are many ways to do that, for me, I usually use next 2 options:
Enter into the container with docker exec -it $your_container_name /bin/bash, then install vim or nano in container.
Modern IDE/editor provides direct access to container, so you could develop/edit files in container just like you are doing it in local.
E.g. VSCode afford "remote containers plugin", after enable it, you could just in your host pc to development the files in container.
Details refers to Developing inside a Container
I have a docker Image with the following Entrypoint.
ENTRYPOINT [ "sh", "-c", "/var/www/html/app/Console/cake schema update -y && /var/www/html/app/Console/cake migration && apache2-foreground"]
Now I want to use portainer.io to manage docker swarm. I created a new service to where I want to set the entrypoint (instead of in the Image). So I deleted the entrypoint In the dockerfile, and added it into "Image" > "Entrypoint". I added:
sh -c /var/www/html/app/Console/cake schema update -y && /var/www/html/app/Console/cake migration && apache2-foreground
However, the containers of the service are failing to start. Without the entrypoint, the containers are starting. Here is an example of the Dockerfile, where the Entrypoint is set.
#start with base Image from php
FROM php:7.3-apache
#install system dependencies and enable PHP modules
RUN apt-get update && apt-get install -y \
libicu-dev \
libpq-dev \
libmcrypt-dev \
mysql-client \
git \
zip \
unzip \
libzip-dev \
&& docker-php-ext-configure zip --with-libzip \
&& docker-php-ext-install zip \
&& rm -r /var/lib/apt/lists/* \
&& docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd \
&& docker-php-ext-install \
intl \
mbstring \
pcntl \
pdo_mysql \
pdo_pgsql \
pgsql \
opcache \
gettext
# zip \
# mcrypt \
#configure imap for mails
RUN apt-get update && \
apt-get install -y \
libc-client-dev libkrb5-dev && \
rm -r /var/lib/apt/lists/*
RUN docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \
docker-php-ext-install -j$(nproc) imap
#install mcrypt
RUN apt-get update \
&& apt-get install -y libmcrypt-dev \
&& rm -rf /var/lib/apt/lists/* \
&& pecl install mcrypt-1.0.2 \
&& docker-php-ext-enable mcrypt
#install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
#change uid and gid of apache to docker user uid/gid
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data
# enable apache module rewrite
RUN a2enmod rewrite
#change ownership of our applications
RUN chown -R www-data:www-data /var/www/html
#Copy file to start schema update on startup
ENTRYPOINT [ "sh", "-c", "/var/www/html/app/Console/cake schema update -y && /var/www/html/app/Console/cake migration && apache2-foreground"]
EXPOSE 80
How do I have to set the Entrypoint? When I set it a portainer, it is later shown under the section CDM.
I use docker-compose to build my containers and on a state I checkout private repository and I build the project inside container with composer.
I have the following structure
- docker-project
-- docker //here I keep the containers
--- apache
---- Dockerfile // the content what I posted
-- docker-compose.yml
-- src //this is the mounted folder where I can work on project
After build I would expect that the build project would show up in the mounted folder but instead I get empty folders here is my compose yml
apache:
container_name: apache2
build:
context: ./docker/apache
ports:
- 80:80
- 443:443
links:
- database
restart: always
volumes:
- ./src:/var/www/
- ./docker/logs/apache2/:/var/log/apache2/
- ~/.ssh/id_rsa:/root/.ssh/id_rsa
- ./docker/apache/sites/:/etc/apache2/sites-enabled
and here I have the docker file
FROM php:7.1-apache
ENV APACHE_DOCUMENT_ROOT /var/www
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl \
openssl \
openssh-server \
sendmail \
sendmail-bin \
mailutils \
libicu-dev \
libmemcached-dev \
libz-dev \
libpq-dev \
libjpeg-dev \
libfreetype6-dev \
libssl-dev \
libmcrypt-dev \
libxml2-dev \
libbz2-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
zlib1g-dev \
libicu-dev \
g++ \
libxml2-dev \
git \
&& rm -rf /var/lib/apt/lists/*
# Install various PHP extensions which are required to build project
RUN docker-php-ext-configure bcmath --enable-bcmath \
&& docker-php-ext-configure pcntl --enable-pcntl \
&& docker-php-ext-configure pdo_mysql --with-pdo-mysql \
&& docker-php-ext-configure pdo_pgsql --with-pgsql \
&& docker-php-ext-configure mbstring --enable-mbstring \
&& docker-php-ext-configure soap --enable-soap \
&& docker-php-ext-install \
bcmath \
json \
intl \
mbstring \
mcrypt \
mysqli \
pcntl \
pdo_mysql \
pdo_pgsql \
soap \
sockets \
zip \
&& docker-php-ext-configure gd \
--enable-gd-native-ttf \
--with-jpeg-dir=/usr/lib \
--with-freetype-dir=/usr/include/freetype2 && \
docker-php-ext-install gd
##install xdebug and enable it. We need this for php unit coverage
RUN yes | pecl install xdebug \
&& docker-php-ext-enable xdebug
RUN yes | pecl install mongodb \
&& docker-php-ext-enable mongodb
RUN update-ca-certificates
# install composer to can install dependencies
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php composer-setup.php --install-dir=/usr/local/bin --filename=composer \
&& php -r "unlink('composer-setup.php');"
RUN mkdir /root/.ssh
RUN rm -rf /var/www/project
WORKDIR /var/www/
RUN git checkout path/to/repo
RUN ls -l /var/www
WORKDIR /repo
RUN composer install
RUN ls -l
on docker-compose build I see that the project will be builded but on
docker-compose upthe folder is empty
What I'm missing in this case?
You are mounting a volume on /var/www when starting the container.
/var/www will be the same as ./src/www on the host machine, replacing anything that was added to /var/www during the build of the image.
Consider using the composer docker image or make sure to install the dependencies to another directory. Check out the vendor_dir key in composer.json
Example:
composer.json
{
"require": {
},
"config": {
"vendor-dir": "/var/www/vendor/"
}
}
Docker image build:
...
RUN mkdir -p /var/www/vendor/
COPY composer.json /var/www/vendor/
RUN cd /var/www/vendor/ && composer install
RUN mkidr -p /var/www/app
...
docker-compose.yml
volumes:
- ./src:/var/www/app
I could give a way of currently we are working. Rather cloning the repo like this what we do, we clone the repo in the local machine.
You have standard docker image that you have created from
php:7.1-apache with required softwares that you need to run the project.
Then you clone your project to you host machine.(think into src folder)
You run the composer install locally.
Then as you have defined the docker-compose.yml mount your src folder into /var/www/
I'm trying to install drupal console in docker (under Linux Antergos). I've the following error :
Warning: mkdir(): Permission denied in phar:///usr/local/bin/drupal/vendor/drupal/console-core/src/Utils/ConfigurationManager.php on line 49
Here is my PHP dockerfile :
FROM php:7.0-fpm
RUN usermod -u 1000 www-data
# Set timezone
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Bruxelles /etc/localtime
RUN "date"
RUN apt-get update && apt-get install -y \
git \
unzip \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
mysql-client \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install mysqli pdo pdo_mysql mcrypt zip mbstring opcache json
# Install Xdebug / Redis
RUN pecl install redis \
&& pecl install xdebug \
&& docker-php-ext-enable redis xdebug
# Set the Drush version.
ENV DRUSH_VERSION 8.1.2
# Install Drush 8 with the phar file.
RUN curl -fsSL -o /usr/local/bin/drush "https://github.com/drush-ops/drush/releases/download/$DRUSH_VERSION/drush.phar" && \
chmod +x /usr/local/bin/drush
# Download console.
RUN curl https://drupalconsole.com/installer -L -o drupal.phar
# Install console.
RUN mv drupal.phar /usr/local/bin/drupal && \
chmod +x /usr/local/bin/drupal && \
drupal init --override
# Create drush-backups dir
RUN mkdir /var/www/drush-backups/
USER 1000
WORKDIR /var/www/html
To solve the problem I had to install Drupal Console by composer. There is certainly another solution, but it's work with it.