问题
遇到一个问题,我自己用Dockerfile
创建了一个nginx
和php-fpm
一体的镜像,但是使用docker run
加上一些参数可以正常启动,但是使用docker-compose
配置成服务却无法正常启动,这就很奇怪了,于是,Google了一番,找到了解决办法。
以下是我的Dockerfile
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| FROM alpine
LABEL MAINTAINER="<crazyjums@gmail.com>"
ADD nginx-1.18.0.tar.gz /tmp COPY nginx/nginx.conf /tmp/nginx.conf COPY start.sh /tmp/start.sh COPY nginx/include/phptest.conf /tmp/phptest.conf COPY nginx/include/default.conf /tmp/default.conf COPY nginx/html/php_nginx/ /tmp/php_nginx/
WORKDIR /tmp/nginx-1.18.0
ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \ && apk update && apk upgrade\ && apk add --no-cache --virtual .build-deps \ mlocate \ g++ \ pcre-dev \ zlib-dev \ make\ gcc\ libc-dev\ autoconf \ && apk add pcre \ libxml2-dev\ sqlite-dev \ && addgroup -S nginx\ && adduser -S -G nginx -s /sbin/nologin -h /usr/local/nginx nginx\ && ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx\ && make\ && make install\ && mkdir -p /data/nginx/logs /data/nginx/logs /usr/local/nginx/conf/include \ && mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.back\ && mv /tmp/nginx.conf /usr/local/nginx/conf/nginx.conf\ && mv /tmp/start.sh /start.sh\ && mv /tmp/phptest.conf /usr/local/nginx/conf/include/phptest.conf \ && mv /tmp/default.conf /usr/local/nginx/conf/include/default.conf \ && mv /tmp/php_nginx /usr/local/nginx/html/php_nginx \ && ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ \ && /usr/local/nginx/sbin/nginx -t
ADD php-7.4.27.tar.gz /tmp COPY php/php-fpm.conf /tmp/php-fpm.conf COPY php/php-fpm.d/www.conf /tmp/www.conf
WORKDIR /tmp/php-7.4.27
RUN ./configure --prefix=/usr/local/php --enable-fpm\ && make \ && make install \ && mv /tmp/php-fpm.conf /usr/local/php/etc/php-fpm.conf \ && mv /tmp/www.conf /usr/local/php/etc/php-fpm.d/www.conf \ && ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/ \ && rm -rf /tmp/* \ && apk del .build-deps
EXPOSE 80 443
ENTRYPOINT ["/bin/sh","/start.sh"]
|
docker-compose.yaml
:
1 2 3 4 5 6 7 8 9 10 11 12 13
| version: "3" services: nginx-php-fpm: image: mmm container_name: nginx-php-fpm-one ports: - 891:80 volumes: - ./nginx/include:/usr/local/nginx/conf/include - ./nginx/html/php_nginx:/usr/local/nginx/html/php_nginx - ./nginx/html/vue_nginx:/usr/local/nginx/html/vue_nginx - ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf - ./nginx/logs:/data/nginx/logs:rw
|
原因
找了半天才发现是我的配置文件少写了配置
原来:
Docker
镜像的缺省命令是bash,如果不加 -it
,bash
命令执行了自动会退出,加-it
后docker
命令会为容器分配一个伪终端,并接管其stdin/stdout
支持交互操作,这时候bash
命令不会自动退出
像不使用docker-compose
,我们会执行类似如下的命令
docker run -d -it mmm /bin/bash
但docker-compose
需要额外配置下
需要在docker-compose.yml
中包含以下行:
1 2
| stdin_open: true tty: true
|
第一个对应于docker run
中的 -i
,第二个对应于 -t
.
修改完的docker-compose.yaml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| version: "3" services: nginx-php-fpm: image: mmm container_name: nginx-php-fpm-one ports: - 891:80 stdin_open: true tty: true volumes: - ./nginx/include:/usr/local/nginx/conf/include - ./nginx/html/php_nginx:/usr/local/nginx/html/php_nginx - ./nginx/html/vue_nginx:/usr/local/nginx/html/vue_nginx - ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf - ./nginx/logs:/data/nginx/logs:rw
|
Reference
写在最后
欢迎大家关注鄙人的公众号【麦田里的守望者zhg】,让我们一起成长,谢谢。