docker mount volumns but keep files inside the docker container - php

I use Dockerfile to build an image of php, but as the default, PHP don't have a php.ini file. So I need to create it. Now I have three ways to do this:
use RUN command in Dockerfile RUN cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini. But this way still use a default setting, I can't edit it (or edit it and commit container).
use COPY command in Dockerfile COPY php.ini /usr/local/etc/php/php.ini. OK, now I can edit the config and then build image, but if I need to change some setting, I need rebuild image
use volumn args, add this in docker-compose.yml - $PWD/php/conf.d:/usr/local/etc/php/conf.d/my_config. By this way, I can edit php.ini anytime and just reload config file. But, the best way I wonder is - $PWD/php/conf.d:/usr/local/etc/php/conf.d. the first way will create a new sub folder in docker container, the second way will delete the *.ini file that originally in the folder
I want to know is here some way that could mount volumn and don't delete the file in container (the second way of 3 but don't delete file)

Actually the files are not deleted from the subfolder. The volume is mounted over it.
Reference
What you can do is, assign a separate folder for your *.ini files, and reference it from the main php.ini file. So every time you reload php, it goes to php.ini and from there it gets your dedicated config folder and loads all the *.ini files from there.

Related

Move files from a docker container to an outside folder on the same server using php

I am fairly new to docker, please bear with me if its a basic question. I have a laravel project on a server, the project is dockerized. What I want to do is move a file from my project to another location on the same server that is not dockerized.
So, my project is setup on /var/www/my-project directory and I want to copy a file from my-project/storage/app/public/file.csv to /var/{destination_folder}. How can I do that in laravel? I think my issue is not related to laravel it is related to docker which is not allowing to move files out of it. Please don't add laravel or php file copy code snippets,I have tried plenty.
What I've tried?
1- I have tried copying file using:
Storage::disk('local')->put('/var/{destination_folder}', 'my-project/storage/app/public/file.csv' )
but, it does not copy the file.
2- I have also tried moving the file using bash script which I'm executing from my laravel controller using shell_exec or process but, it is also not working.
cp "/var/www/my-project/storage/app/public/file.csv" "/var/destination_folder"
What's hapening in this solution is that it is working when I run the command from terminal, but its not working when I call it from my controller and it gives me
cp: cannot create regular file '/var/destination_folder/file.csv': No such file or directory
After googling the above error it seemed that this is a permission issue wo, I changed the permission of the destination folder to 775 and I also checked the user from which I was running the laravel app and it gave me root when I ran whoami from the app.
Let me know how this could be achieved, thank you!
The entire point of docker is that it is isolated from the base host. You cannot simply copy the file out, as the docker host does not have access to any disk that is not mounted.
The easiest option is to create a destination directory and create a bind mount as per https://docs.docker.com/storage/bind-mounts/
You would then use the following argument for your docker run:
--mount type=bind,source=/var/destination_folder,target=/some_directory_inside_your_docker and copy the file to some_directory_inside_your_docker and it will appear in the parent host.
Another option is to generate a user account on the parent host, LOCK IT DOWN HARD for security reasons, and then have a private key inside your docker that would allow your docker to SSH to the parent host (note, this won't work with every network configuration). I don't think it's a good idea when you can do bind mounts, but it would work.

Calling Go using PHP's shell_exec is changing GOPATH

I have a Go app that must run from a PHP script with shell_exec()/exec(). The problem is when I call the Go script that way, the GOPATH changes or can't be found, and the imports from go get can't be found too. Everything is fine when I call it manually on terminal.
Trying to solve it, the GOPATH was set to /var/www on .bashrc and updated with source ~/.bashrc), but when I use go env under apache's user (www-data), the folders act like the Apache user can't read the environment variables and read the default values but the root folder from Go's view is /var/www, not /.
How can I make apache/PHP run Go properly?

Git setup for configuration files

I have a repository working on git. There is a file called config.php inside a directory called site_configuration. What i want is that when someone clones the repository it should download the default configuration file, and then if someone changes and pushes the new code, it should be ignored and shouldn't be pushed to the server.
Currently in my gitignore i have this, but for some reason it still tracks the file and pushes it to the repository.
site_configuration/config.php
You can't force Git to ignore changes to a tracked file (as, in your case, the site_configuration/config.php file) in a safe way. If you want the file to be ignored, you need:
Keep the .gitignore file as you have defined
Rename the site_configuration/config.php file to site_configuration/config.php_sample
Tell everybody to copy config.php_sample to config.php. Maybe you could have some automation for that.

How to specify the files to overwrite using linux cp command on PHP?

Is it possible to tell to linux cp command which files to overwrite via PHP?
Basically, I have searched the conflicts between the source and the destination folders, asked the user what files to overwrite and put them on an array.
Now I want to copy the files, overwriting only the files on the array.
Can you not call copy with only those files that need to be overwritten and exclude the files which are not to be copied?
By default cp overwrites the file, you can try the -f (force option) but that should bot be necessary.

Eclipse output to WAMP webserver www folder

I have an eclipse workspace folder. I want to store all the different projects I am working on over here. Now I am working on a PHP project and have WAMP installed. Is there some way to configure eclipse such that it outputs my project files to the server's www folder.
I don't want to create my workspace in the server's www folder, for the following reasons:
- I will work on multiple projects over time and I want all of them to reside in the same workspace
- I don't want to pollute the web server's www folder (though this is a development machine). I feel that if I have the code in a separate workspace, there is a low chance of me deleting it by mistake. I don't want to end up deciding to clean the www folder and deleting my only copy of the code!
Any alternative solutions to my concern are also welcome :)
You don't need to edit any php.ini files or anything crazy like that; you just need to change a couple of things in Eclipse.
Step #1:
Go into Eclipse and click on Window --> Preferences --> PHP --> PHP Servers
There should be a default server listed; you can edit it or just create a new one(this is what I did) and just set it as default later.
Click add or new or whatever it will come up with 2 boxes.
The first is just what you want to call the server; call it whatever you like.
The second is IMPORTANT -- this is where the www folder for your WAMP server is located. I have a shortcut to it on my desktop with a folder with all my php files in it.
I just typed localhost/name of my folder <--- your folder goes here.
It should be localhost/wamp/www if you don't have a shortcut. Don't hold me to that, though.
Step #2 : Running the program
When you have your php file you want to run, right click it and select Run as --> Run Configurations --> PHP webpage(double click). In the "File" area click Browse. Your project folder with your .php file you want to run should pop right up. If not, you'll have to look for it. Select it, then click "Run". Eclipse will open its default browser, and if all went well, you should be able to see your output.
It probably makes it easier if you just use a separate workspace for your PHP projects and store all the files in the www folder(Eclipse should make sub folders for new projects).
Hope this helps
You can keep the php code in your current eclipse workspace and then create virtual host in the httpd.conf file of apache server with document root pointing to this directory.
Only solution I found was to place your PHP files under the wamp\www folder.
Example:
c:\wamp\www\yourproject\index.php

Categories