Search code examples
phpsshdockerdocker-composesshd

SSH in Docker container causes HTTP 404


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)


Solution

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