[英]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.