无法从外部主机访问 Docker 部署的镜像

标签 docker go amazon-ec2 ansible

所以我有一个 Go 应用程序,我正在使用 Ansible 和 Jenkins 管道作为 Docker 镜像部署到 EC2 实例。我有一个图像,我可以使用 Postman 使用 IP 地址和端口号 8080 从主机外部访问它。当我部署新图像时,我无法使用 IP 地址和端口号获得响应。当我 ssh 进入服务器时,我可以使用 localhost 和端口号到达端点。如果我停止那个图像并开始第一个图像,我就可以再次到达它。

这些图像之间可能存在哪些差异,导致无法再通过 IP 地址访问端点?

我的 Docker 组合文件:

version: '2'
services:
  project-1:
    image: ...
    volumes:
      - /var/log:/var/log
    network_mode: host
    environment:
      - ...
    cpu_shares: 1236
    mem_limit: 2619m

我的 DockerFile:

FROM golang:1.10

WORKDIR /go/src/...
COPY . .

RUN go get -d -v ./...
RUN go install -v ./...

EXPOSE 8080-8080
CMD ./run.sh

我还尝试从 Docker Compose 文件中删除 network_mode 并将其替换为如下所示的端口,但没有成功:

ports:
      - "0.0.0.0:8080:8080"

最佳答案

确保容器将端口绑定(bind)到主机端口。在container networking您可以阅读以下文档:

By default, when you create a container, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host. Here are some examples.

您可以尝试使用上面解释的 -p 标志运行您的容器:

docker run -p 0.0.0.0:8080:80 your-container-image

关于无法从外部主机访问 Docker 部署的镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488723/

相关文章:

go - 如何使用 golang 微服务?

php - Amazon EC2 - 建立数据库连接时出错 - mysql 已死但子系统已锁定

java - 无法再连接到我的 sql docker 容器

elasticsearch - Docker 规模和 Elasticsearch

docker - git repo 的 docker 容器克隆中的 Azure DevOps/TFS $(SYSTEM.ACCESSTOKEN)

node.js - 未找到 sudo npm install 命令

php - 如何仅使用 MySQL 来保护 EC2 服务器的安全?

docker - Docker 中的 GStreamer = 覆盖不起作用

协程超时

go - 如何在 time.Format 中删除毫秒值中的点