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
Related
Here is my Dockerfile:
FROM ros:kinetic-ros-core-xenial
CMD ["bash"]
If I run docker build -t ros . && docker run -it ros, and then from within the container echo $PATH, I'll get:
/opt/ros/kinetic/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
If I exec into the container (docker exec -it festive_austin bash) and run echo $PATH, I'll get:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Why are the environment variables different? How can I get a new bash process on the container with the same initial environment?
The ENTRYPOINT command is only invoked on docker run, not on docker exec.
I assume that this /ros_entrypoint.sh script is responsible for adding stuff to PATH. If so, then you could do something like this for docker exec:
docker exec -it <CONTAINER_ID> /ros_entrypoint.sh bash
docker exec only gets environment variables defined in Dockerfile with instruction ENV. With docker exec [...] bash you additionally get those defined somewhere for bash.
Add this line to your Dockerfile:
ENV PATH=/opt/ros/kinetic/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
or shorter:
ENV PATH=/opt/ros/kinetic/bin:$PATH
This is old question but since it's where google directed me I thought I'll share solution I ended up using.
In your entrypoint script add a section similar to this:
cat >> ~/.bashrc << EOF
export PATH="$PATH"
export OTHER="$OTHER"
EOF
Once you rebuild your image you can exec into your container (notice bash is invoked in interactive mode):
docker run -d --rm --name container-name your_image
docker exec -it container-name /bin/bash -i
If you echo $PATH now it should be the same as what you have set in .bashrc
Problem
I'm able to find the httpd.conf when I run a container from the httpd image, but I can't find that file when I run the container from a PHP image that includes apache, like the image php:7.4.2-apache-buster
The php image was ran this way, also $(pwd) is pointing to the proyect directory
docker container run -d -p 80:80 \
--name containerName \
-v $(pwd):/var/www/html \
php:7.4.2-apache-buster
Tried
I used docker container exec -it normalApache bash in the normal httpd image, in which I was able to find the httpd.conf file in /usr/local/apache2/conf
But when I try to do the exact same thing in the apache included with the php image, I can't find that file because the directory apache2 in /usr/local doesn't even exit
Whatever you are looking for, the Dockerfile for that container can be found at https://github.com/docker-library/php/blob/703a3d0a4e4c149bfd62fc3e7b71645f9496b178/7.4/buster/apache/Dockerfile, and in line 52 it uses /etc/apache2 as the configuration directory
51 ##<autogenerated>##
52 ENV APACHE_CONFDIR /etc/apache2
53 ENV APACHE_ENVVARS $APACHE_CONFDIR/envvars
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
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
I've built an image for the purpose of PHP development, and it became clear to me that I didn't really thought about how to access the tools that I need for every day development. For example: composer, package manager for PHP, I need it to run whenever composer.json updates. I thought it is worth installing those tools inside the same image, but then I don't have a way to access them. So, I can:
Create separate image for composer and run it in different container
Install composer on my host machine.
I'd like to avoid option 2), but then, does it have sense having a setup like 1) ? How did you guys solved this issue ?
Unless you have some quite specific requirements there is a third option:
Connect to the container using docker exec command:
docker exec -it CONTAINER-NAME/ID COMMAND [ARG...]
Here is the example:
1: Create your application:
echo "<?php phpinfo();" > index.php
2: Start container:
docker run -it --rm --name my-apache-php-app -p 80:80 -v "$PWD":/var/www/html php:5.6-apache
3: Open another terminal window and exec required commands inside running container:
docker exec -it my-apache-php-app curl -sS https://getcomposer.org/installer | php
docker exec -it my-apache-php-app ls
If you need shell inside running container - run:
docker exec -it my-apache-php-app bash
That's it!