简体   繁体   English

docker 组成两个容器之间的通信:无法解析主机名 mysshserver:名称或服务未知

[英]docker compose communication between two containers: Could not resolve hostname mysshserver: Name or service not known

I'm having the following problem when trying to verify that my setup is working flawlessly: Could not resolve hostname mysshserver: Name or service not known我在尝试验证我的设置是否正常工作时遇到以下问题: Could not resolve hostname mysshserver: Name or service not known

appuser@54e703cf6eb6:/app$ ssh test@mysshserver
ssh: Could not resolve hostname mysshserver: Name or service not known

I'm starting the mysshserver service from the compose file up with docker compose up --build mysshserver -d我正在使用docker compose up --build mysshserver -d从撰写文件启动 mysshserver 服务

Docker compose file looks like the following: Docker 撰写文件如下所示:

version: '3.9'

services:

  mysshserver:
    image: mysshserver
    build:
      dockerfile: ssh/Dockerfile
    ports:
      - "22:22"

  myapp:
    image: myapp
    build:
      dockerfile: app/Dockerfile

ssh Dockerfile: ssh Dockerfile:

FROM ubuntu:latest

COPY shared/ssh-keys/id_rsa.pub /keys/

# Update package list and install [openssh-server, sudo]
RUN apt update && \
    apt install  openssh-server sudo -y && \
# Configure user test with password test
    useradd -rm -d /home/test -s /bin/bash -g root -G sudo -u 1000 test && \
    echo 'test:test' | chpasswd && \
# Add ssh key
    mkdir -p /home/test/.ssh/ && \
    chmod 0700 /home/test/.ssh  && \
    touch /home/test/.ssh/authorized_keys && \
    chmod 600 /home/test/.ssh/authorized_keys && \
    cat /keys/id_rsa.pub >> /home/test/.ssh/authorized_keys && \
# Start ssh
    service ssh start

EXPOSE 22

CMD ["/usr/sbin/sshd","-D"]

app Dockerfile:应用程序 Dockerfile:

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM ubuntu:latest

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

RUN apt update && \
    apt install -y openssh-client python3-pip python3 

# Install pip requirements
COPY app/requirements.txt .
RUN pip install -r requirements.txt

WORKDIR /app
COPY app /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser --home /home/appuser -u 5678 --disabled-password --gecos "" appuser && \
    chown -R appuser /app && \
    mkdir -p /home/appuser/.ssh

# Setup ssh public key
COPY shared/ssh-keys/id_rsa /home/appuser/.ssh

USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python3", "app.py"]

Docker inspect on the both containers looked also not wrong on the.network section: Docker 检查两个容器,在 .network 部分看起来也没有错:

        "Networks": {
            "codespaces-blank_default": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": [
                    "codespaces-blank-mysshserver-1",
                    "mysshserver",
                    "624f5d61df83"
                ],
                "NetworkID": "6d6278f57d215ab3d28fea84d44957e43d480b6b262e8d1f6323ff38becaa852",
                "EndpointID": "9160e6bc48689fdfb36bbdf614913121a0528d93a1dfe5a1e6d60e75fbb4082b",
                "Gateway": "172.26.0.1",
                "IPAddress": "172.26.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:1a:00:02",
                "DriverOpts": null
            }
        }

Can anybody help?有人可以帮忙吗?

Code is also shared on https://github.com/chrissssss/codespaces-blank代码也在https://github.com/chrissssss/codespaces-blank上共享

Best way to run would be docker compose run myapp bash最好的运行方式是docker compose run myapp bash

What gave the missing link wasn't included in my posting: I started the container to test with separately because it ended very quickly - this gave me another container of the image, but not connected to the.network, thus name resolution and so on wasn't working.我的帖子中没有包含缺少链接的原因:我启动了单独测试的容器,因为它很快结束了 - 这给了我另一个图像容器,但没有连接到.network,因此名称解析等等没有工作。

No chance for others to see that, sorry其他人没有机会看到,抱歉

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

相关问题 Docker 容器之间的组合请求无法解析主机 - Docker compose request between containers could not resolve host 使用Docker Compose在2个Docker容器之间进行Http通信 - Http Communication between 2 docker containers with Docker Compose 两个Docker容器之间的通信 - Communication between two Docker containers Docker 编写容器通信 - Docker compose containers communication 在 docker 组合中命名一个服务以用作主机名 - Name a service in docker compose to be used as a hostname Docker主机名在两个微服务之间进行通信 - Communication between two microservices by Docker hostname Docker 运行,在 docker-compose 构建后无法将主机名“db”转换为地址:名称或服务未知 - Docker run, after docker-compose build could not translate host name “db” to address: Name or service not known docker-compose 主机名在容器之间进行通信适用于 postgres 但不适用于应用程序 - docker-compose hostname to communicate between containers works with postgres but not app 使用 docker-compose 创建的 Flask 和 Bokeh 容器之间的通信失败并显示 ERR_NAME_NOT_RESOLVED - Communication between Flask and Bokeh containers created using docker-compose fails with ERR_NAME_NOT_RESOLVED 使用Java在两个容器之间进行Docker通信 - Docker communication between two containers with Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM