Editing code on local machine not shared with docker httpd container - php

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!!

Related

Cannot make request internally to my symfony application served on localhost:8000 [duplicate]

I have created an nginx container that is open to port 8080:80
so I could access it from the host.
it is connected to php fpm container that has an open port 9000:9000
nginx successfully runs with php.
My problem is that php tries to access localhost:8080
but the problem is that the php localhost:8080 is not valid, it needs to connect to the nginx container.
here is the error on my wordpress site:
you can see that something is funky there...
below I'll attach my docker-compose.yml
Downloading install package from http://localhost:8080/wp-content/themes/realtyspace/plugins/advanced-custom-fields-pro.zip…
Download failed. cURL error 7: Failed to connect to localhost port 8080: Connection refused
docker-compose.yml
version: '2'
services:
my-nginx:
build: .
volumes:
- ./../:/var/www/html
ports:
- "8080:80"
links:
- my-php
my-php:
build:
context: .
dockerfile: Dockerfile.php-fpm
volumes:
- ./../:/var/www/html
ports:
- "9000:9000"
links:
- my-mysql
my-mysql:
image: mariadb:5.5
volumes:
- /var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: wp
MYSQL_DATABASE: wp
MYSQL_USER: wp
MYSQL_PASSWORD: wp
Use docker's internal networking and configure php to access http://my-nginx:80.
localhost will resolve to the isolated IP of the php container itself, not that of the Docker host that's running everything. And trying to pass http://dockerhost:8080 will result in a non-portable docker-compose.yml and likely issues with iptables firewall and nat rules that are more trouble than they are worth. The value of using the v2 compose files is that you get an isolated network internal to Docker with DNS resolution of each of your containers to work with each other.

Access index.php from docker

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...

Database connection from php container with host name "localhost" instead of container name

Our website-framework(s) are designed to work on both xampp and docker environments. We are recognizing our database hosts by host name/IP-address (dev, test, staging, live env). People who are working with xampp are using https://localhost, so they get the environment variable called Development. People who are working with docker are using https://docker as their host. They get the env-variable called Development/Docker. We need this differentiation because inside the php applications our xampp users are connecting to their mysql service with host localhost. Docker users have to connect via host called mysql (this is the container name of the mysql-service).
Because of the last occurred problems (not relevant to be mentioned here) we want a unique solution for both user-groups concerning the database connection: Docker users should be able to connect to their mysql service with host localhost.
docker-compose.yaml (shortened for better overview):
version: '2'
services:
#######################################
# PHP application Docker container
#######################################
app:
build:
context: .
dockerfile: Dockerfile.development
links:
- mail
- mysql
- redis
ports:
- "80:80"
- "443:443"
- "10022:22"
- "3307:3306"
volumes:
- ./app/:/app/
- ./:/docker/
cap_add:
- SYS_PTRACE
env_file:
- etc/environment.yml
- etc/environment.development.yml
environment:
- POSTFIX_RELAYHOST=[mail]:1025
#######################################
# MySQL server
#######################################
mysql:
build:
context: docker/mysql/
dockerfile: MariaDB-10.Dockerfile
ports:
- "3306"
volumes:
- mysql:/var/lib/mysql
env_file:
- etc/environment.yml
- etc/environment.development.yml
#######################################
# phpMyAdmin
#######################################
# /// #
#######################################
# Mail
#######################################
# /// #
#######################################
# Redis
#######################################
# /// #
# Volumes
volumes:
mysql:
phpmyadmin:
redis:
I tried a lot and played with the docker-compose but didn't find a solution for weeks. Tried with links, networks and so on. I think my docker skills are exhausted by now...
I also added to the mysql.conf:
bind-address = 0.0.0.0
Any ideas?
It is because of docker's networking structure.
docker creates 3 base interfaces. docker0 , host and none.
each container uses docker0 by default. then each container will have a virtual network interface for communicating between containers. so you can use your database with mysql address.
if you want to connect to database with address localhost you should config docker to use host network mode (you can do it by adding one line to defenition of your app service in docker compose file). it will be able to connect to every app which is running on your system. By the way you would loose communication with other container by their name. maybe you loose your connection with your redis (which is connected with redis address)
In this network mode, every dependency should be deployed or be exposed to your localhost.

How to access docker url from windows host

I have a php project in localhost which I can access from http://localhost:8000 and I have a service running in a docker container which I can access from http://192.168.56.100:8080. To access http://192.168.56.100:8080 I must configure my browser under manual proxy like this( this is from firefox configuration)
http proxy: proxy.comp.mycompanyname.com
No proxy for: localhost, 127.0.0.1,192.168.56.0/24
If I configure my browser above way then I can access this service http://192.168.56.100:8080 from browser.
Now the problem is, I want to connect to the service from my php project from localhost in windows machine. I tried this
$client = new Soapclient("http://192.168.56.100:8080");
This gives me error
PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'SoapFault: Could not connect to host"
I think I am missing some proxy configuration somewhere but not sure where. How to solve this?
Running following command gives me this
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.56.100:2376 v1.12.3
And my docker compose file
version: '2'
services:
idtool3-mysql-gpas:
image: mysql:5.7
container_name: tmf-gpas-1.7.8-mysql
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "gpas_2016"
volumes:
- ./gpas/sqls:/docker-entrypoint-initdb.d
labels:
- "service-name:gpas"
- "service-type:db-srv"
- "environment:test"
idtool3-gpas-wildfly:
image: tmfev/gpas:1.7.8
container_name: tmf-gpas-1.7.8-wildfly
links:
- "idtool3-mysql-gpas:mysqldb"
ports:
- "8080:8080"
depends_on:
- idtool3-mysql-gpas
labels:
- "service-name:gpas"
- "service-type:app-srv"
- "environment:test"
entrypoint: /bin/bash
command: -c "/opt/wait-for-it.sh mysqldb:3306 -t 120 && /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0"

Localhost connection refused in Docker development environment

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

Categories