I've created a docker with a database and a php server but I'm failing accessing the php file from the server.
For testing purpose I'm currently having 2 index.php in my test app ./index.php and ./app/index.php
This is my docker-compose.yml
version: '3'
services:
symfony:
build:
context: .
dockerfile: docker/Dockerfile
image: project-manager
ports:
- 80:80
db:
image: mysql
ports:
- 3306:3306
volumes:
- "./.data/db:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8080:80"
links:
- db
This is the php dockerfile
FROM php:7.4-fpm
# Install Composer
COPY --from=composer /usr/bin/composer /usr/bin/composer
# Copy all our files in the docker root
COPY . /
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f80a16af8336 project-manager "docker-php-entrypoi…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, 9000/tcp project-manager_symfony_1
d97688010adf phpmyadmin/phpmyadmin "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 0.0.0.0:8080->80/tcp project-manager_phpmyadmin_1
55781c004031 mysql "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp project-manager_db_1
In my /etc/hosts
#Project Manager
127.0.0.1 project-manager.local
I can successfully access to the phpmyadmin using project-manager.local:8080
But if I try the simple project-manager.local/ or project-manager.local/index
I've got an empty response.
Root cause:
For symfony, you bind 80:80, this means you suppose there is a port 80 open in the php container. But, you use php:7.4-fpm which will just open port 9000.
(If you install net-tools in the container & use netstat -oanltp to check, there won't be 80 port open.)
Solutions:
Option 1:
If you insist to use php-fpm, then you need another web server container to pass the 80 request to php container's 9000 port. Maybe could add a more service with nginx container, and refers to connecting-nginx-to-php-fpm to set your configure for nginx container:
fastcgi_pass symfony:9000;
Option 2:
Switch to use php:7.4-apache, which defaults has a web server in the image open the 80 port, like next:
Dockerfile:
FROM php:7.4-apache
COPY . /var/www/html
index.php:
<?php
phpinfo();
NOTE: you should copy files to /var/www/html.
In a word, you should assure the container which you expose 80:80 really have a port 80 open in the container, otherwise, your expose is useless...
Related
I had edited a docker-compose.yml for my dockerized php application.
Bellow is the content of my docker-compose.yml file:
services:
httpd:
image: httpd:latest
ports:
- "80:80" # Default Apache port (Default on PHP 7.4)
- "8073:8073" # PHP 7.3 Apache port
- "8074:8074" # PHP 7.4 Apache port
- "8081:8081" # PHP 8.1 Apache port
volumes:
- ./:/var/www/html/myApp/:rw
- ./dev/Docker/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf
restart: on-failure
container_name: httpd
networks:
- mb-frontend
Code is shared between my host machine and the container as you see. But when i edit my code on the host machine, the code on the container isn't synchronized.
To see if there is changes on the container, I had copied the edited file from the container to my local machine. There is no changes. Code on the container and the host machine aren't the same (Different). Help please!!
My docker-compose file:
version: '3.8'
services:
cache:
image: memcached:latest
restart: always
ports:
- "10001:11211"
server:
build: '.'
tty: true
working_dir: /var/www
environment:
DISABLE_DEFAULT_SERVER: 1
AUTORELOAD_PROGRAMS: "kid_api"
AUTORELOAD_ANY_FILES: 1
volumes:
- ../../Apps/Server:/var/www
- ./php/config/custom.ini:/usr/local/etc/php/conf.d/custom.ini
- ./supervisor/kid_api.conf:/etc/supervisor/service.d/kid_api.conf
ports:
- "9988:9988"
depends_on:
- cache
links:
- cache
My Api.php file into /var/www open socket port for daemon server with Swoole.
When i tried docker-compose up, then ports is ok:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c80bee2baf7 php74_swoole4510_base_server "/entrypoint.sh" 2 hours ago Up 1 second 0.0.0.0:9988->9988/tcp php74_swoole4510_base_server_1
3b0f67cc3295 memcached:latest "docker-entrypoint.s…" 24 hours ago Up 2 hours 0.0.0.0:10001->11211/tcp php74_swoole4510_base_cache_1
but if i tried to run with PhpStorm
then the ports are not open
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7b534100202 php74_swoole4510_base_server "php /var/www/Api.php" 2 seconds ago Up Less than a second php74_swoole4510_base_server_run_df9a640e18bb
3b0f67cc3295 memcached:latest "docker-entrypoint.s…" 24 hours ago Up 2 minutes 0.0.0.0:10001->11211/tcp php74_swoole4510_base_cache_1
How fix it?
docker-compose up is not equivalent to docker-compose run.
PhpStorm does docker-compose run service_name command.
Docker Compose itself doesn't map ports in this case, you can check it in the terminal.
You can spin up the services with docker-compose up in the terminal manually, and then use PhpStorm in the exec mode (the option is visible on your screenshot).
I can't container to listen on port 8000, even after adding the port mapping on my docker-compose.yml file.
All relevant files can be found here: https://github.com/salvatore-esposito/laravel-dockerized
I ran the following commands: docker-compose exec app php artisan serve and it has run successfully.
Anyway if I go inside the container, curl works as expected, but it doesn't work from the outside. The connection gets refused.
I fetched the ip using docker-machine ip
Please note that I mapped the outside-inside port in my container via docker-compose.yml even if in the repository there si no map.
I tried to copy all files to a built image and launch:
docker run --rm -p 8000:8000 --name laravel salvio/php-laravel php artisan serve
and
docker exec -it laravel bash
Once more time if a run "curl localhost:80" and "curl localhost:8000" the former doesn't work and the latter it does whereas if I take the container's ip via docker inspect name_container and digit curl ip_of_container:8000 nothing.
When using docker-compose exec a command keeps running until it's interactive session is stopped(by using ctrl-c or closing the terminal) because it isn't running as a service. To be able to keep the following command running
docker-compose exec app php artisan serve
you would have to open 2 terminals, 1 with the command and 1 to connect to the container and ping port 8000
If you want to access your container port 8000 you would have to expose the port 8000 in the Dockerfile:
# rest of docker file
# Copy existing application directory permissions
#COPY --chown=www-data:www-data ./code /var/www/html
# Change current user to www-data
#USER www-data
# Expose port 9000 and start php-fpm server
EXPOSE 80
EXPOSE 80000
and map it to your host in docker-compose(file):
app:
build:
context: .
dockerfile: .config/php/Dockerfile
image: salvio/php-composer-dev
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www/html
ports:
- "80:80"
- "8000:8000"
volumes:
- ./code/:/var/www/html
- .config/php/php.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- myproject-network
Please keep in mind php artisan serve binds to localhost:8000. This means this is only reachable within the container. Use
php artisan serve --host 0.0.0.0
to bind to the shared network interface. Checkout the following resources:
https://stackoverflow.com/a/54022753/6310593
How do you dockerize a WebSocket Server?
This is my docker-compose.yaml
version: "3.1"
services:
mysql:
image: mysql:8.0.13
container_name: project-mysql
volumes:
- ./docker/data/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: db
MYSQL_USER: dbuser
MYSQL_PASSWORD: secret
ports:
- "3306:3306"
webserver:
image: nginx:alpine
container_name: project-webserver
working_dir: /var/www
volumes:
- .:/var/www
- ./docker/config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8080:80"
php-fpm:
build: ./docker/config/php
container_name: project-php-fpm
working_dir: /var/www
volumes:
- .:/var/www
environment:
XDEBUG_CONFIG: remote_host=172.17.0.1
PHP_IDE_CONFIG: serverName=docker
The problem is that php container can't connect to mysql.
Although from host I can connect with mycli -h 127.0.0.1 -P 3306 -u root.
But when I exec in php container and try it, get 'Connection refused'
Your mysql service binds mysql server's port 3306 to 3306 of the host machine. Thus is it normal that you can connect from host.
From inside php container, localhost is referring to that particular container as #David Maze said in the comments.
Since you are using docker-compose containers are in the same network, so you should use the service name in order to connect to the mysql server.
Try this from php container:
mycli -h mysql -P 3306 -u root
You are able to connect with user root. But in your docker-compose file, you are connecting through user dbuser. Let me know if that's not the issue.
Usually conenction refused you get when request is coming from the different ip than it's bind-address. Try below should work.
Make sure mysql configuration my.cnf or whatever default configs doesn't block connect from outside(check
bind-address in the mysql configuration if it 127.0.0.1 the its only
allow to connect form locally, i would for now make it 0.0.0.0 or
commented that line if exists)
mysqld --verbose --help => you will see all options
mysqld --verbose --help | grep bind-address => check the bind-address
Make sure the user i tried to login has enough privileges to
connect(SELECT user,host FROM mysql.user;) check your user can
connect from docker network => 172.* or anywhere=> %
It may be possible due to cached volume.
Try deleting your volume:
$ docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
07c7bdf3e34ab76d921894c2b834f073721fccfbbcba792aa7648e3a7a664c2e
my-named-vol
Total reclaimed space: 36 B
And then try any one of these:
docker-compose up --build
OR
docker-compose up
Remember: Before deleting your unused volumes run the following command if your docker-compose up is still running:
docker-compose down
I use Docker for my PHP development environment, and I set up my images with Docker Compose this way:
myapp:
build: myapp/
volumes:
- ./myapp:/var/www/myapp
php:
build: php-fpm/
expose:
- 9000:9000
links:
- elasticsearch
volumes_from:
- myapp
extra_hosts:
# Maybe the problem is related to this line
- "myapp.localhost.com:127.0.0.1"
nginx:
build: nginx/
ports:
- 80:80
links:
- php
volumes_from:
- myapp
elasticsearch:
image: elasticsearch:1.7
ports:
- 9200:9200
Nginx is configured (in its Docker file) with a virtual host named myapp.localhost.com (server_name parameter) and that points to the /var/www/myapp folder.
All this works fine.
But here is my problem: my web app is calling itself via the myapp.localhost.com URL with cURL (in the PHP code), which can be more easily reproduced by running this command:
docker-compose run php curl http://myapp.localhost.com
The cURL response is the following:
cURL error 7: Failed to connect to myapp.localhost.com port 80: Connection refused
Do you have any idea on how I can call the app URL? Is there something I missed in my docker-compose.yml file?
Months later, I come back to post the (quite straightforward) answer to my question:
Remove the server_name entry in the Nginx host configuration
Remove the extra_hosts entry in docker-compose.yml file (not necessary, but it's useless)
Simply call the server with the Nginx container name as a host (nginx here):
docker-compose exec php curl http://nginx