Debug with XDebug, VSCode, and Docker - php

I'm trying to configure XDebug in the Docker from docker4drupal and VSCode, I get nothingh although I have follow this:
https://medium.com/#jasonterando/debugging-with-visual-studio-code-xdebug-and-docker-on-windows-b63a10b0dec
https://github.com/felixfbecker/vscode-php-debug
https://wodby.com/docs/stacks/drupal/local/#xdebug
This is my docker-compose.yml
php:
image: wodby/drupal-php:$PHP_TAG
container_name: "${PROJECT_NAME}_php"
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
# PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S opensmtpd:25
DB_HOST: $DB_HOST
DB_PORT: $DB_PORT
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
DB_NAME: $DB_NAME
DB_DRIVER: $DB_DRIVER
PHP_FPM_USER: wodby
PHP_FPM_GROUP: wodby
COLUMNS: 80 # Set 80 columns for docker exec -it.
# ## Read instructions at https://wodby.com/docs/stacks/php/local/#xdebug
PHP_XDEBUG: 1
PHP_XDEBUG_DEFAULT_ENABLE: 1
# # PHP_XDEBUG_REMOTE_CONNECT_BACK: 0
# # PHP_IDE_CONFIG: serverName=my-ide
PHP_XDEBUG_IDEKEY: "VSCODE"
PHP_XDEBUG_REMOTE_HOST: host.docker.internal # Docker 18.03+ Mac/Win
# # PHP_XDEBUG_REMOTE_HOST: 172.17.0.1 # Linux
# PHP_XDEBUG_REMOTE_HOST: 10.254.254.254 # macOS, Docker < 18.03
# PHP_XDEBUG_REMOTE_HOST: 10.0.75.1 # Windows, Docker < 18.03
# PHP_XDEBUG_REMOTE_LOG: /tmp/php-xdebug.log
## PHPUnit Drupal testing configurations
# SIMPLETEST_BASE_URL: "http://nginx"
# SIMPLETEST_DB: "${DB_DRIVER}://${DB_USER}:${DB_PASSWORD}#${DB_HOST}/${DB_NAME}#tests_"
# MINK_DRIVER_ARGS_WEBDRIVER: '["chrome", {"browserName":"chrome","goog:chromeOptions":{"args":["--disable-gpu","--headless"]}}, "http://chrome:9515"]'
volumes:
- ../drupal:/var/www/html
## For macOS users (https://wodby.com/docs/stacks/drupal/local#docker-for-mac)
# - ./:/var/www/html:cached # User-guided caching
# - docker-sync:/var/www/html # Docker-sync
## For XHProf and Xdebug profiler traces
# - files:/mnt/files
My launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"pathMappings": {
"/var/www/html": "/Users/oskar/Webapps/Docker/project/drupal",
"/var/www/html/web": "/Users/oskar/Webapps/Docker/project/drupal/web"
},
"xdebugSettings": {
"max_data": 65535,
"show_hidden": 1,
"max_children": 100,
"max_depth": 5
}
}
]
}
I have the launch.json in .vscode/launch.json
Also my structure is:
/Users/oskar/Webapps/Docker/project/drupal/
/Users/oskar/Webapps/Docker/project/docker/
When I try to run the debug I get nothing.

I know this is old but since I have it working I thought I would leave tracks for others.
Here's what I had to put into my Dockerfile for the php-apache container
# enable xdebug
RUN docker-php-ext-enable xdebug
# precreate log file for xdebug
RUN echo " " >> xdebug.log \
&& chown www-data:www-data xdebug.log \
&& chmod 774 xdebug.log \
# precreate directory for xdebug profiler
&& mkdir profiles \
&& chown www-data:www-data profiles \
&& chmod 774 profiles\
# precreate directory for xdebug tracer
&& mkdir traces \
&& chown www-data:www-data traces \
&& chmod 774 traces
# create and move xdebug.ini initialization file to start up dir
# Add Xdebug to PHP configuration
# See https://xdebug.org/docs/all_settings
RUN echo "" >> xdebug.ini \
&& echo "[xdebug]" >> xdebug.ini \
&& echo "zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so" >> xdebug.ini \
&& echo "xdebug.remote_enable = 1" >> xdebug.ini \
#profile setting
&& echo "xdebug.profiler_enable = 0" >> xdebug.ini \
# use url param XDEBUG_PROFILE nothing or secret found in profile_enable_trigger_value
&& echo "xdebug.profiler_enable_trigger = 1" >> xdebug.ini \
&& echo "xdebug.profiler_output_name = readxdebug.out.%t.pro" >> xdebug.ini \
&& echo "xdebug.profiler_output_dir = /usr/local/etc/php/profiles" >> xdebug.ini \
#trace setting
&& echo "xdebug.trace_enable_trigger = 1" >> xdebug.ini \
# use url param XDEBUG_TRACE nothing or secret found in trace_enable_trigger_value
&& echo "xdebug.trace_output_name = readtrace.%c" >> xdebug.ini \
&& echo "xdebug.trace_output_dir = /usr/local/etc/php/traces" >> xdebug.ini \
#
&& echo "xdebug.remote_autostart = 1" >> xdebug.ini \
&& echo "xdebug.remote_host = host.docker.internal" >> xdebug.ini\
&& echo "xdebug.default_enable=1" >> xdebug.ini\
&& echo "xdebug.remote_port=9000" >> xdebug.ini\
&& echo "xdebug.remote_connect_back=0" >> xdebug.ini\
&& echo "xdebug.idekey=VSCODE" >> xdebug.ini\
&& echo "xdebug.remote_log=/usr/local/etc/php/xdebug.log" >> xdebug.ini\
&& mv xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
I configured XDEBUG to trigger profiles using url param XDEBUG_PROFILE and traces
using url param XDEBUG_TRACE.
I also use the following commands to move the profile or trace output file to a mapped directory for a given project
docker exec -t docker_myprojapp_1 bash -c "mv /usr/local/etc/php/profiles/*.xt /var/
www/html/projectname/dev/profiles/"
docker exec -t docker_myprojapp_1 bash -c "mv /usr/local/etc/php/traces/*.xt /var/
www/html/projectname/dev/traces/"

Related

Map php web socket server inside container to the host

I have a docker-compose.yml file that looks like below
version: "3.9"
services:
php-env:
build: .
volumes:
# This is where php files outside will be located inside the container
- ./src:/var/www/html
ports:
- 9000:80
# mysql_db is also the server name when entering username and password to login to php my admin
mysql_db:
image: mysql:latest
# NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
# (this is just an example, not intended to be a production configuration)
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
# Mysql root password change below
MYSQL_ROOT_PASSWORD: root
# MYSQL_DATABASE: myDb
# MYSQL_USER: user
# mysql_db container name above is also the server name when entering username and password to login to php my admin
# default username is root
phpmyadmin:
image: phpmyadmin:latest
restart: always
ports:
# This is the local host port example: http://localhost:9001/
- 9001:80
environment:
- PMA_ARBITRARY=1
Below is my Docker file
FROM php:8.0-apache
# Simple libmaridb install
# RUN apt-get update -y && apt-get install -y libmariadb-dev
RUN apt-get update && \
apt-get install \
#libzip is for installing packages
libzip-dev \
wget \
grep \
curl \
git \
unzip \
libmariadb-dev \
-y --no-install-recommends
#Install php sockets
RUN docker-php-ext-install sockets
# Install mysqli
# RUN docker-php-ext-install mysqli
# Install pdo mysql
RUN docker-php-ext-install zip pdo_mysql
# copy composer dependency manager installer shell script
COPY ./install-composer.sh ./
# Copy php.ini file
COPY ./php.ini /usr/local/etc/php
# Composer, Git.... will all be installed in php-env container
# Clean up packages and install composer
RUN apt-get purge -y g++ \
&& apt-get autoremove -y \
&& rm -r /var/lib/apt/lists/* \
&& rm -rf /tmp/* \
&& sh ./install-composer.sh \
&& rm ./install-composer.sh
# Change the current working directory
# WORKDIR /var/www/
WORKDIR /var/www/html
What i am trying to create is a web socket server and a client where the websocket server writes the time and the client displays the time
Below is my websocket.php server file
<?php
$address = '0.0.0.0';
$port = 12345;
// Create WebSocket.
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($server, $address, $port);
socket_listen($server);
$client = socket_accept($server);
// Send WebSocket handshake headers.
$request = socket_read($client, 5000);
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $request, $matches);
$key = base64_encode(pack(
'H*',
sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
));
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers .= "Connection: Upgrade\r\n";
$headers .= "Sec-WebSocket-Version: 13\r\n";
$headers .= "Sec-WebSocket-Accept: $key\r\n\r\n";
socket_write($client, $headers, strlen($headers));
// Send messages into WebSocket in a loop.
while (true) {
sleep(1);
$content = 'Now: ' . time();
$response = chr(129) . chr(strlen($content)) . $content;
socket_write($client, $response);
}
?>
Below is my wesocket.html client file
<html>
<body>
<div id="root"></div>
<script>
var host = 'ws://0.0.0.0:12345/websocket.php';
var socket = new WebSocket(host);
socket.onmessage = function(e) {
document.getElementById('root').innerHTML = e.data;
};
</script>
</body>
</html>
Below is how i started my php server where i access the container via docker exec then i start the server
Below is how i am accessing my container
docker exec -it dockercomposefiles-php-env-1 bash
Below is how i am starting my server
php -q websocket.php
Below is how i am starting my client server
php -S 0.0.0.0:8000 websocket.html
Normally if i am using Xamp i should be able to access the client via http://0.0.0.0:8000/ but if i try to that in a browser its not working because its in the container and not in the host system how can i be able to Map this from the container to the host system so that i can be able to access via the host's system browser

Github Apache codespace cant find the requested path when forward port

I'm new to devcontainer and I can't figure out why my apache would forward to port 8000. I tried everything, but it can either not find the requested path, or I dont have permission. Which I tried add the user to www-data. Nothing works.
Docker-compose file:
I tried to change the path to the workspace, and I had to add the command to start apache.
version: "3.4"
services:
app:
build:
context: .
dockerfile: Dockerfile
args:
UID: 1001
restart: unless-stopped
ports:
- "8000:80"
volumes:
- ./:/var/www/html
#- workspace:/workspace
- /var/run/docker.sock:/var/run/docker.sock
command: /bin/sh -c "service apache2 start && while sleep 1000; do :; done"
environment:
- APACHE_RUN_USER=#1001
- APACHE_RUN_GROUP=#1001
devcontainer
// See https://aka.ms/vscode-remote/devcontainer.json for format details.
{
"name": "PHP",
"dockerComposeFile": ["docker-compose.yml"],
"service": "app",
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"applicationUrl": "http://*:8000",
"php.validate.executablePath": "/usr/local/bin/php",
"editor.tabSize": 4,
"workbench.colorTheme": "Quiet Light"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"xdebug.php-debug",
"bmewburn.vscode-intelephense-client",
"mrmlnc.vscode-apache"
]
}
},
"forwardPorts": [8000:80],
//"appPort": [80],
// Uncomment this like if you want to keep your containers running after VS Code shuts down.
// "shutdownAction": "none",
//"workspaceMount": "src=${localWorkspaceFolder},dst=/var/www/html,type=bind,consistency=cached",
//"workspaceFolder": "/var/www/html",
"postCreateCommand": "composer install",
"remoteUser": "php-apache",
"containerUser": "php-apache"
}
Dockerfile, as said I tried to add the user to www-data, I still think I need to do that, the command apache-foreground don't work properly
# [Choice] PHP version (use -bullseye variants on local arm64/Apple Silicon): 8-apache-bullseye, 8.1-apache-bullseye, 8.0-apache-bullseye, 7-apache-bullseye, 7.4-apache-bullseye, 8-apache-buster, 8.1-apache-buster, 8.0-apache-buster, 7-apache-buster, 7.4-apache-buster
ARG VARIANT=8.1-apache-bullseye
FROM php:${VARIANT}
ARG USERNAME=php-apache
ARG USER_UID=1001
ARG USER_GID=$USER_UID
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
&& apt-get update \
&& apt-get install -y sudo \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME
RUN apt-get update && apt-get install -y zip libzip-dev git inetutils-ping
RUN docker-php-ext-install \
zip
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY apache-vhost.conf /etc/apache2/sites-available/000-default.conf
RUN a2enmod rewrite headers
RUN a2dissite *
RUN a2ensite 000-default.conf
VOLUME [ "/var/www/html",]
EXPOSE 8000
CMD [ "apache2-foreground" ]
hostfile
<VirtualHost *:80>
DocumentRoot /var/www/html/public
# LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I appreciate any correction, I tried googling it, but there are few examples, and none of them only got me another error.

Laravel laradock Unable to connect localhost

After installing Docker in ubuntu and adding laradock to an existing project, I run this below command to start using laradock:
docker-compose up -d nginx mysql phpmyadmin workspace
result:
Creating laradock_mysql_1 ... done
Creating laradock_docker-in-docker_1 ... done
Creating laradock_workspace_1 ... done
Creating laradock_phpmyadmin_1 ... done
Creating laradock_php-fpm_1 ... done
Creating laradock_nginx_1 ... done
now after running docker-compose exec bash command I served laravel into that
docker-compose exec workspace bash
root#b3c88be3e389:/var/www# php artisan serve
INFO Server running on [http://127.0.0.1:8000].
Press Ctrl+C to stop the server
when I clicked on IP address, I get this message in browser:
Unable to connect
Firefox can’t establish a connection to the server at 127.0.0.1:8000.
The site could be temporarily unavailable or too busy. Try again in a few moments.
If you are unable to load any pages, check your computer’s network connection.
If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the web.
docker-compose ps command result:
Name Command State Ports
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1 dockerd-entrypoint.sh Up 2375/tcp, 2376/tcp
laradock_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp,:::3306->3306/tcp, 33060/tcp
laradock_nginx_1 /docker-entrypoint.sh /bin ... Up 0.0.0.0:443->443/tcp,:::443->443/tcp, 0.0.0.0:80->80/tcp,:::80->80/tcp, 0.0.0.0:81->81/tcp,:::81->81/tcp
laradock_php-fpm_1 docker-php-entrypoint php-fpm Up 9000/tcp
laradock_phpmyadmin_1 /docker-entrypoint.sh apac ... Up 0.0.0.0:8081->80/tcp,:::8081->80/tcp
laradock_workspace_1 /sbin/my_init Up 0.0.0.0:2222->22/tcp,:::2222->22/tcp, 0.0.0.0:3000->3000/tcp,:::3000->3000/tcp, 0.0.0.0:3001->3001/tcp,:::3001->3001/tcp,
0.0.0.0:4200->4200/tcp,:::4200->4200/tcp, 0.0.0.0:5173->5173/tcp,:::5173->5173/tcp, 0.0.0.0:8001->8000/tcp,:::8001->8000/tcp,
0.0.0.0:8080->8080/tcp,:::8080->8080/tcp
nginx definition into docker-compose.yml:
nginx:
build:
context: ./nginx
args:
- CHANGE_SOURCE=${CHANGE_SOURCE}
- PHP_UPSTREAM_CONTAINER=${NGINX_PHP_UPSTREAM_CONTAINER}
- PHP_UPSTREAM_PORT=${NGINX_PHP_UPSTREAM_PORT}
- http_proxy
- https_proxy
- no_proxy
volumes:
- ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
- ${NGINX_HOST_LOG_PATH}:/var/log/nginx
- ${NGINX_SITES_PATH}:/etc/nginx/sites-available
- ${NGINX_SSL_PATH}:/etc/nginx/ssl
ports:
- "${NGINX_HOST_HTTP_PORT}:80"
- "${NGINX_HOST_HTTPS_PORT}:443"
- "${VARNISH_BACKEND_PORT}:81"
depends_on:
- php-fpm
networks:
- frontend
- backend
and nginx Dockerfile:
FROM nginx:alpine
LABEL maintainer="Mahmoud Zalt <mahmoud#zalt.me>"
COPY nginx.conf /etc/nginx/
# If you're in China, or you need to change sources, will be set CHANGE_SOURCE to true in .env.
ARG CHANGE_SOURCE=false
RUN if [ ${CHANGE_SOURCE} = true ]; then \
# Change application source from dl-cdn.alpinelinux.org to aliyun source
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories \
;fi
RUN apk update \
&& apk upgrade \
&& apk --update add logrotate \
&& apk add --no-cache openssl \
&& apk add --no-cache bash
RUN apk add --no-cache curl
RUN set -x ; \
addgroup -g 82 -S www-data ; \
adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1
ARG PHP_UPSTREAM_CONTAINER=php-fpm
ARG PHP_UPSTREAM_PORT=9000
# Create 'messages' file used from 'logrotate'
RUN touch /var/log/messages
# Copy 'logrotate' config file
COPY logrotate/nginx /etc/logrotate.d/
# Set upstream conf and remove the default conf
RUN echo "upstream php-upstream { server ${PHP_UPSTREAM_CONTAINER}:${PHP_UPSTREAM_PORT}; }" > /etc/nginx/conf.d/upstream.conf \
&& rm /etc/nginx/conf.d/default.conf
ADD ./startup.sh /opt/startup.sh
RUN sed -i 's/\r//g' /opt/startup.sh
CMD ["/bin/bash", "/opt/startup.sh"]
EXPOSE 80 81 443
laradock env:
# All volumes driver
VOLUMES_DRIVER=local
# All Networks driver
NETWORKS_DRIVER=bridge

Docker, Nginx, localhost does not respond

I hope you're all doing well during this particular time.
I'm stuck on a problem concerning my nginx+php image, it's my first image I'm building from scratch (with Google), so I'm not sure everything is correct.
This image will serve me in multiple home projects, I'm using this image in a Gitlab Container Registry.
I'm using this Dockerfile :
FROM alpine:latest
COPY --from=library/docker:latest /usr/local/bin/docker /usr/bin/docker
COPY --from=docker/compose:latest /usr/local/bin/docker-compose /usr/bin/docker-compose
RUN adduser -S www-data -u 1000
RUN apk upgrade -U
RUN apk add --update --no-cache \
git \
bash \
vim
RUN apk --update --no-cache add php7 php7-fpm php7-mysqli php7-json php7-openssl php7-curl \
php7-zlib php7-xml php7-phar php7-intl php7-dom php7-xmlreader php7-ctype php7-session \
php7-mbstring php7-gd nginx curl
RUN apk add --update --no-cache nginx supervisor
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN composer global require "hirak/prestissimo:^0.3" --prefer-dist --no-progress --no-suggest --classmap-authoritative \
&& composer clear-cache
ENV PATH="${PATH}:/root/.composer/vendor/bin"
COPY config/nginx.conf /etc/nginx/nginx.conf
RUN mkdir -p var/cache var/log var/sessions \
&& chown -R www-data var
VOLUME /srv/api/var
VOLUME /var/www/html
VOLUME /var/www/src
COPY index.php /var/www/html/
COPY src/run.sh /run.sh
RUN chmod u+rwx /run.sh
EXPOSE 80
ENTRYPOINT [ "/run.sh" ]
CMD ["init"]
Which calls this bash script, for Symfony migration:
#!/bin/bash
INIT=false
MIGRATION=false
CREATEDB=true
for DOCKER_OPTION in $#
do
case "$DOCKER_OPTION" in
init )
INIT=true
shift
;;
migration )
MIGRATION=true
shift
;;
with-existing-db )
CREATEDB=false
shift
;;
* )
break
;;
esac
done
APP_ENV=${APP_ENV:-dev}
init_project () {
composer install --prefer-dist --no-progress --no-suggest --no-interaction
chmod +x bin/console && sync
php bin/console assets:install
if $CREATEDB
then
php bin/console doctrine:schema:update -f
php bin/console doctrine:fixtures:load -n
fi
}
if $MIGRATION;
then
php bin/console doctrine:migrations:migrate --no-interaction -vvv
chown -R www-data:www-data /srv/api/var/
fi
if $INIT;
then
mkdir -p /var/nginx/client_body_temp
chown www-data:www-data /var/nginx/client_body_temp
mkdir -p /var/run/php/
chown www-data:www-data /var/run/php/
touch /var/log/php-fpm.log
chown www-data:www-data /var/log/php-fpm.log
if [ "$APP_ENV" != 'prod' ];
then
init_project
fi
exec supervisord --nodaemon --configuration="/etc/supervisord.conf" --loglevel=info
fi
exec "$#";
Launched by this Docker-compose:
version: "3.7"
services:
lamp:
build:
context: .
dockerfile: ./Dockerfile
ports:
- 80:80
volumes:
- .:/var/www/html/
- ./src:/var/www/src/
With this Nginx configuration:
user www-data;
worker_processes 4;
error_log /dev/stdout;
pid /tmp/nginx.pid;
events {
worker_connections 1024;
}
http {
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
image/svg+xml svg;
}
client_body_temp_path /tmp/client_body;
fastcgi_temp_path /tmp/fastcgi_temp;
proxy_temp_path /tmp/proxy_temp;
scgi_temp_path /tmp/scgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
# mime types
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
access_log /dev/stdout;
error_log /dev/stdout;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
Everything seems to be going well.
Unfortunately I can't display my index.php (php_info).
When I do
curl -vvv http://localhost:80
* Trying ::1:80...
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.69.1
> Accept: */*
>
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer
And When I do
curl -vvv http://localhost:80 --trace-ascii dump.txt
== sync, corrected by elderman ==
== Info: Connected to localhost (::1) port 80 (#0)
=> Send header, 73 bytes (0x49)
0000: GET / HTTP/1.1
0010: Host: localhost
0021: User-Agent: curl/7.69.1
003a: Accept: */*
0047:
== Info: Empty reply from server
== Info: Connection #0 to host localhost left intact
I've searched the net, but my skills are quite limited in the configuration of nginx.
Do you have any ideas to submit to me.
thank you for your reply,
For VIM, he's already add here :
RUN apk add --update --no-cache \
git \
bash \
vim
So i try with this Dockerfile, whitout PHP/Composer (It's pretty minimalist?) :
FROM alpine:latest
COPY --from=library/docker:latest /usr/local/bin/docker /usr/bin/docker
COPY --from=docker/compose:latest /usr/local/bin/docker-compose /usr/bin/docker-compose
RUN adduser -S www-data -u 1000
RUN apk upgrade -U
RUN apk add --update --no-cache \
git \
bash \
vim
RUN apk --update --no-cache add nginx curl
RUN apk add --update --no-cache nginx supervisor
COPY config/nginx.conf /etc/nginx/nginx.conf
RUN mkdir -p var/cache var/log var/sessions \
&& chown -R www-data var
VOLUME /srv/api/var
VOLUME /var/www/html
VOLUME /var/www/src
COPY index.php /var/www/html/
COPY src/run.sh /run.sh
RUN chmod u+rwx /run.sh
EXPOSE 80
ENTRYPOINT [ "/run.sh" ]
CMD ["init"]
For
curl -vv http://localhost:80
* Trying ::1:80...
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.69.1
> Accept: */*
>
* Recv failure: Connection reset by peer
* Closing connection 0
And for
curl -vv http://localhost:80 --trace-ascii dump.txt
== Info: Trying ::1:80...
== Info: Connected to localhost (::1) port 80 (#0)
=> Send header, 73 bytes (0x49)
0000: GET / HTTP/1.1
0010: Host: localhost
0021: User-Agent: curl/7.69.1
003a: Accept: */*
0047:
== Info: Recv failure: Connection reset by peer
== Info: Closing connection 0
What do you think ?

Nginx pointed to wrong directory with Docker on Windows

I'm setting up a Laravel application with Docker, using a Docker image configuration I found here: https://blog.pusher.com/docker-for-development-laravel-php/
Now, this works fine on my Ubuntu machine (16.04), but on Window (10 Pro) I get a weird error. It first complains about not finding a composer.json file. Then, with each request I make to localhost:8000, I get the following error:
15#15: *1 open() "/var/www/public404" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8000"
I am very new to this, but it seems that nginx points to /var/www/public404 - I have no idea how that "404" got there. I have a feeling it has to do with the line try_files $uri = 404; in the site.conf file, however, I don't really know how that works and I don't want to break it... The weird thing is that this works with Ubuntu, but not on Windows (or maybe that's not weird at all?).
I use docker build . -t my-image to build the image and docker run -p 8000:80 --name="my-container" my-image to run a container using the image.
The EOL of all the config files is set to line feed. Does anybody have any idea how I might fix this?
Dockerfile
FROM nginx:mainline-alpine
LABEL maintainer="John Doe <john#doe>"
COPY start.sh /start.sh
COPY nginx.conf /etc/nginx/nginx.conf
COPY supervisord.conf /etc/supervisord.conf
COPY site.conf /etc/nginx/sites-available/default.conf
RUN apk add --update \
php7 \
php7-fpm \
php7-pdo \
php7-pdo_mysql \
php7-mcrypt \
php7-mbstring \
php7-xml \
php7-openssl \
php7-json \
php7-phar \
php7-zip \
php7-dom \
php7-session \
php7-tokenizer \
php7-zlib && \
php7 -r "copy('http://getcomposer.org/installer', 'composer-setup.php');" && \
php7 composer-setup.php --install-dir=/usr/bin --filename=composer && \
php7 -r "unlink('composer-setup.php');" && \
ln -s /etc/php7/php.ini /etc/php7/conf.d/php.ini
RUN apk add --update \
bash \
openssh-client \
supervisor
RUN mkdir -p /etc/nginx && \
mkdir -p /etc/nginx/sites-available && \
mkdir -p /etc/nginx/sites-enabled && \
mkdir -p /run/nginx && \
ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf && \
mkdir -p /var/log/supervisor && \
rm -Rf /var/www/* && \
chmod 755 /start.sh
RUN sed -i -e "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" \
-e "s/variables_order = \"GPCS\"/variables_order = \"EGPCS\"/g" \
/etc/php7/php.ini && \
sed -i -e "s/;daemonize\s*=\s*yes/daemonize = no/g" \
-e "s/;catch_workers_output\s*=\s*yes/catch_workers_output = yes/g" \
-e "s/user = nobody/user = nginx/g" \
-e "s/group = nobody/group = nginx/g" \
-e "s/;listen.mode = 0660/listen.mode = 0666/g" \
-e "s/;listen.owner = nobody/listen.owner = nginx/g" \
-e "s/;listen.group = nobody/listen.group = nginx/g" \
-e "s/listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm.sock/g" \
-e "s/^;clear_env = no$/clear_env = no/" \
/etc/php7/php-fpm.d/www.conf
EXPOSE 443 80
WORKDIR /var/www
CMD ["/start.sh"]
start.sh
#!/bin/bash
# ----------------------------------------------------------------------
# Create the .env file if it does not exist.
# ----------------------------------------------------------------------
if [[ ! -f "/var/www/.env" ]] && [[ -f "/var/www/.env.example" ]];
then
cp /var/www/.env.example /var/www/.env
fi
# ----------------------------------------------------------------------
# Run Composer
# ----------------------------------------------------------------------
if [[ ! -d "/var/www/vendor" ]];
then
cd /var/www
composer update
composer dump-autoload -o
fi
# ----------------------------------------------------------------------
# Start supervisord
# ----------------------------------------------------------------------
exec /usr/bin/supervisord -n -c /etc/supervisord.conf
site.conf
server {
listen 80;
root /var/www/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ /\. {
deny all;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/sites-enabled/*.conf;
}
supervisord.conf
[unix_http_server]
file=/dev/shm/supervisor.sock
[supervisord]
logfile=/tmp/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=warn
pidfile=/tmp/supervisord.pid
nodaemon=false
minfds=1024
minprocs=200
user=root
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///dev/shm/supervisor.sock
[program:php-fpm7]
command = /usr/sbin/php-fpm7 --nodaemonize --fpm-config /etc/php7/php-fpm.d/www.conf
autostart=true
autorestart=true
priority=5
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true
priority=10
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
As mentioned in the comments above, I just forgot to add the -v parameter in the docker run command, like so:
docker run -p 8000:80 -v $PWD/src:/var/www --name="my-container" my-image
... with $PWD/src being the full path to the src directory.

Categories