docker 启动容器后nginx出现错误日志

1、build好镜像后,启动容器,容器内使用了supervisord启动多个服务,然后就出现以下错误

2019/03/09 17:33:58 [emerg] 81#0: still could not bind()
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)

dockerfile内容

ARG OS_VER=latest


FROM centos:$OS_VER
MAINTAINER zxd <zhenxidog@163.com>


#root用户密码
ARG ROOT_PASSWORD=123456
#php版本,因为php版本间配置文件模板不相同,此处的版本号只能为大于7.0以上版本
ARG PHP_VER=7.2.8
#nginx版本
ARG NGINX_VER=1.15.2
#xhprof版本
ARG XHPROF_VER=1.2
#hiredis版本
ARG HIREDIS_VER=0.13.3
#swoole版本
ARG SWOOLE_VER=4.0.4


#映射配置文件
ADD ./etc /usr/src/etc


#基础环境配置
RUN yum install vim wget git net-tools -y \
    && yum install epel-release -y \
    && yum update -y \
    && yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel \
        libpng libpng-devel curl curl-devel libicu libicu-devel libmcrypt  libmcrypt-devel freetype freetype-devel \
        libmcrypt libmcrypt-devel autoconf gcc-c++ gcc make automake cmake ncurses-devel bison bison-devel\
    && yum install vixie-cron crontabs -y \
    && yum install python-setuptools -y \
    && easy_install supervisor \
    && yum install openssh-server -y \
    && yum install libgearman-devel -y \
    && echo PermitRootLogin  yes >> /etc/ssh/sshd_config \
    && echo PasswordAuthentication yes >> /etc/ssh/sshd_config \
    && echo RSAAuthentication yes >> etc/ssh/sshd_config \
    && sed -i "s/UseDNS yes/UseDNS no/" /etc/ssh/sshd_config \
    && echo "root:$ROOT_PASSWORD" | chpasswd \
    && ssh-keygen -t dsa -f /etc/ssh/ssh_host_rsa_key \
    && ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key \
    && ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key \
    && yum clean all && rm -rf /var/cache/yum/*


#安装php
RUN cd /usr/src \
    && curl -o php.tar.gz http://php.net/get/php-${PHP_VER}.tar.gz/from/this/mirror -L \
    && mkdir php \
    && tar -xzvf php.tar.gz -C ./php --strip-components 1 \
    && cd php \
    && ./configure --prefix=/usr/local/php --with-config-file-path=/etc/php --enable-soap --enable-mbstring=all \
        --enable-sockets --enable-fpm --with-gd --with-freetype-dir=/usr/include/freetype2/freetype \
        --with-jpeg-dir=/usr/lib64 --with-zlib --with-iconv --enable-libxml --enable-xml  --enable-intl \
        --enable-zip --enable-pcntl --enable-bcmath --enable-maintainer-zts --with-curl --with-openssl \
        --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
    && make \
    && make install \
    && mkdir /etc/php \
    && cp /usr/src/php/php.ini-development /etc/php/php.ini \
    && echo $MYSQL_SOCK_DIR > /tmp/temp_mysql_sock_dir.txt && temp_mysql_sock_dir=$(sed "s/\//\\\\\//g" /tmp/temp_mysql_sock_dir.txt) && rm -rf /tmp/temp_mysql_sock_dir.txt \
    && sed -i "s/mysqli.default_socket =/mysqli.default_socket = ${temp_mysql_sock_dir}\/mysql.sock/" /etc/php/php.ini \
    && sed -i "s/pdo_mysql.default_socket=/pdo_mysql.default_socket = ${temp_mysql_sock_dir}\/mysql.sock/" /etc/php/php.ini \
    && cp /usr/src/php/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm \
    && chmod +x /etc/init.d/php-fpm \
    && cd /usr/local/php/etc \
    && cp php-fpm.conf.default php-fpm.conf \
    && sed -i "s/;daemonize = yes/daemonize = no/" php-fpm.conf \
    && cp ./php-fpm.d/www.conf.default ./php-fpm.d/www.conf \
    && sed -i "s/export PATH/PATH=\/usr\/local\/php\/bin:\$PATH\nexport PATH/" /etc/profile \
    && sed -i "s/export PATH/PATH=\/etc\/init.d:\$PATH\nexport PATH/" /etc/profile \
    && rm -rf /usr/src/php.tar.gz && rm -rf /usr/src/php \
    #php redis扩展
    && /usr/local/php/bin/pecl install redis && echo "extension=redis.so" >> /etc/php/php.ini \
    #php grpc扩展
    && /usr/local/php/bin/pecl install grpc && echo "extension=grpc.so" >> /etc/php/php.ini \
    #php protobuf扩展
    && /usr/local/php/bin/pecl install protobuf && echo "extension=protobuf.so" >> /etc/php/php.ini \
    #php swoole扩展
    && cd /usr/src && curl -o hiredis.tar.gz https://github.com/redis/hiredis/archive/v${HIREDIS_VER}.tar.gz -L && mkdir hiredis && tar -xzvf hiredis.tar.gz -C ./hiredis --strip-components 1 \
    && cd hiredis && make && make install && mkdir /usr/lib/hiredis && cp libhiredis.so /usr/lib/hiredis && mkdir /usr/include/hiredis && cp hiredis.h /usr/include/hiredis \
    && echo '/usr/local/lib' >> /etc/ld.so.conf && ldconfig \
    && /usr/local/php/bin/pecl download swoole-${SWOOLE_VER} && tar -zxvf swoole-${SWOOLE_VER}.tgz && cd swoole-${SWOOLE_VER} \
    && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config --enable-openssl --enable-async-redis && make && make install \
    && echo "extension=swoole.so" >> /etc/php/php.ini && rm -rf /usr/src/hiredis.tar.gz && rm -rf /usr/src/hiredis && rm -rf swoole-${SWOOLE_VER}.tgz && rm -rf swoole-${SWOOLE_VER} \
    #php xhprof扩展
    && cd /usr/src \
    && curl -o xhprof.tar.gz https://github.com/longxinH/xhprof/archive/v${XHPROF_VER}.tar.gz -L \
    && tar -xvf xhprof.tar.gz \
    && cd xhprof-${XHPROF_VER}/extension \
    && /usr/local/php/bin/phpize \
    && ./configure --with-php-config=/usr/local/php/bin/php-config --enable-xhprof && make && make install \
    && mkdir -p -m 777 /tmp/xhprof \
    && echo -e "[xhprof]\nextension = xhprof.so\nxhprof.output_dir = /tmp/xhprof" >> /etc/php/php.ini \
    && mkdir /var/tools \
    && cd /usr/src/xhprof-${XHPROF_VER} \
    && mv xhprof_html /var/tools/ \
    && mv xhprof_lib /usr/local/php/lib/php \
    && sed -i "s/dirname(__FILE__) . '\/..\/xhprof_lib'/'xhprof_lib'/" /var/tools/xhprof_html/index.php \
    && sed -i "s/dirname(__FILE__) . '\/..\/xhprof_lib'/'xhprof_lib'/" /var/tools/xhprof_html/callgraph.php \
    && sed -i "s/dirname(__FILE__) . '\/..\/xhprof_lib'/'xhprof_lib'/" /var/tools/xhprof_html/typeahead.php \
    && rm -rf /usr/src/xhprof-${XHPROF_VER} && rm -rf /usr/src/xhprof.tar.gz \
    #php gearman扩展
    && cd /usr/src \
    && wget https://github.com/wcgallego/pecl-gearman/archive/gearman-2.0.3.tar.gz && tar -zxvf gearman-2.0.3.tar.gz\
    && cd pecl-gearman-gearman-2.0.3 \
    && /usr/local/php/bin/phpize \
    && ./configure --with-php-config=/usr/local/php/bin/php-config \
    && make && make install \
    && echo "extension=gearman.so" >> /etc/php/php.ini && rm -rf /usr/src/pecl-gearman-gearman-2.0.3 && rm /usr/src/gearman-2.0.3.tar.gz

    #php xdebug
RUN /usr/local/php/bin/pecl install xdebug \
    && echo -e "[xdebug]\nzend_extension=/usr/local/php/lib/php/extensions/no-debug-zts-20170718/xdebug.so\nxdebug.enable=1\nxdebug.remote_enable=1\nxdebug.remote_connect_back=0\nxdebug.remote_host=192.168.31.135\nxdebug.remote_port=9000\nxdebug.profiler_enable=1\nxdebug.remote_log=/var/log/xdebug_remote.log\nxdebug.remote_autostart = 1" 
#RUN cd /usr/src \
#    && curl  'https://xdebug.org/files/xdebug-2.6.0.tgz' -o xdebug.tar.gz \
#    && mkdir xdebug \
#    && tar -xvzf xdebug.tar.gz -C xdebug --strip-components=1 \
#    && cd xdebug \
#    && /usr/local/php/bin/phpize \
#    && ./configure --with-php-config=/usr/local/php/bin/php-config --enable-xdebug \
#    && make \
#    && make install \
#    && rm -rf /usr/src/xdebug.tar.gz && rm -rf /usr/src/xdebug

#安装nginx
RUN cd /usr/src \
    && curl -o nginx.tar.gz http://nginx.org/download/nginx-${NGINX_VER}.tar.gz -L \
    && mkdir nginx && tar -xzvf nginx.tar.gz -C ./nginx --strip-components 1 \
    && cd nginx \
    && ./configure \
     --prefix=/usr/local/nginx \
     #--prefix=/data/www \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock \
        --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module \
        --with-http_gzip_static_module --http-client-body-temp-path=/tmp/nginx/client/ \
        --http-proxy-temp-path=/tmp/nginx/proxy/ \
        --http-fastcgi-temp-path=/tmp/nginx/fcgi/ \
        --with-pcre --with-http_dav_module \
    && make && make install \
    && useradd nginx \
    && mkdir -p -m 777 /tmp/nginx \
    && echo "#!/bin/sh" > /etc/init.d/nginx \
    && echo "#description: Nginx web server." >> /etc/init.d/nginx \
    && echo -e "case \$1 in \n\
            restart): \n\
                /usr/local/nginx/sbin/nginx -s reload \n\
                ;; \n\
            stop): \n\
                /usr/local/nginx/sbin/nginx -s stop \n\
                ;; \n\
            *): \n\
                /usr/local/nginx/sbin/nginx \n\
                ;; \n\
        esac \n" >> /etc/init.d/nginx \
    && chmod +x /etc/init.d/nginx \
    #&& sed -i "3a daemon off;" /etc/nginx/nginx.conf \
    #&& sed -i "s/index  index.html index.htm;/index  index.php index.html index.htm;/" /etc/nginx/nginx.conf \
    #&& sed -i "s/# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000/location ~ \.php\$ { \nfastcgi_pass 127.0.0.1:9000;\nfastcgi_index  index.php;\nfastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;\ninclude fastcgi_params;\n }/" /etc/nginx/nginx.conf \
    && echo "<?php phpinfo()?>" > /usr/local/nginx/html/index.php \
    && rm -rf /etc/nginx && cp -rf /usr/src/etc/nginx /etc/nginx \
    && mkdir -m 777 -p /var/log/nginx \
    && rm -rf /usr/src/nginx.tar.gz && rm -rf /usr/src/nginx


#安装必要的服务
#RUN cd /usr/src \
#    && /usr/local/php/bin/php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
#    && /usr/local/php/bin/php composer-setup.php  --install-dir=/usr/local/bin --filename=composer \
#    && rm -rf composer-setup.php \
#    && /usr/local/php/bin/php /usr/local/bin/composer config -g repo.packagist composer https://packagist.phpcomposer.com \
#    && /usr/local/php/bin/php /usr/local/bin/composer create-project -s dev erik-dubbelboer/php-redis-admin /var/tools/phpredisadmin -vvv \
#    && cd /var/tools/phpredisadmin && cp includes/config.sample.inc.php includes/config.inc.php \
#    && sed -i "s/=> 'local server'/=> 'feehi server'/" includes/config.inc.php \
#    && sed -i "s/\/\/'auth' => 'redispasswordhere'/'auth' => '${REDIS_PASS}'/" includes/config.inc.php \
#    && sed -i "s/'scansize' => 1000/'scansize' => 1000,\n'login' => array('admin' => array('password' => '${REDIS_PASS}')),/" includes/config.inc.php \
#    && rm -rf /root/.composer/cache/ \
#    && cd /usr/src \
#    && curl -o phpmyadmin.tar.gz https://files.phpmyadmin.net/phpMyAdmin/${PHPMYADMIN_VER}/phpMyAdmin-${PHPMYADMIN_VER}-all-languages.tar.gz \
#    && mkdir -p /var/tools/phpmyadmin \
#    && tar -xzvf phpmyadmin.tar.gz -C /var/tools/phpmyadmin --strip-components 1 \
#    && rm -rf /usr/src/phpmyadmin.tar.gz


#服务器基础设置
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' > /etc/timezonesource \
    && source /etc/profile \
    &&echo [supervisord] > /etc/supervisord.conf \
    && echo nodaemon=true >> /etc/supervisord.conf \
    && echo user=root >> /etc/supervisord.conf \
    \
    && echo [program:sshd] >> /etc/supervisord.conf \
    && echo command=/usr/sbin/sshd -D >> /etc/supervisord.conf \
    \
    && echo [program:nginx] >> /etc/supervisord.conf \
    && echo command=/usr/local/nginx/sbin/nginx >> /etc/supervisord.conf \
    \
    && echo [program:php-fpm] >> /etc/supervisord.conf \
    && echo command=/usr/local/php/sbin/php-fpm >> /etc/supervisord.conf \
    \
    && echo [program:crond] >> /etc/supervisord.conf \
    && echo command=/usr/sbin/crond -n >> /etc/supervisord.conf


EXPOSE 80
#EXPOSE 9001
#EXPOSE 9000
CMD ["/usr/bin/supervisord"]

nginx配置文件内容

#user  nobody;
worker_processes  1;
#daemon off;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 100m;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        root   html;
        index  index.html index.htm index.php;

        location /phpmyadmin {
            alias /var/tools/phpmyadmin;
            index index.php;
        }

        location ~ /phpmyadmin.+\.php.*$ {
            if ($fastcgi_script_name ~ /phpmyadmin/(.+\.php.*)$) {
                set $valid_fastcgi_script_name $1;
            }
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/tools/phpmyadmin/$valid_fastcgi_script_name;
            include  fastcgi_params;
        }

        location /phpredisadmin {
            alias /var/tools/phpredisadmin;
            index index.php;
        }

        location ~ /phpredisadmin.+\.php.*$ {
            if ($fastcgi_script_name ~ /phpredisadmin/(.+\.php.*)$) {
                set $valid_fastcgi_script_name $1;
            }
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/tools/phpredisadmin/$valid_fastcgi_script_name;
            include  fastcgi_params;
        }

        location /xhprof_html {
            alias /var/tools/xhprof_html;
            index index.php;
        }

        location ~ /xhprof_html.+\.php.*$ {
            if ($fastcgi_script_name ~ /xhprof_html/(.+\.php.*)$) {
                set $valid_fastcgi_script_name $1;
            }
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/tools/xhprof_html/$valid_fastcgi_script_name;
            include  fastcgi_params;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include conf.d/*.conf;
}

完整的dockerifle文件及配置文件在github
https://github.com/zhengxidon...

容器启动后,进去查看nginx日志发现nginx不断重启,然后就出现以上错误日志了。nginx配置文件daemon off;注释去掉后,再重新启动容器就不会出现上面的错误,这是为什么呢?

阅读 7k
1 个回答

supervisor 监控nginx 一直在重启的问题,启动容器后日志一直输出以下错误

2019/03/09 17:33:58 [emerg] 81#0: still could not bind()
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/03/09 17:34:01 [emerg] 82#0: bind() to 0.0.0.0:80 failed (98: Address already in use)

supervisor 监控nginx ,写好配置文件之后,发现一直在重启,排查之后发现是命令不对:

command = /usr/local/bin/nginx 这个命令默认是后台启动,但是supervisor不能监控后台程序,所以supervisor就一直执行这个命令。

命令加上-g 'daemon off;'这个参数可解决这问题,这个参数的意思是在前台运行。

command = /usr/local/bin/nginx  -g 'daemon off;'

或直接在nginx.conf配置文件顶部加上

daemon off;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题