I am trying to set up ECS in order to run my php/nginx docker application.
It works locally using this docker-compose.yml file:
version: '2'
services:
nginx:
image: NGINX-IMAGE
ports:
- 80:80
links:
- php
volumes_from:
- php
environment:
APP_SERVER_NAME: <ip>
php:
image: PHP-IMAGE
ports:
- 9000:9000
volumes:
- /var/www/html
The problem is that I can't get this working using ECS.
I don't know how to create the web-data volume and let nginx grap it using volumes_from.
I am trying to create the volume using this JSON:
volumes='[
{
"name": "webdata",
"host": {
"sourcePath": "/var/www/html"
}
}
]'
And then in my container-definitions to the php-container I add:
"mountPoints":
[
{
"sourceVolume": "webdata",
"containerPath": "/var/www/html",
"readOnly": false
}
]
However, when I do this, it adds the content from the host's /var/www/html folder to the /var/www/html folder of the containers.
My question is, how do I configure the volume to use the data from the php's /var/www/html container and let nginx access this data?
I managed to find a solution that suited the setup for ECS.
I simply created a VOLUME in my php Dockerfile referencing /var/www/html.
This means I longer need to reference the volume in the volumes section of the php container. And nginx will still be able to access the volume with volumes_from.
Update
This is my working task definition for ECS:
task_template='[
{
"name": "nginx",
"image": "NGINX_IMAGE",
"essential": true,
"cpu": 10,
"memoryReservation": 1000,
"portMappings": [
{
"containerPort": 80,
"hostPort": 80
}
],
"environment" : [
{ "name" : "APP_SERVER_NAME", "value" : "%s" }
],
"links": [
"app"
],
"volumesFrom": [
{ "sourceContainer": "app" }
]
},
{
"name": "app",
"image": "IMAGE",
"essential": true,
"cpu": 10,
"memoryReservation": 1000,
"portMappings": [
{
"containerPort": 9000,
"hostPort": 9000
}
]
}
]'
And then I added VOLUME ["/var/www/html"] to my app Dockerfile. Now nginx can access the data with the volumes_from argument in the task definition.
Related
I want to ask about debugging Yii 1.1 applications. I've tried implementing the answers on StackOverflow and other websites, but my VSCode still can't debug the application, the breakpoints that have been set are never read at all. I am using Docker to run Yii.
Here are the details of the file I used.
docker-compose.yml
version: '3'
services:
web:
container_name: php72
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:80"
volumes:
- ./:/var/www/html
- ./xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
networks:
- app-network
mysql:
image: mysql:8.0.31-oracle
restart: always
environment:
MYSQL_ROOT_PASSWORD: '123456'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: '123456'
MYSQL_DATABASE: 'test_db'
volumes:
- db_data:/var/lib/mysql
ports:
- 3306:3306
networks:
- app-network
networks:
app-network:
volumes:
db_data:
Dockerfile
FROM php:7.2-apache
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && \
install-php-extensions gd xdebug pdo pdo_mysql pdo_pgsql mongodb mbstring zip
EXPOSE 80
xdebug.ini
zend_extension=xdebug
[xdebug]
xdebug.mode=debug
xdebug.discover_client_host=1
xdebug.idekey=VSCODE
xdebug.start_with_request=yes
xdebug.client_port=9003
xdebug.remote_host="host.docker.internal"
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:8001"
],
"program": "",
"cwd": "${workspaceRoot}/../../",
"port": 9003,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}
And here is my project structure:
I access my apps in a browser with localhost:8000, then I try to turn on the VSCode debugger, but this is the result:
Any help is very much appreciated.
Is there any missing configuration?
After watching several videos on how to setup Xdebug for an application running inside a docker container, I finally found the answer that works for my case.
I changed my docker-compose.yml to be like this:
version: '3'
services:
web:
container_name: php72
build:
context: .
dockerfile: Dockerfile
extra_hosts:
- "host.docker.internal:host-gateway" // Add extra host for docker
ports:
- "8000:80"
volumes:
- ./:/var/www/html // this is the remote path where my apps installed
- ./xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
networks:
- app-network
mysql:
image: mysql:8.0.31-oracle
restart: always
environment:
MYSQL_ROOT_PASSWORD: '123456'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: '123456'
MYSQL_DATABASE: 'wms_test'
volumes:
- db_data:/var/lib/mysql
ports:
- 3306:3306
networks:
- app-network
networks:
app-network:
volumes:
db_data:
And as per as #LazyOne advise, since I am using Xdebug 3 I have change my xdebug.ini also to be like this:
zend_extension=xdebug
[xdebug]
xdebug.mode=develop,debug
xdebug.start_with_request=yes
xdebug.client_port=9003
xdebug.client_host=host.docker.internal
xdebug.idekey="VSCODE"
xdebug.log=/tmp/xdebug_remote.log
And the important part, after watching this YouTube video Setup Xdebug WITH DOCKER and debug in VSCode I've figured out what is missing in my previous setup, and that is the pathMapping. So I've changed my launch.json to be like this:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html" : "${workspaceFolder}"
}
}
]
}
And voila that work like a charm:
I've set up a new laravel project and here's the step I followed :
install laravel on docker using
curl -s "https://laravel.build/example-app" | bash
changing mysql port as 3306 is already used in my machine. Files have changed
.env file
DB_CONNECTION=mysql
DB_HOST=0.0.0.0
DB_PORT=4306
DB_DATABASE=example_app
DB_USERNAME=sail
DB_PASSWORD=password
docker-compose file (mysql container section)
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-4306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
build and run container
./vendor/bin/sail up
run 1st migration
Everything works fine at this point.
Then I install breeze package and run the migration
composer require laravel/breeze && php artisan migrate
Everything is still working at this point.
Then when I try to register a new user entering the adress localhost/register, I get the following error
lluminate \ Database \ QueryException
SQLSTATE[HY000] [2002] Connection refused
select count(*) as aggregate from `users` where `email` = my_new_user#test.com
Am I missing something in the .env or the docker-compose.yml ?
Thanks a lot
I found a fix by changing the environment variable with the Gateway IP
Use this command to find this IP
docker network inspect bridge
you should have a result like the following :
[
{
"Name": "bridge",
"Id": "17da7215587ae714f631d213fed173550b69d38a5824c1998e1c3cd4b2e954b",
"Created": "2022-10-26T19:26:38.585884296Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
use the Gateway entry in your .env file
I have 3 docker containers that are supposed to talk to each other. A very classic stack with nginx, php-fpm and mariadb.
My stack used to work, but now my containers can't seem to talk to each other anymore, and I don't know what changed. The nginx can't talk to the php-fpm, and the php-fpm can't talk to mariadb.
Here is the docker-compose I use :
version: '3'
services:
web-nginx:
image: nginx:stable-alpine
container_name: web-nginx
restart: always
ports:
- "80:80"
web-php:
restart: always
build:
context: ./php
dockerfile: Dockerfile
container_name: web-php
web-mariadb:
build:
context: ./mariadb
dockerfile: Dockerfile
restart: always
container_name: web-mariadb
ports:
- "3306:3306"
The content of docker-compose ps :
docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------
web-mariadb docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
web-nginx /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/tcp
web-php docker-php-entrypoint php-fpm Up 9000/tcp
My DockerFiles :
FROM php:fpm-alpine
ENV USER=docker
ENV UID=1000
RUN adduser \
--disabled-password \
--gecos "" \
--home "$(pwd)" \
--no-create-home \
--uid "$UID" \
"$USER"
[...]
USER docker
FROM mariadb
RUN echo max_allowed_packet=512M >> /etc/mysql/my.cnf
The nginx.conf file
[...]
http {
[...]
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
include fastcgi_params;
fastcgi_pass web-php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
}
}
}
So my php-fpm is supposed to be accessed through port 9000 while mariadb listens to the classical 3306.
Between containers, ping commands are OK, but nc are not (I use nc because telnet is not bundled in alpine images):
docker exec -u 0 -it web-nginx sh #I use -u 0 to be root because otherwise ping is not allowed in the container
/ # ping -c1 web-php
PING web-php (172.22.0.3): 56 data bytes
64 bytes from 172.22.0.3: seq=0 ttl=64 time=0.156 ms
--- web-php ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.156/0.156/0.156 ms
/ # nc -v web-php 9000
nc: web-php (172.22.0.3:9000): Host is unreachable
docker exec -u 0 -it web-php sh
ping -c1 web-mariadb
PING web-mariadb (172.22.0.4): 56 data bytes
64 bytes from 172.22.0.4: seq=0 ttl=64 time=0.199 ms
--- web-mariadb ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.199/0.199/0.199 ms
/var/www/html # nc -v web-mariadb 3306
nc: web-mariadb (172.22.0.4:3306): Host is unreachable
Here is the docker inspect command :
[
{
"Name": "web_default",
"Id": "e458e63d89e3f6e2c35644aa03f377dc9fba6975b4b8838727b85c57c3b183ba",
"Created": "2021-01-28T15:00:06.814105722+01:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.22.0.0/16",
"Gateway": "172.22.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"af441d802fd26173ea98ca81b85eb48cbea36b65b867df46b6ddb2f5ac484816": {
"Name": "web-nginx",
"EndpointID": "6b69e2033b3de50c86161cbec103843de5f83ab53ecebd557ff66ae57adaae04",
"MacAddress": "02:42:ac:16:00:02",
"IPv4Address": "172.22.0.2/16",
"IPv6Address": ""
},
"be99788a48d03fb51288aab6373596c41ed8adcbcf8ceaa8ae2ece1e0aa10843": {
"Name": "web-mariadb",
"EndpointID": "8f52e8fe97f24161934b9ab3dcdde9da69aeb80388deb3f6442dd089554e30f0",
"MacAddress": "02:42:ac:16:00:04",
"IPv4Address": "172.22.0.4/16",
"IPv6Address": ""
},
"f1d2aa6a6c2e156416da3ca0424336380b01950d6f0990203a2d0484ad0d04a5": {
"Name": "web-php",
"EndpointID": "7e6b95ccb7da5bcdd3754d1965820e429bdf5370cecee0bceb29c79138d36e0e",
"MacAddress": "02:42:ac:16:00:03",
"IPv4Address": "172.22.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "web",
"com.docker.compose.version": "1.27.4"
}
}
]
Since ping works between containers, I assume network is correct, but ports are not opened.
Also, I noticed that my containers can't reach internet. Any ping to google or just "outside" is rejected.
I tried restarting the docker service and even reboot my server, but no luck.
I don't know how to troubleshoot this.
Server running centos 8 :
Linux myserver 4.18.0-240.10.1.el8_3.x86_64
Docker version 20.10.2, build 2291f61
Any help much appreciated.
Thanks !
We are developing a laravel application using docker and kubernetes. While I am handling docker part and other guy handles kubernetes part.
Site was made live but other developer asked me to provide a code to use laravel-echo-server's host url as ws.example.com instead of example.com:6001.
Since port 6001 is used by echo server, I had created a docker-composer.yml its contents are as follows:
.
.
.
# Laravel Echo Server
laravel-echo-server:
build:
context: ./docker_files/laravel-echo-server
dockerfile: Dockerfile
container_name: bns_echo_server
volumes:
- ./docker_files/laravel-echo-server/laravel-echo-server.json:/var/www/laravel-echo-server.json:ro
depends_on:
- app
- webserver
ports:
- "6001:6001"
links:
- redis
networks:
- app-network
.
.
.
its corresponding Docker file is as follows:
FROM node:alpine
# Create app directory
RUN mkdir -p /var/www
#WORKDIR /usr/src/app
WORKDIR /var/www
# Install app dependencies
COPY package.json /var/www
RUN apk add --update \
python \
python-dev \
py-pip \
build-base
RUN npm install
# Bundle app source
COPY laravel-echo-server.json /var/www/laravel-echo-server.json
EXPOSE 3000
CMD [ "npm", "start", "--force" ]
and the laravel-echo-server.json file is as follows:
{
"authHost": "http://webserver",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "123",
"key": "123"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"port": "6379",
"host": "redis"
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "*.*",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
authHost is http://webserver as webserver is the container name of nginx
Please guide us how can we achieve this goal ?
While setting up a php dev environment with docker, I ran into an issue while setting up remote debugging (XDEBUG) through a dbgp proxy.
Connecting my host machine to the proxy doesn't seem to be a problem, but the proxy container cannot reach my host machine over the port that is configured (in this case 9003)
I'm using docker compose on windows.
Sucessfully connecting my dev machine to the proxy:
INFO: dbgp.proxy: Server:onConnect ('172.18.0.1', 36558) [proxyinit -p 9003 -k XDEBUG_IDEA -m 1 ]
When executing a request containing the right IDE key (e.g. http://localhost/?XDEBUG_SESSION_START=XDEBUG_IDEA), the proxy reacts correctly but is unable to contact the gateway over the port that was registered by my dev machine)
INFO: dbgp.proxy: connection from 172.18.0.2:40902 [<__main__.sessionProxy instance at 0x7fcff1998998>]
ERROR: dbgp.proxy: Unable to connect to the server listener 172.18.0.1:9003 [<__main__.sessionProxy instance at 0x7fcff1998998>]
Traceback (most recent call last):
File "/usr/local/bin/pydbgpproxy", line 223, in startServer
self._server.connect((self._serverAddr[0], self._serverAddr[1]))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 111] Connection refused
WARNING: dbgp.proxy: Unable to connect to server with key [XDEBUG_IDEA], stopping request [<__main__.sessionProxy instance at 0x7fcff1998998>]
INFO: dbgp.proxy: session stopped
Any ideas on what is going wrong here?
Firewall issues can be excluded here, since i basically just turned it of.
I did log into the dbgpproxy container and was able to send requests to the gateway ip over ports 80 an 8080, but got the same connection refused when trying port 9003)
Any pointers would be greatly appreciated!
The docker compose file:
version: '2'
volumes:
mysqldata:
driver: local
services:
app:
restart: "always"
image: php:7.0-fpm
command: "true"
volumes:
- .:/var/www/html
nginx:
restart: "always"
build: ./docker/nginx/
ports:
- "80:80"
depends_on:
- php
php:
restart: "always"
build: ./docker/php/
environment:
XDEBUG_CONFIG: remote_host=dbgpproxy
expose:
- "9000"
depends_on:
- mysql
volumes_from:
- app
composer:
restart: "no"
image: composer/composer:php7
command: install
volumes:
- .:/app
dbgpproxy:
restart: "always"
image: christianbladescb/dbgpproxy
expose:
- "9000"
ports:
- "9001:9001"
environment:
DOCKER_HOST: 10.0.75.1
mysql:
image: mysql:latest
volumes:
- mysqldata:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
depends_on:
- mysql
environment:
PMA_HOST: mysql
redis:
image: redis
ports:
- "6379:6379"
mailcatcher:
image: schickling/mailcatcher
restart: "always"
Docker network info:
[
{
"Name": "test_default",
"Id": "8f5b2e1188d65948d6a46977467b181e7fdb4b112a688ff87691b35c29da8970",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Containers": {
"05725540eca07666de250f2bb9ae856da69c0c325c4476150f214ba32a9b8714": {
"Name": "test_nginx_1",
"EndpointID": "723a820ea07e77cf976712293a911be3245e862477af6e0ecdcc1462536de6f5",
"MacAddress": "02:42:ac:12:00:08",
"IPv4Address": "172.18.0.8/16",
"IPv6Address": ""
},
"78085ebed911e767a9c006d909cb245e0392055d37550c6cfa3a618969bef821": {
"Name": "test_dbgpproxy_1",
"EndpointID": "2332e1a01a8c0ec7262d96829d7d8f3cb4c711b6e9033ab85a8dfdb57ae01382",
"MacAddress": "02:42:ac:12:00:0a",
"IPv4Address": "172.18.0.10/16",
"IPv6Address": ""
},
"7e12ea0a3a9b90360be6c15222fd052fbf02065aa18b8a3b12d19779bef4b41b": {
"Name": "test_phpmyadmin_1",
"EndpointID": "456a6508b6a507e01584beaf54eec9605db449261749065a562a6fb62111bb9c",
"MacAddress": "02:42:ac:12:00:05",
"IPv4Address": "172.18.0.5/16",
"IPv6Address": ""
},
"81043a642cd9932e16bc51ba4604f6057d82e2c05f6e7378a85adfaa2de87f28": {
"Name": "test_app_1",
"EndpointID": "cfa41a5f210d4907747dcf7d516c6bdaecb817c993867a1e5f8e0250d33c927b",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"8b0cd7dc33fb783ae811f7ba15decd0165199da66242a10a33d8ee86c41bd664": {
"Name": "test_mailcatcher_1",
"EndpointID": "f2ed38e42dffd9565822a7ac248dcb022a47c8a78b05e93793b62d7188d0823c",
"MacAddress": "02:42:ac:12:00:06",
"IPv4Address": "172.18.0.6/16",
"IPv6Address": ""
},
"d552bf1ab3914220b8fbf9961cc3801acbe180c6e945bd0b4c3bcf8588352a5d": {
"Name": "test_mysql_1",
"EndpointID": "6188cbeb49cf8afc2a7622bd6ef7fc7076ea91b909ec3efc1d9a1ed1d35d5790",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"ecc941fc337d727e3c118bf9112dee1552ef5db7c94b24706c7d03bc42ea6c0a": {
"Name": "test_redis_1",
"EndpointID": "3f4254982ed1be8354f514dd717993e02b4afdfad8d022f5f8daf0b919a852e1",
"MacAddress": "02:42:ac:12:00:07",
"IPv4Address": "172.18.0.7/16",
"IPv6Address": ""
},
"f15f53405205db7263013fbb1ef1272764ca16850a46097b23d3619cd3d37b20": {
"Name": "test_php_1",
"EndpointID": "5fe30610823cd5660bf62e7612007ff4eef0316cbdfd15dbc0e56cafa6a3aca7",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
That's because pydbgpproxy works similar to XDebug and it is trying to connect to the wrong IP address. The correct IP address pydbgpproxy should connect to is host.docker.internal.
Situation:
xDebug ---> pydbgpproxy -X-> Host
This is because pydbgpproxy received the wrong IP from Docker in the first place.
So I guess you have to hardcode the host.docker.internal IP into pydbgpproxy