当docker-compose的一部分时,自定义nginx容器立即退出 [英] Custom nginx container exits immediately when part of docker-compose
问题描述
我正在尝试通过简单的nginx容器设置来学习如何使用docker compose,该容器将请求重新路由到虚幻容器.我正在使用标准的鬼影图像,但是有一个自定义的nginx图像(继承自标准图像).
I'm trying to learn how to use docker compose with a simple setup of an nginx container that reroutes requests to a ghost container. I'm using the standard ghost image but have a custom nginx image (that inherits from the standard image).
当我使用"docker-compose up"运行合成文件时,它会立即退出,而"docker_nginx_1以代码0退出".但是,当我手动构建并运行它时,它运行良好,并且可以将浏览器导航到该容器并查看默认的nginx页面.我对撰写文件的误解是什么导致其行为与自定义构建有所不同?我该如何更改才能使其继续运行?
When I run the composition using "docker-compose up" it exits immediately with "docker_nginx_1 exited with code 0". However, when I build and run it manually, it runs fine and I can navigate my browser to the container and view the default nginx page. What am I misunderstanding about my compose file that causes it to behave differently than being custom built? What can I change to get it to stay running?
免责声明:我同时也在学习nginx,因此一次学习两件事可能会给我带来不必要的问题.
Disclaimer: I am also learning nginx as I go, so learning two things at once may be causing me undue problems.
原始文件稍微复杂一点,但是我将问题简化为:如果我对自定义映像使用build命令,该自定义映像除了从默认的nginx映像继承外什么也不做,因此会立即退出.如果我使用默认的nginx图像,它将起作用.这些是现在相关的文件:
The original files were a bit more complex, but I've reduced the issue to simply: If I use the build command for a custom image that does nothing but inherit from the default nginx image, it exits immediately. If I use the default nginx image, it works. These are the now relevant files:
撰写文件:
ghost:
expose:
- "2368"
image: ghost
nginx:
# image: nginx << If I use this instead of my custom build, it doesn't exit
build: ./nginx
ports:
- "80:80"
- "443:443"
links:
- ghost
nginx/Dockerfile:
nginx/Dockerfile:
FROM nginx
原始文件(具有与上面相同的撰写文件):
ORIGINAL FILES (with the same compose file as above):
nginx/Dockerfile:
nginx/Dockerfile:
FROM nginx
RUN rm /etc/nginx/nginx.conf
COPY conf/nginx.conf /etc/nginx/nginx.conf
COPY conf/sites-available/ghost /etc/nginx/sites-available/ghost
RUN mkdir /etc/nginx/sites-enabled
RUN ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost
EXPOSE 80 443
# Is this even the right command I have no idea
CMD service nginx start
nginx/conf/nginx.conf:
nginx/conf/nginx.conf:
daemon off;
user nginx;
# Let nginx figure out the processes I guess
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
nginx/conf/sites-available/ghost
nginx/conf/sites-available/ghost
server {
listen 80;
server_name 127.0.0.1;
access_log /var/log/nginx/localhost.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://0.0.0.0:2368;
proxy_redirect off;
}
}
正在编写:
plays-MacBook-Pro:docker play$ docker-compose up
Creating docker_ghost_1...
Creating docker_nginx_1...
Attaching to docker_ghost_1, docker_nginx_1
docker_nginx_1 exited with code 0
Gracefully stopping... (press Ctrl+C again to force)
Stopping docker_ghost_1... done
手动运行:
plays-MacBook-Pro:nginx play$ docker build --no-cache -t nginx_custom .
Sending build context to Docker daemon 8.704 kB
Step 0 : FROM nginx
---> 914c82c5a678
Step 1 : RUN rm /etc/nginx/nginx.conf
---> Running in 4ce9de96bb36
---> 98f97a9da4fc
Removing intermediate container 4ce9de96bb36
Step 2 : ADD conf/nginx.conf /etc/nginx/nginx.conf
---> dd3e089208a9
Removing intermediate container 36b9a47e0806
Step 3 : ADD conf/sites-available/ghost /etc/nginx/sites-available/ghost
---> 55fae53e5810
Removing intermediate container a82741d24af4
Step 4 : RUN mkdir /etc/nginx/sites-enabled
---> Running in 7659ead01b7b
---> 406be1c42394
Removing intermediate container 7659ead01b7b
Step 5 : RUN ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost
---> Running in e9658a08affa
---> 021a84216e8a
Removing intermediate container e9658a08affa
Step 6 : EXPOSE 80 443
---> Running in 230e4523794c
---> 23d85e1a04cb
Removing intermediate container 230e4523794c
Step 7 : CMD service nginx start
---> Running in 209e129cae21
---> d7004d6fa223
Removing intermediate container 209e129cae21
Successfully built d7004d6fa223
plays-MacBook-Pro:nginx play$ docker run -t nginx_custom
[It sits here on an empty line, running in the background]
推荐答案
只是遇到了同样的问题,最初的 fix 是要更改docker-compose.yml
中的服务名称.
Just ran into this same issue, and the initial fix was to change the name of the service in docker-compose.yml
.
这可行,但之所以可行的原因,是因为Docker-compose缓存了构建&将其与服务名称绑定.第一个之后的每个docker-compose up
都将使用之前构建的内容,因此您对Dockerfile
或docker-compose.yml
的该部分所做的任何更改基本上都将被忽略.
This worked, but the reason it worked is because Docker-compose caches the build & ties it to the service name. Every docker-compose up
after the first one just uses what it built before, so any changes you make to the Dockerfile
, or that section of the docker-compose.yml
are basically ignored.
当您(和我)更改服务名称时,由于该服务名称之前没有被标记过,因此它触发了新的构建.
When you (and I) changed the service name, it triggered a new build since that service name hasn't been tagged before.
真实解决方案是执行以下操作: docker-compose构建重建图像(后跟docker-compose up
).他们的文档并没有真正强调这个问题.
The real solution is to do a: docker-compose build to rebuild the image (followed by a docker-compose up
). Their documentation doesn't really emphasize this issue.
这篇关于当docker-compose的一部分时,自定义nginx容器立即退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!