简体   繁体   English

运行接受来自主机的流量的 Docker 容器

[英]Running a Docker container that accept traffic from the host

I have the following config:我有以下配置:
Dockerfile文件

FROM centos  
MAINTAINER Eduar Tua <eduartua@gmail.com>  

RUN yum -y update && yum clean all
RUN yum -y install httpd && yum clean all
RUN echo "Apache works" >> /var/www/html/index.html

EXPOSE 80

ADD run-apache.sh /run-apache.sh
RUN chmod -v +x /run-apache.sh

CMD ["/run-apache.sh"]

The run-apache.sh script: run-apache.sh 脚本:

#!/bin/bash

rm -rf /run/httpd/* /tmp/httpd*

exec /usr/sbin/apachectl -D FOREGROUND

Then I build the image with:然后我用以下方法构建图像:

sudo docker build --rm -t platzi/httpd .  

then然后

sudo docker run -d -p 80:80 platzi/httpd

After that when I try to run the container accepting connections from the host in the 80 port I get this:之后,当我尝试运行容器以接受来自 80 端口中的主机的连接时,我得到以下信息:

67ed31b50133adc7c745308058af3a6586a34ca9ac53299d721449dfa4996657
FATA[0002] Error response from daemon: Cannot start container     67ed31b50133adc7c745308058af3a6586a34ca9ac53299d721449dfa4996657: Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use

Any help?有什么帮助吗?

It is saying port 80 is busy ... run this to see who is using port 80说 80 端口很忙……运行这个看看谁在使用 80 端口

sudo netstat -tlnp | grep 80 # sudo apt-get install net-tools # to install netstat

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1380/nginx -g daemo
tcp6       0      0 :::80                   :::*                    LISTEN      1380/nginx -g daemo

scroll to far right to see offending PID of process holding port 80 ... its PID 1380 so lets do a process list to see that pid滚动到最右边以查看持有端口 80 的进程的违规 PID ...其 PID 1380 所以让我们做一个进程列表来查看该 pid

ps -eaf | grep 1380

root      1380     1  0 11:33 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

so teardown that offending process to free up the port 80所以拆除那个有问题的过程以释放端口 80

sudo kill 1380  # if you know the pid ( 1380 for example )

__ or __ __ 或者 __

sudo fuser -k 80/tcp #  just kill whatever pid is using port 80 tcp

If after doing above its still saying busy then probably the process which you killed got auto relaunched in which case you need to kill off its watcher however you can walk up the process tree from netstat output to identify this parent process and kill that too如果在执行上述操作后仍然说忙,那么您杀死的进程可能会自动重新启动,在这种情况下,您需要杀死其观察者,但是您可以从 netstat 输出中走上进程树以识别该父进程并杀死它

Here is how to identify the parent pid of a given process pid以下是如何识别给定进程pid的父pid

ps -eafww

eve         2720    2718  0 07:56 ?        00:00:00 /usr/share/skypeforlinux/skypeforlinux --type=zygote

in above pid is 2720 and its parent will be the next column to right pid 2718 ... there are commands to show a process tree to visualize these relationships上面的 pid 是 2720,它的父级将是右 pid 2718 的下一列......有一些命令可以显示进程树来可视化这些关系

ps -x --forest  

or或者

pstree  -p

with sample output of样本输出为

systemd(1)─┬─ModemManager(887)─┬─{ModemManager}(902)
           │                   └─{ModemManager}(906)
           ├─NetworkManager(790)─┬─{NetworkManager}(872)
           │                     └─{NetworkManager}(877)
           ├─accounts-daemon(781)─┬─{accounts-daemon}(792)
           │                      └─{accounts-daemon}(878)
           ├─acpid(782)
           ├─avahi-daemon(785)───avahi-daemon(841)
           ├─colord(1471)─┬─{colord}(1472)
           │              └─{colord}(1475)
           ├─containerd(891)─┬─containerd-shim(1836)─┬─registry(1867)─┬─{registry}(1968)
           │                 │                       │                ├─{registry}(1969)
           │                 │                       │                ├─{registry}(1970)

The error seems pretty clear:错误似乎很清楚:

FATA[0002] Error response from daemon: Cannot start container 67ed31b50133adc7c745308058af3a6586a34ca9ac53299d721449dfa4996657: Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use FATA[0002] 来自守护进程的错误响应:无法启动容器 67ed31b50133adc7c745308058af3a6586a34ca9ac53299d721449dfa4996657:启动用户空间代理时出错:监听 tcp 0.0.0.0.0.0.0.0.0.0.0.0

It says, "address already in use".它说,“地址已在使用中”。 This means that something on your system -- probably a web server like Apache -- is already listening on port 80. You will either need to:这意味着您系统上的某些东西——可能是像 Apache 这样的 Web 服务器——已经在侦听端口 80。您要么需要:

  • stop the web server,停止网络服务器,
  • select a different host port in the -p argument to docker run ordocker run-p参数中选择不同的主机端口或
  • just drop the -p argument.只需删除-p参数。

Because Docker can't set up the requested port forwarding, it does not start the container.因为 Docker 无法设置请求的端口转发,所以它不会启动容器。

Options (a) and (b) will both allow the container to bind to port 80 on your host.选项 (a) 和 (b) 都将允许容器绑定到主机上的端口 80。 This is only necessary if you want to access the container from somewhere other than your host.仅当您想从主机以外的其他地方访问容器时才需要这样做。

Option (c) is useful if you only want to access the container from the docker host but do not want to otherwise expose the container on your local network.如果您只想从 docker 主机访问容器,但不想以其他方式在本地网络上公开容器,则选项 (c) 很有用。 In this case, you would use the container ip address as assigned by docker, which you can get by running docker inspect and perusing the output, or just running:在这种情况下,您将使用由 docker 分配的容器 IP 地址,您可以通过运行docker inspect并仔细阅读输出来获取,或者直接运行:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_id

If you are running Ubuntu, just run如果您正在运行 Ubuntu,只需运行

sudo /etc/init.d/apache2 stop

Then reload your Docker Image然后重新加载你的 Docker 镜像

docker reload

I found so solution:我找到了这样的解决方案:

$ docker stop container_name
$ docker commit container_name image_name
$ docker rm container_name

then u can create a new container from the image:然后你可以从图像创建一个新容器:

$ docker run -d -P --name container_name_the_same_or_new image_name

and now works.现在工作。

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

相关问题 配置apache2和主机以使用nginx将流量传递到docker容器 - Configure apache2 and host to pass traffic to a docker container with nginx 从另一个Docker容器(同一主机,Apache)调用Docker容器中的API - Calling an API in a Docker container from a different Docker container (same host, Apache) 创建Docker容器来托管网站 - creating docker container to host website 如何通过互联网访问我的Docker容器(笔记本) 我的主机正在Google Cloud上运行 - How to access my docker container (Notebook) over the Internet. My host is running on Google Cloud 防止apache2 / http2作为Docker容器的守护进程运行? - Prevent apache2 / http2 from running as daemon for Docker container? Docker容器虚拟主机SSL配置 - Docker Container Virtual Host SSL Configuration 通过Apache主机对Docker容器的代理请求 - Proxy request through Apache host to docker container Docker - 在不同网站的主机和容器上运行Apache - Docker - Run Apache on host and container for different websites 如何访问Docker容器中的主机MySQL服务器? - How to access host MySQL server in a Docker container? 从主机Web浏览器访问驻留在Docker容器内的apache2 - Accessing apache2 residing inside Docker container from Host machine web browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM