Docker run php : Interactive shell - php

I am running the command
docker run php
and the terminal shows 'Interactive shell' and the docker image exits automatically. Here is the docker status
docker ps -a
"docker-php-entrypoi…" Less than a second ago Exited (0) 3 seconds ago

Please try the following:
docker run -it --rm php bash

You need to tell docker run that it's an interactive process and allocate a tty for keyboard input, i.e.
$ docker run -it php
Interactive shell
php >

php needs -a to run in an interactive mode. -it is to keep a persistent session. To get an interactive directly, just run:
docker run -it --rm php php -a

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

How to set an environment variable for an apache2 -DFOREGROUND [duplicate]

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

Does Php-fpm can process a cli script? What is happening inside official docker php?

I am using official Docker Php-fpm (https://github.com/docker-library/php/blob/master/7.2/alpine3.8/fpm/Dockerfile) and official Nginx Image for my php website. I have configured Nginx to talk to php-fpm over port 9000. The entrypoint of the docker is given below.
#!/bin/sh
set -e
# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- php-fpm "$#"
fi
exec "$#"
You can see when we try to execute a command like docker exec -it container whoami, it is prefixed with php-fpm.
So my question is when I pass a PHP cli script like e.g docker exec -it container composer install how it is interpreted ? does composer install processed by php-fpm or php-cli (/usr/local/bin/php) ?
As per my knowledge composer is a cli script which I install like below should be processed by php-cli.
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin -- --filename=composer
exec "$#" only executes the command that is defined as CMD in Dockerfile/Docker-compose file only once when the container is created initially. Once the container starts and you want to run any command on it then that command from docker exec doesn't override that CMD["php-fpm"]
Also composer script uses php-cli not php-fpm.
https://github.com/dbjpanda/composer/blob/70557f3ab7b84a896179396509611fae66b19773/bin/composer#L4

How to run Docker from php?

I am a beginner in using Docker, and I am trying to run docker from php, specifically I need to run openface code. I used the command lines provided here https://cmusatyalab.github.io/openface/setup/ to make sure that docker is running on my pc correclty and it works. but now I need to call it from PHP, I wrote the same commands in batch file as follows
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash
cd /root/openface
./demos/compare.py images/examples/{lennon*,clapton*}
pause
and tried to execute it in php by calling
echo shell_exec ("test.bat");
but when I run the batch file directly, only the first line is executed. as it seems the next command is not being executed inside the docker container.
how can I make all commands execute?
any help will be very much appreciated,
thank you
The problem is the first bash won't exit before you exit it and the rest of the commands are interpreted by host bash.
What you need is that your work is done and then you get inside a bash (inside the container)
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash -c "cd /root/openface && ./demos/compare.py images/examples/{lennon*,clapton*} && exec bash"
First "bash -c" is to execute the commands and last command exec bash override the main bash and gives you a shell

Running package manager inside the docker

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!

Categories