SSH in Docker container causes HTTP 404 - php

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"]

Related

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

Docker use Multiple nginx server with single php instance

I have several websites using php, each website is on a different docker.
In normal case, out of docker, all the websites use the same php installation.
How can that be achieved with docker?
this is my current setup. my single nginx docker uses this docker instance. (php-fpm.dockerfile only installing php-mysql).
php:
env_file: ".env"
build :
context : .
dockerfile : php-fpm.dockerfile
volumes:
- "./server/www/:/var/www/:cached"
I want to add another nginx server using this php instance, is it possible?
this is my entire relevant yml file, nginx_server instance us using php, how can i tell the php instance to process nginx_site instance as well.
version: "3.5"
nginx_server:
image: nginx
env_file: ".env"
links:
- php
- mysql
ports:
- "8081:80"
restart:
always
volumes :
- "./server/nginx/default.conf.php.template:/etc/nginx/conf.d/default.conf.template"
- "./server/logs/:/var/log/nginx/:cached"
- "./server/www/:/var/www/:cached"
command : /bin/bash -c "envsubst '$$NGINX_HOST,$$NGINX_ROOT,$$NGINX_INDEX_FILE,$$NGINX_PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
environment:
- NGINX_HOST=$NGINX_SERVER_HOST
- NGINX_ROOT=$NGINX_SERVER_ROOT/public
- NGINX_INDEX_FILE=index.php
- NGINX_PORT=8081
nginx_site:
image: nginx
env_file: ".env"
links:
- php
- mysql
ports:
- "8082:80"
restart:
always
volumes :
- "./site/nginx/default.conf.php.template:/etc/nginx/conf.d/default.conf.template"
- "./site/logs/:/var/log/nginx/:cached"
- "./site/www/:/var/www/:cached"
command : /bin/bash -c "envsubst '$$NGINX_HOST,$$NGINX_ROOT,$$NGINX_INDEX_FILE,$$NGINX_PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
environment:
- NGINX_HOST=$NGINX_SITE_HOST
- NGINX_ROOT=/var/www/
- NGINX_INDEX_FILE=index.php
- NGINX_PORT=8082
php:
env_file: ".env"
build :
context : .
dockerfile : php-fpm.dockerfile
volumes:
- "./server/www/:/var/www/:cached"
thanks

How use php from another docker container

in my app i have separated docker containers for nginx, mysql, php and supervisor. But now i require set in supervisor a program which run php script. It`s possible call php from another container?
EDIT
Example:
When i run supervisor program test, then i see error: INFO spawnerr: can't find command 'php'. I know that php is not in the container supervisor, but how i call from container php? And i require same php as for application.
./app/test.php
<?php
echo "hello world";
docker-compose.yml
version: "2"
services:
nginx:
build: ./docker/nginx
ports:
- 8080:80
volumes:
- ./app:/var/www/html
links:
- php
- mysql
php:
build: ./docker/php
volumes:
- ./app:/var/www/html
ports:
- 9001:9001
mysql:
build: ./docker/mysql
ports:
- 3306:3306
volumes:
- ./data/mysql:/var/lib/mysql
supervisor:
build: ./docker/supervisor
volumes:
- ./app:/var/www/html
ports:
- 9000:9000
supervisor.conf
[program:test]
command = php /var/www/html/test.php
process_name = %(process_num)02d
numprocs = 1
autostart = false
autorestart = true
Please check this repo in github
I used angular, laravel and mongo
with 3 containers, for mongo, php-fpm and nginx to make proxi to api and angular.
Angular dose not use nodejs container because I build angular ng build and this out the build in the folder angular-dist.
The folder angular-src its the source code of angular
into folder laravel run the command composer install, if you use Linux use sudo chmod 777 -R laravel
you can see this
and the route http://localhost:8000/api/
and the route http://localhost:8000/api/v1.0

How to launch existing Wordpress app image with Docker not at default 80 port?

may be somebody know how to resolve my issue?
If I use my existing app with wp-content and existing db, every time I've get redirected to the localhost:80 port. How to launch it at another port, maybe 8000 for example?
I have a Wordpress app Dockerfile with next lines:
FROM wordpress:latest
COPY ./src /var/www/html
ENV WORDPRESS_DB_PASSWORD mypass
ENV WORDPRESS_DB_NAME mydb
ENV WORDPRESS_DB_HOST mysql:3306
MySQL Dockerfile with existing db dump:
FROM mariadb:10.1.20
COPY dump/dump.sql /docker-entrypoint-initdb.d
ENV MYSQL_ROOT_PASSWORD mypass
CMD ["mysqld"]
And docker-compose.yml with that:
version: '2'
services:
mysql:
build: mysql/
restart: always
volumes:
- db_data:/var/lib/mysql
wordpress:
depends_on:
- mysql
build: wpapp/
ports:
- 8000:80
restart: always
volumes:
db_data:
Thanks for help everyone!
Override the database siteurl and homeurl vales with these lines in you wp-config.php:
define('WP_HOME', 'http://localhost:8000/');
define('WP_SITEURL', 'http://localhost:8000/');
After many tests I can recap how to solve this issue.
We need to change option_value of 2 rows in db table wp_options with option_names siteurl and home to http://localhost:8000 in this case.
We need to trigger dockers build to mysql container with volume.
docker-compose down -v
optional for clean docker cache
docker rm $(docker ps -aq)
docker volume rm $(docker volume ls -q)
docker rmi $(docker images -q)
this is not optional ofcourse
docker-compose up -d --build
The main thing is to clean browser cache manually, because it invokes redirect faster then cache clean if we use CMD + R for example in Safari and it seems like nothing is working after changes.

Docker: Can't pick up config file for nginx on Windows 10

I have some configuration in docker-compose.yml file.
nginx:
image: nginx:latest
ports:
- 8083:80
links:
- php
volumes:
- /c/xampp/htdocs/Drukwerk/rePortal:/var/www/html
- /c/docker/reportal/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
php:
build: ./php/
expose:
- 9000
links:
- mysql
volumes:
- /c/xampp/htdocs/Drukwerk/rePortal:/var/www/html
mysql:
image: mysql:5.6
volumes:
- /var/lib/docker-data/reportal:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: reportal
MYSQL_USER: reportal
MYSQL_PASSWORD: reportal
And when I try to run docker-compose up -d I got this error message:
C:\docker\reportal>docker run --name reportalnginx -v /c/xampp/htdocs/Drukwerk/rePortal:/var/www/html:ro -v /nginx/default.conf:/etc/nginx/conf.d/default.conf:ro -P -d nginx
docker: Error response from daemon: Conflict. The name "/reportalnginx" is already in use by container 2e7427b8dd13515c9868cceb31e56b7cd10626feff7a561c1bc19eeaa1158f1c. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.
But when I remove line with config file path for nginx - it works normal.
Also I've been trying to use hint from this question:
Question Link
I was able to run nginx, but volumes wasn't connected.
How I can solve this kind of problem?
try to remove all containers
docker rm -f $(docker ps -a -q)
and run again. or check you nginx in system. maybe you can stop it.

Categories