How can I overwrite a file through Dockerfile in docker container? - php

I have to overwrite a file through Dockerfile. In particular on an Ubuntu container with Apache and PHP and I have to overwrite the file php5-cgi.conf.
I tried to use the following command:
COPY php5-cgi.conf /etc/apache2/conf-enabled/php5-cgi.conf
but I had the error: File already exists
I have also tried to use the following command
RUN cp -f php5-cgi.conf /etc/apache2/conf-enabled/
but the file is not copied when the container is running, Is there any advice on this?

Drop the file name from the destination:
COPY php5-cgi.conf /etc/apache2/conf-enabled/
The destination is an absolute path (not filename), or a path relative to the work directory, into which the source will be copied inside the destination container.

I just tested following docker file with no problem.
from debian:jessie
COPY debian_version /etc/debian_version
As PolarisUser stated in comments, you need to put debian_version in the same folder as dockerfile or use absolute path.
Another way would be mounting the file when running the container.
docker run -d -v php5-cgi.conf:/etc/apache2/conf-enabled/php5-cgi.conf --name your_container_name <imagename:tag> <startup command>

docker cp "yourfilename" "containername":/destination
Below is a working example:
docker cp config.json bigdataapp:/app/src/bigdatapp/wwwroot/assets/config/config.json

Related

unable to create files from linux library via php

I have an application in php that works on the docker. I would like to send a command from php code to container that should create files (some_dir/certs/cert.crt etc.). This command i run like this (by system/exec/shell_exec or symfony/process)
system("traefik-certs-dumper file --source acme.json --dest some_dir --version v2");
When php run this code then directory has been created but not files, also i don't have any error.
This command works when i make it from terminal via docker exec but not from php. This is probably some permission problem between php and docker container, but i don't know how can i set it.
I'm trying to set in docker file this, but not working:
RUN chmod 777 /go/bin/traefik-certs-dumper
RUN usermod -u 1000 www-data
also standard command works like this:
system("mkdir -p some_dir_1234");
system("touch some_dir_1234/some_file_1234");
How can I allow an installed library to create files?
I finally was able to find a solution. In a separate container, I had a process supervisor running, who saw this file, because it was mounted to the main application directory also. What had to be done was to mount the file to the main container and the supervisor container.
Try
exec("traefik-certs-dumper file --source acme.json --dest some_dir --version v2");
intead of system()

"no such file or directory" when passing full path to Docker image, but works with relative path to same folder

From a PHP page in Apache I'm trying to run a docker command that will generate a PDF. I've added both my user and the www-data user to the docker group so they can execute a docker command without sudo.
This works within a PHP file that exists at the /home/my_user/projects/my_project/public folder:
system("docker run --rm -v `pwd`:/app -w /app weasyprint:51 ./healthcheck.htm /weasyprint_test.pdf");
But when I specify the full path (which I've verified is correct), it doesn't work:
system("docker run --rm -v `pwd`:/app -w /app weasyprint:51 /home/my_user/projects/my_project/public/healthcheck.htm /home/my_user/projects/my_project/public/weasyprint_test.pdf");
I see this in the PHP error log when I run the second version:
su-exec: /home/my_user/projects/my_project/public/healthcheck.htm: No such file or directory
For the life of me, I can't figure out why the first version would work and the second wouldn't because they are referring to the same path, just via different syntax. Please let me know if you have any ideas.
If it helps, I'm using the this Docker image for Weasyprint: https://hub.docker.com/r/minidocks/weasyprint
On the Docker VM run ls /home/ and I would wager the directory you expect isn't there. The user referenced in your path probably doesn't exist on the VM, only on your host machine.
The relative path works because it's relative to the root directory provided.

Link Codeception Vendor Path with Linux

Right now I have a project here:
/srv/www/web/src/private/protected
It has a /vendors/ folder with /codeception in it. I have to currently do:
./vendor/codeception/codeception/codecept run
How can I link this directory/command 'codecept' so I don't have to type the absolute path every time?
I want:
codecept run
Instead of:
./vendor/codeception/codeception/codecept run
In your project folder you could
ln -s vendor/codeception/codeception/codecept .
to create a symlink in the current folder.
I added a symbolic link like so:
sudo ln -s /srv/www/web/src/private/protected/vendor/codeception/codeception/codecept /usr/local/bin/codecept
Edit your ~/.bashrc (or ~/.bash_profile). Add the following:
export PATH=$PATH:bin:vendor/bin
This will allow you to run any executables in both the bin/ and vendor/bin folders without having to cd into them.
You need to close and reopen your terminal for the change to take effect!
concatenate it to $PATH environment variable
PATH=$PATH:full_path_to_script
$PATH is an environment veritable used to find commands/scripts in wanted paths.
each time you are looking for a script or command it looks at the Paths that are listed in this variable
see your paths with:
~> echo $PATH

Forbidden in official image Docker PHP

I'm testing the official image docker PHP:
docker run -d -p 8000:80 --name test php:7-apache
Then, I test http://localhost:8000, I find this:
Forbidden
You don't have permission to access / on this server.
What am I doing wrong?
docker run -d -p 8000:80 --name test php:7-apache -v "$PWD":/var/www/html
Go to your project folder and run the above command or replace $PWD with your project directory. -v will sync your project to the docker container.
Or create a Docker file and use copy to run your project within the container.
Any one of the above will solve your problem.
The probable reason for the error is the fact that there is no project to render.
There's no hello world included with this image, /var/www/html/ is empty so there's no content to view. You need to bundle your own code in as the page on this image instructs:
We recommend that you add a custom php.ini configuration. COPY it into
/usr/local/etc/php by adding one more line to the Dockerfile above and
running the same commands to build and run:
FROM php:7.0-apache
COPY config/php.ini /usr/local/etc/php/
COPY src/ /var/www/html/
If you don't have files, it's behavior is normal.
But if you have, probabily is problem of permissions.
Error 403 is likely permission problem.
Inside container, run command:
$ ls -lsh
If letter 'r' (read) don't, you can try:
$ chmod +r <file>
Edit/add the .htaccess rewrite rule:
.htaccess
...
RewriteRule /
...

Docker Compose Wordpress example

I have been following the Quickstart Guide: Compose and Wordpress and can't get it to work.
I think it has something to do with my directory structure. After running all the steps I had a sub-directory called wordpress but I think all of my code referenced a sub-directory called code so I decided to rename my wordpress directory code. I then re-run docker-compose up but this also unfortunately didn't work.
UPDATE: By "can't get it to work" when I enter load the website the PHP server returns:
Not Found
The requested resource / was not found on this server
Your Dockerfile and docker-compose.yml should be inside your wordpress directory. (The one you renamed code)
Your confusion
For some reason you think there is a code directory involved. There is no directory like that in your wordpress and it's perfectly normal.
Let's break down the command that are confusing you :
In the Dockerfile :
ADD . /code
This line has for effect to copy the directory indicated (here it's . ) into the container directory indicated (here it's /code).
It's equivalent to a cp command only that you copy from the host to the container.
In the docker-compose.yml :
volumes:
- .:/code
This command is exposing a directory to a container. Here we expose the host directory . into the container directory /code.
Note : If you understood volume you might realize that the ADD instruction in your Dockerfile is unnecessary.
Why it's not working
Your docker-compose.yml and Dockerfile are above the intended working directory.
So when the docker-compose execute it copy the content of your current directory into the /code directory of the container.
So when php try to execute in /code it doesn't see the file he expect hence the Not found error.
Fix it
Either copy you Dockerfile and docker-compose.yml into the code directory (the one you renamed).
Or modify the Dockerfile and docker-compose.yml so that they point to the good directory.
By having a closer look to that quick start guide: The Dockerfile and the compose file should be inside your wordpress project folder that you created in the beginning. And also inside that wordpress folder there should be a sub-folder code. Run docker-compose from within the wordpress folder that way should work.
Unfortunately when you run the curl https://wordpress.org/latest.tar.gz | tar -xvzf - command from within your wordpress project folder it creates an other folder wordpress below your project directory. You should rename that one to code.

Categories