How can I mount a volume to docker container in OSX? - php

My ultimate goal is to run different versions of PHP on my local computer for testing.
I believe Docker is the best way to accomplish this.
I have been able to get a container with Apache and PHP running via this tutorial: https://github.com/tutumcloud/apache-php
But the issue is that I cannot mount a volume so that I can edit local files and view them on the docker container.
Here are my steps in terminal running in the same directory as the docker file:
docker build -t tutum/apache-php .
docker run -d -p 8080:80 tutum/apache-php -v /Users/user-name-here/apache-php/sample:/app/
The error I get back is:
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"-v\": executable file not found in $PATH".
I'm on OSX - El Captain, just installed latest version of Docker and Docker tools.

The basic format of the docker run command is:
docker run [<options>] <image> [<command>]
In this case, your image is tutum/apache-php, and the run command is being parsed like this:
docker run -d -p 8080:80 tutum/apache-php -v /Users/user-name-here/apache-php/sample:/app/
docker run
options: -d -p 8080:80
image: tutum/apache-php
command: -v /Users/user-name-here/apache-php/sample:/app/
This is the source of your error.
exec: "-v": executable file not found in $PATH
Since the command begins with -v, it will try to locate and execute that command. Of course, it doesn't exist, so you will get this error.
The fix is simply to move the -v option and its argument to the proper place.
docker run -d -p 8080:80 -v /Users/user-name-here/apache-php/sample:/app/ tutum/apache-php

Related

"docker: invalid reference format" when downloading latest version of php

I'm running this command on the Docker PowerShell terminal. I am using VS Code. It gives a "docker: invalid reference format." error message each time.
docker run -it --name php_dev --rm -v "${PWD}":/root php:latest bash
your code should work on terminal.. check on terminal.
Try this
docker run -it --name php_dev --rm -v "${PWD}:/root" php:latest bash

Is there anyway to start docker container without a service?

I have a php container which needs php-fpm to be started everytime I start the container . Now because of a wrong configuration in php-fpm config file , fpm does not gets started and so , container cannot start. Is there anyway that I can start the container without php-fpm so that I can fix the config file?
The container error is as follows :
[04-Sep-2020 13:47:30] ERROR: [/usr/local/etc/php-fpm.conf:7] value is NULL for a ZEND_INI_PARSER_ENTRY
[04-Sep-2020 13:47:30] ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf'
[04-Sep-2020 13:47:30] ERROR: FPM initialization failed
There are two ways to fix the image. Since I can't find image digitalocean/php, I'll use php:7.4-fpm in my example.
First way:
Copy file from the container and use it to build your own image:
Create Dockerfile:
FROM php:7.4-fpm
COPY ./php-fpm.conf /usr/local/etc/php-fpm.conf
Then:
docker run --detach --name php php:7.4-fpm tail -f /dev/null
docker cp php:/usr/local/etc/php-fpm.conf php-fpm.conf
docker stop php
docker rm -v php
# Edit php-fpm.conf
docker build --tag myphp-fm .
docker run --detach --name php myphp-fpm
and you get running container based on the fixed image.
Second way:
Run a shell using the broken image, fix the file and create a new image using the shell container
docker run -it --name php php:7.4-fpm bash
# Edit /usr/local/etc/php-fpm.conf
# If you install any additional tools remember to remove them afterwards
# and clean any cache's
# Once you're done exit the shell, thus stopping the container
docker commit -a "you" -m "/usr/local/etc/php-fpm.conf fix" php myphp-fpm
docker stop php
docker rm -v php
docker run --detach --name php myphp-fpm
and again you get running container based on the fixed image.
Of course, you can run your new image in whatever way you run the original image in the beginning.
I recommend the first way as it's way easier to edit the file outside the container.

Call Docker container outside docker

I have a project running under Apache and without docker. However, in this project, I have a PDF export and this PDF export I want to call it in a docker container. How do you do that?
Here is my docker-compose.yaml :
version: "3.3"
services:
php:
image: registry.gitlab.com/nodevo/keneo/php:latest
volumes:
- .:/srv:rw,cached
- ~/.ssh/id_rsa:/root/.ssh/id_rsa:ro
- ~/.composer:/tmp
browsershot:
image: ouranoshong/browsershot
links:
- chrome
chrome:
build: ./docker/chrome
cap_add:
- SYS_ADMIN
ports:
- '9223:9222'
The Browsershot service downloads the PDF and have node and npm. For that, it simulates a browser, so I have a chrome service but I don't know if it's the right method.
These 2 commands works in a terminal :
docker-compose run --rm browsershot node -v
docker-compose run --rm browsershot npm -v
However, Browsershot needs the path of the executable node and npm which are suddenly the 2 commands above just like that :
/**
* #param string $nodePath
* #param string $npmPath
*/
public function __construct(string $nodePath, string $npmPath)
{
$this->browserShot = new Browsershot();
$this->browserShot
->setNodeBinary($nodePath)
->setNpmBinary($npmPath)
->showBackground()
->setOption('fullPage', true)
->setOption('args', ['--disable-web-security'])
->emulateMedia('screen')
;
}
The variable $nodePath is docker-compose run --rm browsershot node and variable $npmPath id docker-compose run --rm browsershot npm.
This code works with executable in my local machine like /usr/local/bin/node and /usr/local/bin/npm.
But, when I launch the export with docker commands, I get this error:
HTTP 500 Internal Server Error
The command "PATH=$PATH:/usr/local/bin NODE_PATH=`docker-compose run --rm browsershot node docker-compose run --rm browsershot npm root -g` docker-compose run --rm browsershot node '/media/vdufour/DATA/Sites/keneo/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"file:\/\/\/tmp\/639306024-0718333001579852859\/index.html","action":"pdf","options":{"args":["--disable-web-security"],"viewport":{"width":800,"height":600},"fullPage":true,"emulateMedia":"screen","delay":2000,"displayHeaderFooter":false,"printBackground":true}}'" failed.
Exit Code: 1(General error)
Working directory: /media/vdufour/DATA/Sites/keneo/public
Output:
================
Error Output:
================
Couldn't connect to Docker daemon at http+docker://localhost - is it running?
If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
Couldn't connect to Docker daemon at http+docker://localhost - is it running?
If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
When using docker-compose, I just "ssh" into the container's shell like this:
docker-compose exec php /bin/bash
(where php is the container in my docker-compose.yml)
Rememvber though that for every terminal window you open, you also need to run the following to load in the environment vars:
eval $(docker-machine env)
Without doing that, you'll get the Couldn't connect to Docker daemon message.
Try to run commands with bash
docker-compose run --rm browsershot bash -c "npm && node && some_command..."

How to run a PHP script on docker?

I have index.php:
<?php
echo "Hello World";
?>
Dockerfile from the website: https://docs.docker.com/samples/library/php/
FROM php:7.2-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php" ]
I build image and run container:
docker build -t my-php-app .
docker run -p 7000:80 --rm --name hello-world-test my-php-app
I see only text "Hello World" but my application doesn't work in http://localhost:7000/ why?
If you want to run some script "on the fly" with php-cli you can create the container and remove it immediately after the script execution.
Just go to the directory with your code and run:
Unix
docker container run --rm -v $(pwd):/app/ php:7.4-cli php /app/script.php
Windows - cmd
docker container run --rm -v %cd%:/app/ php:7.4-cli php /app/script.php
Windows - power shell
docker container run --rm -v ${PWD}:/app/ php:7.4-cli php /app/script.php
--rm will remove the container after execution
-v $(pwd):/app/ will mount current directory
php:7.4-cli is the image
and php /app/script.php is the command which will be executed after the container is created
You can keep the same base image as you have php:7.2-cli:
FROM php:7.2-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php" ]
...build the image:
docker build -t my-php-app .
...run it:
docker run --rm --name hello-world-test my-php-app
You will obtain:
Hello World
Everything you did was correct except the port mapping (-p 7000:80) which is not necessary because you aren't running a web server.
== EDIT
If you want to run it as a web server, use the following Dockerfile:
FROM php:7.2-apache
COPY . /var/www/html/
...build it:
docker build -t my-php-app .
...and run it:
docker run -p 8080:80 -d my-php-app
You will then have your PHP script running on 8080.
 1. Create simple php script:
echo '<?php echo "Working";' > my.php
 2. Run docker:
docker run -p 8080:8080 --rm -v $(pwd):$(pwd) php:7.4-cli php -S 0.0.0.0:8080 $(pwd)/my.php
 3. Open in browser:
http://localhost:8080/
Many answers suggest using Apache for this, but that is not required. You need to have your application in the container run continuously on a specific port. You can keep the php:7.2-cli image, but your CMD should be different:
CMD [ "php", "-S 0.0.0.0:80", "./index.php" ]
This will run the built-in PHP webserver and after that you can expose it with the docker run command you already had
Here is a quick and simple example with Docker on Windows 11, assuming you have a similar directory structure as the example below:
C:\Users\YourName\Workspace\MyProject\program.php
And program.php has the following content:
<?php echo "It works!"; ?>
Then, in the Command Prompt, navigate to the project directory:
cd C:\Users\YourName\Workspace\MyProject
Run with CLI
docker run --rm -p 8080:8080 -v %CD%:/cli php:7.4-cli php -S 0.0.0.0:8080 /cli/program.php
View: http://localhost:8080
Run with SERVER
docker run --rm -d -p 8081:80 -v %CD%:/server --mount type=bind,source="%CD%",target=/var/www/html php:apache
View: http://localhost:8081/program.php
Then feel free to modify program.php and refresh the page.
Environment
Docker version 20.10.16, build aa7e414
Windows 11 Home, Version 22H2, OS build 22622.436

Docker "Operation not permitted" issue on Windows

I'm trying to use Docker on Windows through Docker Toolbox, but I'm struggling to make it work. I've pulled Docker PHP image. For example, this simple ls command fails:
$ docker run -it --rm -v /$(pwd):/home/projects php:7.0-cli ls -l /home/projects
ls: cannot open directory /home/projects: Operation not permitted
Also, any other operation within the mounted volume fails with Operation not permitted message.
Looks like a path issue with the volume mapping. Docker Toolbox uses Git Bash for the terminal, which uses /c as the root of the C: drive:
$ echo $(pwd)
/c/Users/elton
So your /$(pwd) is prepdening an extra forward slash. I'd try with a fully-qualified path first just to verify:
$ docker run -it --rm -v /c/projects:/home/projects php:7.0-cli ls -l /home/projects

Categories