PHP is unable to see env var when runing in docker - docker

I have this docker-compose config.
The "app" is a PHP application. As you can see, 3 env vars are passed to the container.
However, after docker-compose up, PHP doesn't see these. They are not returned by getenv() and they cannot be found $_ENV either.
What's wrong here?
version: '3.4'
services:
db:
image: postgres:11.0
restart: always
environment:
POSTGRES_PASSWORD: testuser
POSTGRES_USER: test
POSTGRES_DB: db
volumes:
- /data/db
redis:
image: redis:latest
restart: always
volumes:
- /data/redis
app:
build:
context: .
dockerfile: Dockerfile
environment:
- DATABASE_URL=postgresql://db:5432/db
- REDIS_URL=tcp://redis:6379?database=1
- NODE_ENV=development
ports:
- '80:80'
volumes:
- '${BASEDIR}:/var/www/some'
Here is my Dockerfile:
FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
software-properties-common \
apt-utils \
tzdata \
locales
RUN add-apt-repository ppa:ondrej/php
RUN apt-get update
RUN locale-gen en_US.UTF-8
RUN echo "Europe/Budapest" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata
RUN apt-get -y update && apt-get -y install \
libglu1-mesa \
less \
vim \
nginx \
php7.4 \
php7.4-fpm \
php7.4-cli \
php7.4-common \
php7.4-curl \
php-deepcopy \
php7.4-gd \
php7.4-mbstring \
php7.4-pgsql \
php7.4-soap \
php7.4-xdebug \
php7.4-zip \
php7.4-xml \
phpunit \
npm && npm i -g npm
ENV NGINX_RUN_USER www-data
ENV NGINX_RUN_GROUP www-data
ENV NGINX_LOG_DIR /var/log/nginx
ENV NGINX_LOCK_DIR /var/lock/nginx
ENV NGINX_PID_FILE /run/nginx.pid
RUN mkdir www
RUN mkdir -p /var/www/dbv.local/html
RUN chmod -R 755 /var/www/dbv.local
COPY ./php.ini /etc/php/7.4/fpm/php.ini
COPY ./dbv.local /etc/nginx/sites-available/dbv.local
COPY ./lib/aspose_php.so /usr/lib/php/20190902
COPY ./lib/libaspose_cpp_clang3_libstdcpp.so /usr/lib/libaspose_cpp_clang3_libstdcpp.so
COPY ./lib/libAspose.Slides_clang3_libstdcpp.so /usr/lib/libAspose.Slides_clang3_libstdcpp.so
COPY ./lib/libphpcpp.so.2.2 /usr/lib/libphpcpp.so.2.2
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
RUN ln -s /etc/nginx/sites-available/dbv.local /etc/nginx/sites-enabled/
ADD ./xdebug.ini /etc/php/7.4/mods-available/xdebug.ini
ADD ./aspose_php.ini /etc/php/7.4/mods-available/aspose_php.ini
ADD ./start.sh /root/start.sh
RUN ln -s /etc/php/7.4/mods-available/xdebug.ini /etc/php/7.4/mods-available/20-xdebug.ini
RUN ln -s /etc/php/7.4/mods-available/aspose_php.ini /etc/php/7.4/fpm/conf.d/aspose_php.ini
RUN ln -s /etc/php/7.4/mods-available/aspose_php.ini /etc/php/7.4/cli/conf.d/aspose_php.ini
RUN rm -rf /var/lib/apt/lists/*
RUN apt-get clean
CMD ["/root/start.sh"]
EXPOSE 80 9000 5432
Edit:
start.sh is just a one liner
service php7.4-fpm start && nginx
Ubuntu 18 is necessary. I could use an official Nginx image though. Maybe that's the issue?

Related

running existing yii1 application in docker

I have a existing yii1 application. And I am using local xampp.
So I try to dockerise the existing yii1 application.
So This is my dockerfile in root folder:
FROM php:8.1-apache as dev
ENV DEBIAN_FRONTEND=noninteractive
ENV APP_ENV=development
WORKDIR /var/www/html
RUN apt-get update \
&& apt-get -y install --no-install-recommends apt-utils zip unzip nano ncdu 2>&1 \
&& apt-get -y install --no-install-recommends python graphviz 2>&1 \
&& apt-get -y install git iproute2 procps lsb-release \
&& apt-get install -y -qq software-properties-common \
&& apt-get install -y -qq wget git lynx ack-grep \
&& yes | pecl install xdebug \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& apt-get -y install libicu-dev \
&& docker-php-ext-install intl pdo_mysql opcache \
&& pecl install apcu && docker-php-ext-enable apcu \
&& echo "apc.enable_cli=1" > /usr/local/etc/php/php.ini \
&& echo "apc.enable=1" > /usr/local/etc/php/php.ini \
&& echo "post_max_size = 100M" > /usr/local/etc/php/php.ini \
&& a2enmod rewrite \
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install gnupg2 -y
RUN rm -rf /etc/apache2/sites-enabled \
&& ln -s /var/www/html/.devcontainer/sites-enabled /etc/apache2/sites-enabled
RUN echo 'alias ll="ls -la --color=auto"' >> ~/.bashrc && \
echo "alias ack='ack-grep'" >> ~/.bashrc
RUN chown www-data:www-data -R ./
ENV DEBIAN_FRONTEND=dialog
and this is my docker-compose.yml file also in root folder:
version: '3'
services:
web:
image: nguyenmanhluu/yii1:1.0
container_name: dockeryiidisc
ports:
- "9002:80"
build: .
volumes:
- ../:/var/www/html
command: /bin/sh -c "service apache2 start && while sleep 1000; do :; done"
db:
container_name: dockeryiimysql
image: mysql:latest
volumes:
- dockeryiimysql:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
MYSQL_DATABASE: sdi
phpmyadmin:
container_name: dockeryiipma
image: phpmyadmin:latest
environment:
UPLOAD_LIMIT: 300M
PMA_ARBITRARY: 1
APACHE_HTTP_PORT_NUMBER: 8080
ports:
- 8080:8080
command: /bin/bash -c "sed -i \"s/80/$$APACHE_HTTP_PORT_NUMBER/g\" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf && /docker-entrypoint.sh apache2-foreground"
volumes:
dockeryiimysql: {}
So if I go to my root folder and do a docker-compose up. Then I see in docker desktop all three containers are running.
And if I go to: localhost:8080 I see the phpmyadmin database.
But if I go to localhost:9002 I see the startscreen of xampp. And I dont be redirected to the real application.
So what I have to change?
Thank you.
What I mean I will be redirected to: http://localhost:9002/dashboard/
I solved, like this:
version: '3'
services:
web:
image: nguyenmanhluu/yii1:1.0
container_name: dockeryiidisc
ports:
- "8082:80"
build:
context: ..
dockerfile: Dockerfile
target: dev
volumes:
- ./:/var/www/html
command: /bin/sh -c "service apache2 start && while sleep 1000; do :; done"
db:
container_name: dockeryiimysql
image: mysql:latest
volumes:
- dockeryiimysql:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
MYSQL_DATABASE: sdi
phpmyadmin:
container_name: dockeryiipma
image: phpmyadmin:latest
environment:
UPLOAD_LIMIT: 300M
PMA_ARBITRARY: 1
APACHE_HTTP_PORT_NUMBER: 8080
ports:
- 8080:8080
command: /bin/bash -c "sed -i \"s/80/$$APACHE_HTTP_PORT_NUMBER/g\" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf && /docker-entrypoint.sh apache2-foreground"
volumes:
dockeryiimysql: {}

ERROR: Service 'ubuntu' failed to build: The command '/bin/sh -c apt-get update

when I execute the command docker-compose up --build -d
I get these errors:
E: Unable to locate package php7.4 E: Couldn't find any package by
glob 'php7.4' E: Couldn't find any package by regex 'php7.4' E: Unable
to locate package php7.4-fpm E: Couldn't find any package by glob
'php7.4-fpm' E: Couldn't find any package by regex 'php7.4-fpm' ERROR:
Service 'ubuntu' failed to build: The command '/bin/sh -c apt-get
update && apt-get install -y curl zip unzip php7.4
php7.4-fpm gettext-base sudo' returned a non-zero code:
100
can you help me please what is wrong with my dockerfile and docker-compose.yml?
here is my dockerfile
FROM ubuntu:18.04
# ENV PORT=80
# ENV FPMSTREAM=9000
RUN apt-get update \
&& apt-get install -y curl zip unzip \
php7.4 php7.4-fpm \
gettext-base sudo
COPY ./webapp /var/www/webapp
WORKDIR /var/www/webapp
ADD default.conf /etc/nginx/conf.d/default.conf
RUN chown -R www-data:www-data /var/www && \
chmod -R 775 /var/www && \
useradd -m docker && echo "docker:docker" | chpasswd && \
adduser docker sudo
USER docker
docker-compose.yml
version: "3.8"
services:
ubuntu:
build: .
container_name: ubuntu-container
external_links:
- nginx
- db
nginx:
image: nginx:stable
container_name: nginx-container
ports:
- "80:80"
expose:
- 9000
volumes:
- ./code:/var/www/webapp
- ./default.conf:/etc/nginx/conf.d/default.conf
db:
image: mysql:8.0
container_name: mysql-container
command: --default-authentication-plugin=mysql_native_password
restart: always
env_file:
- .env
volumes:
- ./mysql-data:/var/lib/mysql
expose:
- 3306
ports:
- "3306:3306"
your problem is that you're trying to install a version of PHP which is not available in Ubuntu 18.04.
The latest available version is 7.2, so you need to replace php7.4 php7.4-fpm with either php php-fpm or php7.2 php7.2-fpm
Please try the below Dockerfile for build
FROM ubuntu:18.04
# ENV PORT=80
# ENV FPMSTREAM=9000
#Add Repos for PHP modules
RUN apt-get update \
&& apt -y install software-properties-common \
&& add-apt-repository ppa:ondrej/php
RUN apt-get update \
&& apt-get install -y curl zip unzip \
php7.4 php7.4-fpm \
gettext-base sudo
COPY ./webapp /var/www/webapp
WORKDIR /var/www/webapp
ADD default.conf /etc/nginx/conf.d/default.conf
RUN chown -R www-data:www-data /var/www && \
chmod -R 775 /var/www && \
useradd -m docker && echo "docker:docker" | chpasswd && \
adduser docker sudo
USER docker

Docker - No such file or directory. Copy does not copy all files

Dockerfile does not completely copy all files from the local directory.
I don’t understand why he copies the folder from the backend, but it doesn’t have all the files.
Thanks for help..
structure:
docker/
django/
/Dockerfile
/backend/
requirements.txt
src/
angular4/
/Dockerfile
/client
docker-compose.yml
docker-compose.yml
version: '3'
services:
db:
image: postgres:9.6
hostname: db
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: alinta
ports:
- "5432:5432"
backend:
build: ./django
image: alinta
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/var/www/alinta/
ports:
- "8000:8000"
depends_on:
- db
migration:
image: alinta
command: python3 manage.py migrate --noinput
volumes:
- .:/var/www/alinta/
depends_on:
- db
frontend:
build: ./angular4
volumes:
- .:/var/www/alinta
ports:
- "4200:4200"
Dockerfile (django):
FROM postgres:9.6
RUN apt-get update && apt-get install -q -y postgresql-9.6 postgresql-client-9.6 postgresql-contrib-9.6 postgresql-client-common postgresql-common
#RUN echo postgres:postgres | chpasswd
#RUN pg_createcluster 9.6 main --start
#RUN /etc/init.d/postgresql start
FROM python:3.7
MAINTAINER Nikita Alekseev <nik_alekseev#outlook.com>
# Alinta
# Version: 1.0
# Install Python and Package Libraries
RUN apt-get update && apt-get upgrade -y && apt-get autoremove && apt-get autoclean
RUN apt-get install -y \
libffi-dev \
libssl-dev \
libxml2-dev \
libxslt-dev \
libjpeg-dev \
libfreetype6-dev \
zlib1g-dev \
net-tools \
vim
RUN apt-get install -y \
python3-pip \
python3-dev \
python3-virtualenv \
libpq-dev \
postgresql \
postgresql-contrib \
nginx \
curl
RUN pip3 install virtualenv
# Project Files and Settings
ARG PROJECT=alinta
ARG PROJECT_DIR=/var/www/${PROJECT}
RUN mkdir -p $PROJECT_DIR/backend/src
RUN mkdir -p $PROJECT_DIR/backend/src/static
RUN mkdir -p $PROJECT_DIR/backend/src/media
#WORKDIR $PROJECT_DIR
COPY ./backend /var/www/alinta/
WORKDIR $PROJECT_DIR/backend
RUN virtualenv -p python3.7 --no-site-packages env
RUN /bin/bash -c "source env/bin/activate"
RUN pip3 install -r requirements.txt
WORKDIR $PROJECT_DIR/backend/src
EXPOSE 8000
docker-compose build
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
For your Dockerfile your requirements.txt is in backend directory, and not on the same level, so adding or copying it will make it visible:
so add:
ADD ./backend/requirements.txt requirements.txt
or
COPY ./backend/requirements.txt requirements.txt
before you run
RUN pip3 install -r requirements.txt
Just copying backend directory as:
COPY ./backend /var/www/alinta/
will not allow Docker to know the context of your requrements.txt. Using ADD or COPY will instruct Docker how to locate file.

Can I use a docker volume with S3FS?

I would like to have a shared directory between my containers: ftp and s3fs. Todo so, I have created a volume in my docker-compose file called s3.
If I stop s3fs from running in my s3fs container, then I can create files in the ftp container and they will show up in side s3fs under /home/files.
However, when running s3fs the directory /home/files remains empty whilst I create files in the ftp.
This is what my /proc/mounts file looks like:
/dev/sda2 /home/files ext4 rw,relatime,data=ordered 0 0
s3fs /home/files fuse.s3fs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
I belive fuse maybe overriding my docker volume, has anyone encountered this problem before?
docker-compose.yml
version: "3"
services:
ftp:
image: app/proftpd:latest
volumes:
- s3:/home/files
ports:
- 2222:2222
s3fs:
image: app/s3fs:latest
command: start
env_file:
- s3fs/aws.env
volumes:
- s3:/home/files
cap_add:
- SYS_ADMIN
devices:
- "/dev/fuse"
environment:
ENVIRONMENT: "dev"
volumes:
s3:
s3fs - Dockerfile
FROM ubuntu:16.04
RUN apt-get update -qq
RUN apt-get install -y \
software-properties-common
RUN apt-get update -qq
RUN apt-get install -y \
automake \
autotools-dev \
fuse \
g++ \
git \
libcurl4-openssl-dev \
libfuse-dev \
libssl-dev \
libxml2-dev \
make \
pkg-config \
curl
RUN curl -L https://github.com/s3fs-fuse/s3fs-fuse/archive/v1.84.tar.gz | tar zxv -C /usr/src
RUN cd /usr/src/s3fs-fuse-1.84 && ./autogen.sh && ./configure --prefix=/usr --with-openssl && make && make install
COPY entrypoint.sh /opt/s3fs/bin/entrypoint.sh
RUN mkdir -p /home/files
WORKDIR /opt/s3fs/bin
ENTRYPOINT ["/bin/sh", "./entrypoint.sh"]
s3fs - entrypoint.sh
#!/usr/bin/env bash
case $1 in
start)
echo "Starting S3Fs: "
s3fs mybucket /home/files -o allow_other,nonempty -d -d
;;
esac
ftp - Dockerfile
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y \
openssh-server \
proftpd-basic \
proftpd-mod-mysql
COPY proftpd.conf /etc/proftpd/proftpd.conf
COPY sftp.conf /etc/proftpd/conf.d/sftp.conf
COPY setup.sh /etc/proftpd/setup.sh
RUN chmod 500 /etc/proftpd/setup.sh && /etc/proftpd/setup.sh
EXPOSE 2222
ENTRYPOINT ["/bin/sh", "/etc/proftpd/entrypoint.sh"]
You can mount s3 in your docker container in next way
1.Add to Dockerfile
RUN apt-get install -y fuse s3fs
RUN mkdir /root/.aws
RUN touch /root/.aws/.passwd-s3fs && chmod 600 /root/.aws/.passwd-s3fs
COPY entrypoint.sh ./
RUN chmod 700 entrypoint.sh
ENTRYPOINT entrypoint.sh
2.Create entrypoint.sh with next script
#!/bin/sh
echo "$AWS_CREDS" > /root/.aws/.passwd-s3fs
echo "$BUCKET_NAME /srv/files fuse.s3fs _netdev,allow_other,passwd_file=/root/.aws/.passwd-s3fs 0 0" > /etc/fstab
mount -a
<your old CMD or ENTRYPOINT>
3.In docker-compose.yml add next
<your-container-name>:
image: ...
build: ...
environment:
- AWS_ID="AKI..."
- AWS_KEY="omIE..."
- AWS_CREDS=AKI...:2uMZ...
- BUCKET_NAME=<YOUR backed name>
devices:
- "/dev/fuse"
cap_add:
- SYS_ADMIN
security_opt:
- seccomp:unconfined

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

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.

Resources