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
Related
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.
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
Dockerfile
COPY php.ini /usr/local/etc/php/conf.d/
RUN apt-get update &&\
apt-get install --no-install-recommends --assume-yes --quiet ca-certificates curl git &&\
rm -rf /var/lib/apt/lists/*
RUN curl -Lsf 'https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz' | tar -C '/usr/local' -xvzf -
ENV PATH /usr/local/go/bin:$PATH
RUN go get github.com/mailhog/mhsendmail
RUN cp /root/go/bin/mhsendmail /usr/bin/mhsendmail
RUN echo 'sendmail_path = /usr/bin/mhsendmail --smtp-addr mailhog:1025' > /usr/local/etc/php/php.ini
docker-compose.yml
version: '3'
services:
GKapp:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: GKapp
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: GKapp
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
mailhog:
image: mailhog/mailhog:v1.0.0
ports:
- "1025:1025"
- "8025:8025"
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
Stuff I'm tyring as follows:-
docker exec -it GKapp bash
cat /usr/local/etc/php/php.ini
sendmail_path = /usr/bin/mhsendmail --smtp-addr mailhog:1025
/usr/bin/mhsendmail --help
Usage of /usr/bin/mhsendmail:
-f, --from string SMTP sender (default "www#2693eda79e6e")
-i, --long-i Ignored. This flag exists for sendmail compatibility. (default true)
-o, --long-o Ignored. This flag exists for sendmail compatibility. (default true)
-t, --long-t Ignored. This flag exists for sendmail compatibility. (default true)
--smtp-addr string SMTP server address (default "localhost:1025")
-v, --verbose Verbose mode (sends debug output to stderr)
/usr/bin/mhsendmail andy#mailhog.local <<EOF
From: Andy <kinsta#mailhog.local>
To: Test <test#mailhog.local>
Subject: Hello, Andy!
Hey there,
Missing you pig time.
Hogs & Kisses,
Andy
EOF
error sending mail
2022/08/31 12:52:42 dial tcp 127.0.0.1:1025: getsockopt: connection refused
So, I'm unable to send an email using mailhog from within the container and from a script on the local file-system also:-
email.php
<?php
$to = "test#mailhog.local";
$subject = "Hey, I’m Pi Hog Pi!";
$body = "Hello, MailHog!";
$headers = "From: pihogpi#kinsta.com" . "\r\n";
mail($to, $subject, $body, $headers);
echo 'email sent';
None of the above methods work? help.
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 ?
I'm trying to make a program, that are able to DNS Lookup a website, it works fine. Now i wan't to run it in docker, but when try to dns lookup a website, i don't get any output.
<title>DNS LOOKUP</title>
<header>
<p>Do a DNS Lookup on any website!</p>
</header>
<form method="post" action="?action">
<input type="text" name="Host" placeholder="Host" />
<input type="submit" value="DNS Lookup" />
</form>
<?php
if(isset($_GET['action']))
{
$host = $_POST['Host'];
$command = shell_exec("nslookup $host");
}
echo $command;
?>
Docker Compose file:
version: '2'
services:
lookup:
build: .
ports:
- '8888:80'
stdin_open: true
tty: true
volumes:
- ./source:/var/www/html
- ./logs:/var/log/apache2
Docker File:
FROM ubuntu:16.04
RUN apt update
RUN apt install -y \
apache2 \
php \
libapache2-mod-php \
dnsutils
RUN useradd -d /home/chal/ -m -s /bin/nologin chal
WORKDIR /home/chal
COPY source .
USER chal
ENTRYPOINT service apache2 start && /bin/bash