I need to create a Dockerfile for my php service. I just tried many options found in web, and none work correctly. I have simple PHP project in /home/karol/docker-file/service/start.php. Really important for me. Thanks in advance.
Currently looking like this:
FROM php:5.6-apache
COPY ./ /var/www/html/
WORKDIR /usr/src/service
CMD [ "php", "./requestdispatcher.php" ]
Reference: https://hub.docker.com/_/php/
DockerFile
FROM php:5.6-cli
COPY /home/karol/docker-file/service /usr/src/service
WORKDIR /usr/src/service
CMD [ "php", "./start.php" ]
Build docker image
docker build -t imagename .
Run container
docker run -it --rm --name conatainername imagename
When running as a service
FROM php:5.6-apache
COPY /home/karol/docker-file/service /var/www/html/
Build Image
docker build -t imagename .
Run Container
docker run -d -p 8080:80 --name conatainername imagename
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
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 created a Docker container with Apache on it. Everything works, except that I can't link MySQL (https://hub.docker.com/_/mysql/) to my container, and I'm not sure what I did wrong.
Here are the steps I did:
I run mysql server container
docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=pass123 -d mysql/mysql-server:latest
Then, I run and link my Apache Docker:
docker run -it --link mysqlserver:mysql -v "$(pwd)":/var/www/html -p 80:80 -p 3306:3306 apachebash
But I cant access MySQL.
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!