How to use enviroment variable that is set in docker composer - php

I am using docker container that runs codeigniter application and I have setted enviroment variable for base url in docker composer.yml like:
version: '3.4' services:
app:
image: WEBPORTAL_VERSION
ports:
- port_key:port_num
environment:
- BASE_URL=http://example.com
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.role == manager
Now, I want to access the environment key i.e. base url in codeigniter application
I am using:
$_config['base_url']=getenv('BASE_URL');
where BASE_URL is key initialized in docker composer file above.
The problem here is getenv do not fetch from environment set in docker composer?

Finally! solved the problem, by default in php-fpm config file was taking
clear_env = yes
which is the default value, I changed it and updated as
clear_env=no

Related

Multiple Docker Containers Using the Same Laravel Source

I are using Docker for our deployments and for local development. We are running the same Laravel application for many customers .env file. In order to get our local to work properly with multiple sites we needed to figure out a way for each site to have it's own environment file. We created a folder structure like the following.
docker/
- src/ <-- Laravel App Here
- local/
- env/
- site1.env
- site2.env
The directory we are using for our environment files is being used in our docker-compose.yml file currently as a volume.
- ./local/env/site1.env:/var/www/html/.env:delegated
This is copying the proper env file to our containers /var/www/html directory and this works great. However, where we are experiencing an issue is that when you do docker-compose up -d it is syncing the .env file back to our src/ directory since we have a mount for that as well.
- ./src:/var/www/html/:delegated
Where the issues start to arise is when you open the .env file that is synced back to src/, not only is the file an empty file but if you add anything to it and save it will sync back to the container killing the site because it no longer has all of the env entries.
I would like to figure out a way to ignore the .env file so that it does not sync back to the src/ directory. Laravel requires it to exist in the /var/www/html directory in the container and I understand why it is syncing back down to the src/ directory but I am unsure how to stop it or if it is even possible.
Am I doing this wrong? I thought I would give configs a shot but I found that I cannot use configs because we are not running in swarm mode.
An example docker-compose entry is below
site1:
build:
context: .
dockerfile: DevDockerfile
working_dir: /var/www/html
environment:
DB_HOST: mysql
DB_PORT: 3306
DB_DATABASE: dbname
DB_USERNAME: dbusername
DB_PASSWORD: password-here
volumes:
- ./src:/var/www/html/:delegated
- ./local/client/site1/:/var/opt/client/:delegated
- ./local/env/site1.env:/var/www/html/.env:delegated
networks:
- ournetwork
depends_on:
- mysql
- smtp

Using Laravel Websocker with docker/ docker-compose.yml is not working

I am developing a Laravel application. I am trying to use Laravel Websocket in my application, https://docs.beyondco.de/laravel-websockets. I am using Docker/ docker-compose.yml. Since the Laravel Websocket run locally on port 6001, I am having problem with integrating it with docker-compose. Searching solution I found this link, https://github.com/laradock/laradock/issues/2002. I tried it but not working. Here is what I did.
I created a folder called workspace under the project root directory. Inside that folder, I created a file called, Dockerfile.
This is the content of Dockerfile
EXPOSE 6001
In the docker-compose.yml file, I added this content.
workspace:
port:
- 6001:6001
My docker-compose.yml file looks something like this
version: "3"
services:
workspace:
port:
- 6001:6001
apache:
container_name: web_one_apache
image: webdevops/apache:ubuntu-16.04
environment:
WEB_DOCUMENT_ROOT: /var/www/public
WEB_ALIAS_DOMAIN: web-one.localhost
WEB_PHP_SOCKET: php-fpm:9000
volumes: # Only shared dirs to apache (to be served)
- ./public:/var/www/public:cached
- ./storage:/var/www/storage:cached
networks:
- web-one-network
ports:
- "80:80"
- "443:443"
php-fpm:
container_name: web-one-php
image: php-fpm-laravel:7.2-minimal
volumes:
- ./:/var/www/
- ./ci:/var/www/ci:cached
- ./vendor:/var/www/vendor:delegated
- ./storage:/var/www/storage:delegated
- ./node_modules:/var/www/node_modules:cached
- ~/.ssh:/root/.ssh:cached
- ~/.composer/cache:/root/.composer/cache:delegated
networks:
- web-one-network
When I run "docker-compose up --build -d", it is giving me the following error.
ERROR: The Compose file './docker-compose.yml' is invalid because:
Unsupported config option for services.workspace: 'port' (did you mean 'ports'?)
What is wrong and how can I fix it? How can I use Laravel Web Socket with docker-compose?
I tried changing from 'port' to 'ports', then I got the following error message instead.
ERROR: The Compose file is invalid because:
Service workspace has neither an image nor a build context specified. At least one must be provided.
Your Dockerfile is wrong. A Dockerfile must start with a FROM <image> directive as explained in the documentation. In your case it might be sufficient to run an existing php:<version>-cli image though, avoiding the whole Dockerfile stuff:
workspace:
image: php:7.3-cli
command: ["php", "artisan", "websockets:serve"]
Of course you will also need to add a volume with the application code and a suitable network. If you add a reverse proxy like Nginx in front of your services, you don't need to export the ports on your workspace either. Services may access other services as long as they are in the same network.

Environment variables not found symfony

I have a docker container :
services:
php-fpm:
build:
context: ./docker/php-fpm
volumes:
- ./symfony:/home/home
container_name: php
ports:
- "9004:9001"
networks:
- local
working_dir: /home/home
environment:
- DATABASE_HOST=test
- DATABASE_PORT=
- DATABASE_NAME=test
I made :
docker-compose build --no-cache
docker-compose up
clear cache
When I refresh the page I get : Environment variable not found: "DATABASE_HOST".. What's the problem I don't understand. I spent a lot of time analyze this issue. Have you an idea about that ? Thx in advance. Btw when I do docker inspect I see all this environment variables assigned.
I've run into this problem myself. You have to explicitly map the environment variables you want to make accessible to php/symfony in php-fpm.conf like:
[www]
env[MY_ENV_VAR_1] = 'value1'
env[MY_ENV_VAR_2] = 'value2'
However that doesn't seem to work with actual enviornment variables from the host!.
There is a long discussion of that here (along with several, what seem to me, laborious work-arounds to the problem: https://groups.google.com/forum/#!topic/docker-user/FCzUbjTIp_0
I've successfully done it in the pool.d configuration file like so:
env[DATABASE_HOST] = $DATABASE_HOST
env[DATABASE_PORT] = $DATABASE_PORT
env[DATABASE_NAME] = $DATABASE_NAME
I just add this in as part of the docker file:
ADD fpm/app.pool.conf /etc/php5/fpm/pool.d/
If you extend from the official PHP Docker image, it sets clear_env = no for you or you can set it yourself in your image's pool config.
Here's the line adding clear_env = no to the config in the official image. You just need to add this to your FPM pool config, or you can add them one-by-one if you prefer as shown by Robert.

Docker - Pass a environment setting via docker-compose

I am new to Docker and have a working docker-compose file, apart from one part. What I want to achieve is to set an environment setting so that in my PHP application I can use some variables to determine which resources I load in.
In MAMP PRO, for instance, you can access environment settings on this page:
.
In my docker-compose file I have the following:
services:
webserver:
build: ./docker/webserver
image: perch
ports:
- "80:80"
- "443:443"
volumes:
- C:/websites/sitename/www:/var/www/html
links:
- db
environment:
- DEVELOPER_ENV=development
At the moment the variable - from what I can tell - isn't being set as my php variables to detect the environment, fail. Any pointers would be appreciated.
Apache by default removes most environment variables for security reasons.
But you can whitelist variables in the etc/apache2/conf-enabled/expose-env.conf file.
So I added theses commands to my dockerfile:
RUN
echo 'PassEnv DB_PW' >> /etc/apache2/conf-enabled/expose-env.conf \
&& echo 'PassEnv DB_USER' >> /etc/apache2/conf-enabled/expose-env.conf
Alternatively you can copy or mount the expose-env.conf.

Retain environment variable values for Symfony2 app development in Docker

I have a Symfony2 app running inside Docker container. The parameters.yml file is set to pick environment variables as following:
framework:
secret: "%env.secret%"
...
The Docker compose file contents are:
services:
my-website:
env_file: my-website.env
build: .
expose:
- "80"
volumes:
- .:/app
The environment variables file is:
SYMFONY__ENV__SECRET=1234567890
...
Everything works fine when accessed via "app.php". However when accessed in dev mode (via app_dev.php) it fails to pick the environment variables. Is there any way to use the environment variables? I do not want to create another parameters.yml file with hardcoded values. Than you!

Categories