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
Related
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
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 have such simple dockerfile for PHP:
# Base image
FROM php:7-fpm
# Update packages list
RUN apt-get --yes update;
# Install SSH server, set root password and allow root login
RUN apt-get --yes install openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:123' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# Run SSH server
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
And such docker-compose.yml file
web:
image: nginx:latest
volumes:
- /c/Users/marcin/dock-test/composers/l1.app/html/:/usr/share/nginx/html/
- /c/Users/marcin/dock-test/composers/l1.app/nginx/conf.d/:/etc/nginx/conf.d/
- /c/Users/marcin/dock-test/composers/l1.app/nginx/log/:/var/log/nginx/
ports:
- "8080:80"
working_dir: /usr/share/nginx/html/
links:
- php
- db
container_name: l1.web
environment:
- VIRTUAL_HOST=l1.app
php:
build: ../builds
dockerfile: Dockerfile-php7-fpm
volumes:
- /c/Users/marcin/dock-test/composers/l1.app/html/:/usr/share/nginx/html/
- /c/Users/marcin/dock-test/composers/l1.app/php/config/:/usr/local/etc/php/
working_dir: /usr/share/nginx/html/
links:
- db
container_name: l1.php
ports:
- "22020:22"
db:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=pass
- MYSQL_DATABASE=
- MYSQL_USER=
- MYSQL_PASSWORD=
expose:
- 3306
volumes:
- /c/Users/marcin/dock-test/composers/l1.app/mysql/data/:/var/lib/mysql/
- /c/Users/marcin/dock-test/composers/l1.app/mysql/conf.d/:/etc/mysql/conf.d/
- /c/Users/marcin/dock-test/composers/l1.app/mysql/log/:/var/log/mysql/
ports:
- "33060:3306"
container_name: l1.db
The problem - everything is working fine until I add in my dockerfile the last shown line:
CMD ["/usr/sbin/sshd", "-D"]
If I add this line, SSH is working fine but I'm getting 404 when displaying the page. When I comment this line, I'm getting page without a problem but obviously this SSH is not working.
What could be the problem with this? I just want to add I need this SSH service in PHP container (and running docker exec in this case is not an option)
The base image php-fpm ends with
CMD ["php-fpm"]
Your own CMD would override that (meaning php .
One workaround would be at least to ADD and call a wrapper script which would:
call php-fpm
launch sshd daemon
But that wouldn't play well with stop/kill signals, which would not stop everything. There are special images for managing more than one main process.
The OP Marcin Nabiałek confirms in the comments below:
I've created such file:
#!/bin/sh
# Start PHP
php-fpm -D
# Start SSH
/usr/sbin/sshd -D
and it seems to be working now without a problem.
A complete answer from #VonC and #Marcin Nabialek.
# php-fpm -D
# /usr/sbin/sshd -D
# use \n to make the content into multiple lines
RUN printf "php-fpm -D\n/usr/sbin/sshd -D" >> /start.sh
RUN chmod +x /start.sh
CMD ["/start.sh"]