简体   繁体   English

Docker-compose 无法启动 apache 服务器

[英]Docker-compose can't start apache server

When i'm running sudo docker-compose up inside my dir, i get this error.当我在目录中运行sudo docker-compose up ,出现此错误。 I'm trying to make a container, that host a php website, where you can do whoami on it.我正在尝试制作一个容器,该容器托管一个 php 网站,您可以在其中执行 whoami。 Thanks谢谢

(13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:80
| no listening sockets available, shutting down
| AH00015: Unable to open logs

Dockerfile: Dockerfile:

FROM ubuntu:16.04

RUN apt update

RUN apt install -y apache2 php libapache2-mod-php 

RUN useradd -d /home/cp/ -m -s /bin/nologin cp

WORKDIR /home/cp

COPY source .

USER cp

ENTRYPOINT service apache2 start && /bin/bash

docker-compose.yml docker-compose.yml

version: '2'

services:
   filebrowser:
        build: .
        ports:
            - '8000:80'
        stdin_open: true
        tty: true
        volumes:
            - ./source:/var/www/html
            - ./logs:/var/log/apache2

There's a long-standing general rule in Unix-like operating systems that only the root user can open "low" ports 0-1023.在类 Unix 操作系统中有一个长期存在的一般规则,即只有 root 用户才能打开“低”端口 0-1023。 Since you're trying to run Apache on the default HTTP port 80, but you're running it as a non-root user, you're getting the "permission denied" error you see.由于您尝试在默认 HTTP 端口 80 上运行 Apache,但您以非 root 用户身份运行它,因此您会看到“权限被拒绝”错误。

The absolute easiest answer here is to use a prebuilt image that has PHP and Apache preinstalled.这里最简单的答案是使用预先安装了 PHP 和 Apache 的预构建映像。 The Docker Hub php image includes a variant of this. Docker Hub php镜像包含一个变体。 You can use a simpler Dockerfile:您可以使用更简单的 Dockerfile:

FROM php:7.4-apache
# Has Apache, mod-php preinstalled and a correct CMD already,
# so the only thing you need to do is
COPY source /var/www/html

# If you want to run as a non-root user, you can specify
RUN useradd -r -U cp
ENV APACHE_RUN_USER cp
ENV APACHE_RUN_GROUP cp

With the matching docker-compose.yml使用匹配docker-compose.yml

version: '3' # version 2 vs 3 doesn't really matter
services:
  filebrowser:
    build: .
    ports:
      - '8000:80'
    volumes:
      - ./logs:/var/log/apache2

If you want to build things up from scratch, the next easiest option would be the Apache User directive: have your container start as root (so it can bind to port 80) but then instruct Apache to switch to the unprivileged user once it's started up.如果你想从头开始构建,下一个最简单的选择是 Apache User指令:让你的容器以 root 身份启动(这样它可以绑定到端口 80),然后在启动后指示 Apache 切换到非特权用户. The standard php:...-apache image has an option to do this on its own which I've shown above.标准php:...-apache图像有一个选项可以自行执行此操作,我已在上面显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM