Why are environment variables not being set inside of my docker container? - docker

I have the following in my docker-compose.yml:
php:
build:
args:
http_proxy:
https_proxy:
no_proxy:
context: ./vm-images/php
environment:
http_proxy:
https_proxy:
no_proxy:
CURRENT_ENVIRONMENT: DOCKER
container_name: php
ports:
- "9000:9000"
depends_on:
- mysql
links:
- mysql:mysql
logging:
driver: "json-file"
volumes:
- /var/www/:/var/www/
My DOCKERFILE has the following:
FROM ubuntu:latest
RUN locale-gen en_US.UTF-8 && \
export LANG=en_US.UTF-8 && \
export LC_ALL=en_US.UTF-8
RUN apt-get update && \
apt-get install -y --reinstall ca-certificates
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated --no-install-recommends \
software-properties-common python-software-properties
RUN add-apt-repository ppa:ondrej/php
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated --no-install-recommends \
git-core \
libjpeg-progs \
mysql-client \
optipng \
php5.6 \
php5.6-curl \
php5.6-intl \
php5.6-fpm \
php5.6-gd \
php5.6-mcrypt \
php5.6-mysqli \
php5.6-pdo \
php5.6-xdebug \
php5.6-xml \
php5.6-zip
RUN rm -r /var/lib/apt/lists/*
RUN rm -r /var/cache/apt/*
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
php -r "unlink('composer-setup.php');"
RUN mkdir -p /run/php && chown www-data:www-data /run/php
COPY files/etc/php/fpm/php-fpm.conf /etc/php/5.6/fpm/php-fpm.conf
COPY files/etc/php/fpm/pool.d/www.conf /etc/php/5.6/fpm/pool.d/www.conf
COPY files/etc/php/fpm/php.ini /etc/php/5.6/fpm/php.ini
COPY files/etc/php/cli/php.ini /etc/php/5.6/cli/php.ini
COPY files/etc/php/mods-available/xdebug.ini /etc/php/5.6/mods-available/xdebug.ini
RUN phpenmod curl intl gd mcrypt mysqli pdo xdebug xml zip
CMD ["php-fpm5.6", "--nodaemonize", "--fpm-config", "/etc/php/5.6/fpm/php-fpm.conf"]
When I run docker-compose --verbose up -d I notice that there are no env vars passed in for my php container. I've verified that those env vars are set on my host, and they are passed in for all of my other containers without fail. Anyone have any ideas?
EDIT: Strangely enough, I've also noticed that /proc/$PID/environ (where $PID is the PID of the php container [obtained by running docker inspect --format "{{.State.Pid}}" php] on the host machine is empty.

Related

Can't enable Docker Xdebug

I am trying to use Xdebug on Docker, I'm using Debian, PHP 8. Cannot make a connection with Xdebug. Won't give any errors.
Since I don't get any errors or logs can't figure out what is wrong with my configurations.
Everything builds normally with the configurations below. Could someone help me activate Xdebug?
Dockerfile:
FROM debian:stretch-slim as builder
RUN mkdir -p /usr/share/man/man1
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends wget gnupg2 unzip curl cron make \
locales ca-certificates apt-transport-https default-mysql-client-core sendmail \
default-jre-headless ca-certificates-java
RUN sed -i 's/mozilla\/DST_Root_CA_X3.crt/!mozilla\/DST_Root_CA_X3.crt/g' /etc/ca-certificates.conf
RUN update-ca-certificates
RUN wget -q https://packages.sury.org/php/apt.gpg -O- | apt-key add - && \
echo "deb https://packages.sury.org/php/ stretch main" | tee /etc/apt/sources.list.d/php.list && \
cd /etc/apt/sources.list.d && \
rm -rf ondrej-ubuntu*
RUN apt-get update && apt-get install -y \
git \
zip \
unzip \
sudo \
libzip-dev \
libicu-dev \
libbz2-dev \
libpng-dev \
libjpeg-dev \
libmcrypt-dev \
libreadline-dev \
libfreetype6-dev \
g++ \
nano \
vim \
php-xdebug
RUN curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
RUN apt-get update && apt-get install -y nodejs
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install -y --no-install-recommends \
php8.0 \
php8.0-mysql \
php8.0-gd \
php8.0-xml \
php8.0-mbstring \
php8.0-curl \
libapache2-mod-php8.0 \
php8.0-intl \
php8.0-soap \
php8.0-zip \
php8.0-ldap \
php8.0-dev \
php-pear \
mysql-client \
apache2 \
php-xdebug && \
update-alternatives --set php /usr/bin/php8.0 && \
a2enmod php8.0 && \
a2enmod rewrite && \
a2enmod ssl
RUN echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen && \
echo "tr_TR.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
RUN apt-get -y autoremove wget unzip && apt-get -y clean && \
rm -rf "/tmp/"* "/var/cache/apt" "/usr/share/man" "/usr/share/doc" "/usr/share/doc-base" "/usr/share/info/*" >> /dev/null
RUN chown -R www-data. /var/www/html
RUN apt-get -y autoremove && apt-get -y clean && \
rm -rf "/tmp/"* "/var/cache/apt" "/usr/share/man" "/usr/share/doc" "/usr/share/doc-base" "/usr/share/info/*" >> /dev/null
RUN curl -Ss https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
RUN a2enmod rewrite
CMD service apache2 restart
ENTRYPOINT ["/usr/sbin/apache2ctl"]
CMD ["-D", "FOREGROUND"]
EXPOSE 80
docker-compose.yml
version: "3.3"
networks:
my_network:
volumes:
my_volume:
services:
mysqllatest:
image: mysql:latest
container_name: mysqllatest
volumes:
- my_volume:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
tty: true
environment:
MYSQL_DATABASE: local_db
MYSQL_USERNAME: root
MYSQL_PASSWORD: toor
MYSQL_ROOT_PASSWORD: toor
MYSQL_SERVICE_NAME: mysql
ports:
- "3306:3306"
networks:
- my_network
php8debian:
container_name: php8debian
build: ./base/
volumes:
- ../:/var/www/html/
- ./server/php/vhost/000-default.conf:/etc/apache2/sites-enabled/000-default.conf
- ./server/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
ports:
- 80:80
extra_hosts:
- "mysite.test:127.0.0.1"
stdin_open: true
tty: true
networks:
- my_network
xdebug.ini
zend_extension=xdebug.so
[XDebug]
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_port = 9003
xdebug.client_host = 'host.docker.internal'
xdebug.idekey = VSCODE
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"hostname": "0.0.0.0",
"pathMappings": {
"/var/www/html": "${workspaceRoot}/my_site"
},
"log": true
}
]
}

Enable/Disable xDebug through Docker-Compose, is it possible?

I have the following Dockerfile:
FROM php:7.2-apache
RUN apt-get update && apt-get install -y \
zlib1g-dev \
git \
unzip \
&& rm -rf /var/lib/apt/lists/ \
&& docker-php-ext-install zip pdo_mysql bcmath \
&& a2enmod rewrite \
&& pecl install xdebug-2.9.0 redis \
&& docker-php-ext-enable xdebug redis \
&& mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" \
&& mv /var/www/html /var/www \
&& chown -R www-data:www-data /var/www/ \
&& ln -s /usr/local/bin/php /usr/bin/php
COPY --from=composer:1.9 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/drm-case
CMD ["docker/apache/bootstrap.sh"]
And this is the docker-compose.yml file that uses the previous Dockerfile to build the containers (not all the time though):
version: "2.4"
services:
case-v2-apache:
container_name: local-dev
image: local-dev:1.6 # increase this number when changing the Dockerfile
depends_on:
mysql-server:
condition: service_healthy
volumes:
- ${LOCAL_PATH}:/var/www:delegated
- ${LOCAL_PATH}/docker/apache/conf.d:/usr/local/etc/php/conf.d
- ${LOCAL_PATH}/docker/apache/conf-enabled/servername.conf:/etc/apache2/conf-enabled/servername.conf
- ${LOCAL_PATH}/docker/apache/sites-available/000-default.conf:/etc/apache2/sites-available/000-default.conf
ports:
- "8009:80"
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
build:
context: ${LOCAL_PATH:-./local-dev}
dockerfile: docker/apache/Dockerfile
networks:
- main
environment:
- VIRTUAL_HOST=local-dev.localhost
- COMPOSER_MEMORY_LIMIT=-1
- COMPOSER_AUTH=${COMPOSER_AUTH}
- COMPOSER_ALLOW_SUPERUSER=1
- PHP_IDE_CONFIG=serverName=local-dev
One of the files being copied in this line: ${LOCAL_PATH}/docker/apache/conf.d:/usr/local/etc/php/conf.d is xdebug related meaning is the one enabling and setting up the extension.
Wonder if there is a way to tell Docker by using ARG or ENV variables to enable/disable xDebug while starting the container? Has anyone tried such thing before? If so can you help me with some ideas?
You could simply move/rename the xdebug conf file based on a build arg…
FROM php:7.2-apache
RUN apt-get update && apt-get install -y \
zlib1g-dev \
git \
unzip \
&& rm -rf /var/lib/apt/lists/ \
&& docker-php-ext-install zip pdo_mysql bcmath \
&& a2enmod rewrite \
&& pecl install xdebug-2.9.0 redis \
&& docker-php-ext-enable xdebug redis \
&& mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" \
&& mv /var/www/html /var/www \
&& chown -R www-data:www-data /var/www/ \
&& ln -s /usr/local/bin/php /usr/bin/php
COPY --from=composer:1.9 /usr/bin/composer /usr/bin/composer
RUN if [[ "$DISABLE_XDEBUG" == "1" ]] ; then mv "$PHP_INI_DIR/conf.d/xdebug.ini" "$PHP_INI_DIR/conf.d/xdebug.ini.disabled"
WORKDIR /var/www/drm-case
CMD ["docker/apache/bootstrap.sh"]
… or something like that?

composer install with prestissimo in docker container

I can try composer install in docker container on ubuntu16.04 image with hirak/prestissimo plugin.
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y software-properties-common \
&& LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php \
&& apt-get update \
&& apt-get install -y php7.1 git vim supervisor curl unzip mysql-client wget \
&& apt-get install -y --no-install-recommends php7.1-cli php7.1-dev \
php7.1-pgsql php7.1-sqlite3 php7.1-gd \
php7.1-curl php7.1-memcached \
php7.1-imap php7.1-mysql php7.1-mbstring \
php7.1-xml php7.1-imagick php7.1-zip php7.1-bcmath php7.1-soap \
php7.1-intl php7.1-readline php7.1-mcrypt php7.1-fpm
RUN curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer
RUN composer --no-interaction global require 'hirak/prestissimo'
COPY docker/config/ /config/
RUN cp /config/supervisord.conf /etc/supervisor/supervisord.conf
WORKDIR /app
EXPOSE 9000
ENTRYPOINT ["/config/entrypoint.sh"]
And my docker-compose.yml file
version: '3'
services:
php71:
restart: unless-stopped
image: mylogin/myimage
volumes:
- ./:/app
networks:
- my-network
But when i run composer-install in my docker container parallel installation with hirak/prestissimo plugin doesn't work.

Docker multi-stage build not copying between stages

I'm trying to create a multi-stage build where the first stage does a yarn install for the theme and the second stage sets up the PHP environment for Drupal.
When I look at the output it looks like yarn install is being run but the COPY command near the bottom doesn't copy it across to the PHP image. If I'm right when this works the node_modules directory should be created on my local machine?
docker-compose.yml:
version: '3.7'
services:
web:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/var/www/html:cached
env_file:
- ./local-development.env
ports:
- "8888:80"
db:
image: mysql:5.7
env_file:
- ./local-development.env
ports:
- "3306:3306"
Dockerfile:
FROM node:latest as yarn-install
WORKDIR /app
COPY ./web/themes/material_admin_mine ./
RUN yarn install --verbose --force;
# from https://www.drupal.org/docs/8/system-requirements/drupal-8-php-requirements
FROM php:7.2-apache
# Install & setup Xdebug
RUN yes | pecl install xdebug \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_connect_back=0' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_host=docker.for.mac.localhost' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_port=9000' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_handler=dbgp' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_mode=req' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_autostart=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.idekey=PHPSTORM' >> /usr/local/etc/php/conf.d/xdebug.ini
# Install git & mysql-client for running Drush
RUN apt update; \
apt install -y \
git \
mysql-client
# install the PHP extensions we need
RUN set -ex; \
\
if command -v a2enmod; then \
a2enmod rewrite; \
fi; \
\
savedAptMark="$(apt-mark showmanual)"; \
\
apt-get update; \
apt-get install -y --no-install-recommends \
libjpeg-dev \
libpng-dev \
libpq-dev \
unzip \
git \
; \
\
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer; \
\
docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr; \
docker-php-ext-install -j "$(nproc)" \
gd \
opcache \
pdo_mysql \
pdo_pgsql \
zip \
; \
\
# reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies
apt-mark auto '.*' > /dev/null; \
apt-mark manual $savedAptMark; \
ldd "$(php -r 'echo ini_get("extension_dir");')"/*.so \
| awk '/=>/ { print $3 }' \
| sort -u \
| xargs -r dpkg-query -S \
| cut -d: -f1 \
| sort -u \
| xargs -rt apt-mark manual; \
\
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
rm -rf /var/lib/apt/lists/*
# 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=60'; \
echo 'opcache.fast_shutdown=1'; \
echo 'opcache.enable_cli=1'; \
} > /usr/local/etc/php/conf.d/opcache-recommended.ini
# Various packages required to run Gulp in the theme directory
# gnupg is require for nodejs
RUN apt update; \
apt install gnupg -y; \
apt install gnupg1 -y; \
apt install gnupg2 -y; \
cd ~; \
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh; \
bash nodesource_setup.sh; \
apt install nodejs -y; \
npm install gulp-cli -g -y; \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - ;\
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list; \
apt update && apt install yarn -y;
WORKDIR /var/www/html
COPY --from=0 /app ./web/themes/material_admin_mine
When your Dockerfile ends with:
WORKDIR /var/www/html
COPY --from=0 /app ./web/themes/material_admin_mine
That should in fact copy the data from the first build stage to the final image. But then when you launch the container with
volumes:
- ./:/var/www/html:cached
everything in the /var/www/html directory tree, including that final COPY step, is hidden and replaced with what's in the current directory on the host. If you think of this like a copy, it's a one-way copy into the container; later changes will get copied back out to the host, but there's nothing that synchronizes what's in the image with what you previously had in the directory at startup time.
A Dockerfile intrinsically can't affect host filesystem content. In your case it sounds like the host content is secondary to your application proper. Given what's going into the first stage, I'd just run the yarn install step on the host and be done with it (you probably already have Node and Yarn available even). Otherwise you'd need a more selective volumes: section that carefully tried to avoid overwriting that one directory; you might be able to mount something like ./web/src:/var/www/html/web/src to only include your application code and avoid hiding the .../web/themes tree.

what should I write on dockerfile for nginx and php:7.0-fpm?

I want to work on nginx server and I use php:7.0-fpm
what should I write on dockerfile especially where begining from?
This is my docker-compose.yml
version: '3'
services:
php:
build:
context: .
dockerfile: Dockerfile_php
image: php-fpm71:phalcon
networks:
- app
ports:
- "9000:9000"
volumes:
- ./www:/var/www/html
- ./conf/php/php.ini:/usr/local/etc/php/php.ini
- ./shared:/shared
nginx:
build:
context: .
dockerfile: Dockerfile_nginx
networks:
- app
depends_on:
- php
ports:
- "80:80"
volumes:
- ./www:/var/www/html
- ./conf/nginx/conf.d:/etc/nginx/conf.d
- ./logs/nginx:/var/log/nginx
- ./shared:/shared
mysql:
networks:
- app
depends_on:
- php
image: mysql:latest
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./data/mysql:/var/lib/mysql
- ./shared:/shared
networks:
app:
Dockerfile_nginx
FROM nginx:latest
MAINTAINER Yakup Arslan <arslan.yakup#hotmail.com>
RUN apt-get update -y
RUN apt-get install -y \
vim
RUN apt-get autoremove -y && \
apt-get autoclean -y && \
apt-get clean -y && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN rm -rf /var/log /var/cache
Dockerfile_php
#
# PHP-7.1 Phalcon Support
#
#
# Pull base image
FROM php:7.1-fpm
MAINTAINER Yakup Arslan <arslan.yakup#hotmail.com>
RUN curl -O https://codeload.github.com/phalcon/cphalcon/tar.gz/v3.1.2
RUN tar xvzf v3.1.2
RUN cd cphalcon-3.1.2/build && ./install
RUN cd ../../ && rm -Rf cphalcon-3.1.2 && rm -Rf v3.1.2
RUN echo extension=phalcon.so > /usr/local/etc/php/conf.d/phalcon.ini
RUN apt-get update -y
RUN apt-get install -y \
git \
zip \
unzip \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev
RUN git clone https://github.com/phalcon/phalcon-devtools.git /usr/local/lib/phalcon-devtools && \
ln -svf /usr/local/lib/phalcon-devtools/phalcon.php /usr/local/bin/phalcon && \
chmod ugo+x /usr/local/bin/phalcon
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
php composer-setup.php && \
php -r "unlink('composer-setup.php');" && \
mv composer.phar /usr/local/bin/composer
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) mysqli \
pdo_mysql \
bcmath \
gd
RUN apt-get autoremove -y && \
apt-get autoclean -y && \
apt-get clean -y && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /etc/php5 /etc/php/5* /usr/lib/php/20121212 /usr/lib/php/20131226
RUN rm -rf /var/log /var/cache

Resources