问题
I have a running container:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3825fa57b1ae repo:0.1.0 "/app/gogs/docker/st…" 29 minutes ago Up 29 minutes 22/tcp, 3000/tcp gogs-repo
and tried to curl it from another container as follows:
docker run --rm curlimages/curl:7.69.1 -L -v http://gogs-repo:3000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Could not resolve host: gogs-repo
I've started the gogs-repo container as follows:
docker run --rm --name gogs-repo repo:0.1.0
usermod: no changes
Apr 26 10:37:56 syslogd started: BusyBox v1.31.1
2020/04/26 10:37:56 [TRACE] Log mode: File (Info)
Apr 26 10:37:57 sshd[36]: Server listening on :: port 22.
Apr 26 10:37:57 sshd[36]: Server listening on 0.0.0.0 port 22.
and as you can see, it is up and running. Why curl can the reach http://gogs-repo:3000?
回答1:
Assuming that your service is running on port 3000 inside container gogs-repo(because I am not sure after looking at the container logs), you should make the following changes in the existing setup.
First thing first. The two containers - gogs-repo and another container (from where you are trying to access the service) are not connected to each other. Hence the host gogs-repo is not resolved.
To fix this, you need to run both the containers in single network.
You can perform the following steps to achieve the desired result.
Create a private bridge network.
docker network create --driver bridge mynetworkNow start your application containers along with the
--network mynetworkadded to your docker run command.Finally, the updated docker run command looks like the following:
docker run --network mynetwork --rm --name gogs-repo repo:0.1.0 docker run --rm curlimages/curl:7.69.1 -L -v http://gogs-repo:3000
With the following changes, you are good to access http://gogs-repo:3000
Good read
Here is one more approach (easy but not recommended) to achieve the desired result.
Using Docker links
As mentioned here:
Links allow containers to discover each other and securely transfer information about one container to another container. When you set up a link, you create a conduit between a source container and a recipient container.
We can link the container from which we want to access the service using --link argument.
docker run --link gogs-repo:gogs-repo --rm curlimages/curl:7.69.1 -L -v http://gogs-repo:3000
What --link does is that it creates a secure tunnel between the containers that don’t need to expose any ports externally on the container.
来源:https://stackoverflow.com/questions/61439710/could-not-resolve-host-gogs-repo