Get Host of linked container - docker

I have the following setup:
services:
web: &web
ports:
- "3000:3000"
env_file:
- .env.web
links:
- google_service
google_service:
command: bundle exec rails s -b 0.0.0.0 -p 3001
ports:
- "3001:3001"
environment:
RAILS_ENV: development
When I run docker-compose run --publish 3000:3000 web then I can access lvh.me:3001 in my browser.
But when in the container web I try to access this url I get Errno::ECONNREFUSED (Failed to open TCP connection to test.lvh.me:3001 (Connection refused - connect(2) for "127.0.0.1" port 3001)):
How can I access port 3001 from container google_service in the container web? Thanks

As Suggested by Creek, the best way here to call google service container from web container is by addressing google_service:3001.
In networks created via docker compose, the containers know each other by the service names, no matter whether they are linked or not. By default, they are aware about each other.
In case you want to make it accessible via host, use the IP or DNS name of the host machine OR use network mode as host "https://docs.docker.com/compose/compose-file/#network_mode" in docker compose.
While using host network mode, localhost will be the host machine & not the container itself.

Related

Docker with rabbitmq networking

I have trouble understanding how docker port mapping works. I have a docker-compose file with a couple of containers, one of them is a rabbitmq service.
The docker-compose file is:
version: "3.9"
volumes:
test:
external: true
services:
rabbitmq3:
container_name: "rabbitmq"
image: rabbitmq:3.8-management-alpine
environment:
- RABBITMQ_DEFAULT_USER=myuser
- RABBITMQ_DEFAULT_PASS=mypassword
ports:
# AMQP protocol port
- '5671:5672'
# HTTP management UI
- '15671:15672'
So the container runs using docker compose up, no problem. But when I access the rabbitmq management plugin using container_ip:15671 or container_ip:15671, I don't get anything. But when I access it using 127.0.0.1:15672, I can access the management plugin.
It probably is a stupid question but how can I access the container service using localhost?
The port sematic is as such <HOST_PORT>:<CONTAINER_PORT>. So -p 15671:15672 implies that the container port 15672 is mapped to the port 15671 on your machine.
Based on your docker compose file, the ports 5671 and 15671 are exposed on your machine.
The management portal can be accessed using http://localhost:15671 and the rabbitmq service can be used using the http://localhost:5671.
The IP 127.0.0.1 is localhost.

How to expose redis-server port started using "webdis docker image" to host machine

I want to monitor redis running in webdis docker container.
I use telegraf which collects redis stats but, telegraf is installed on host machine and it cannot connect to redis as it is running inside docker on 6379 port.
I tried to map docker port 6379 on which redis is running inside docker with hosts 6379 port so telegraf can listen to redis metrices, but telegraf cannot listen as connection breaks.
when I use telnet on host, I get connection closed by foreign host error.
telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.
Also, I am able to connect to webdis port on host machine, which is running on port 7379 inside wedis container.
To start webdis I am using following command : "docker run -d -p 8080:7379 -p 6379:6379 webdis"
Further to debug, I checked that redis inside webdis container is running on interface 127.0.0.1:6379
I checked that it should be running on 0.0.0.0:6379 in-order for port mapping to work properly.
How can I run redis inside webdis image on 0.0.0.0:6379?
Is there any other way I can monitor redis server running inside webdis container?
I tried to start redis-server inside webdis container by binding it to 0.0.0.0 using redis.conf file, but it still binds with 127.0.0.1
To which docker image are you refering. Is it this one? https://github.com/anapsix/docker-webdis/
If yes, when checking the Dockerfile, it does not include redis itself but in docker-compose.yaml there is a redis service include. This one does not expose ports which you need to connect to redis from outside of the container.
You need to change redis service to the following:
...
redis:
image: 'anapsix/redis:latest'
ports:
- '6379:6379'
I have this problem recently ago and I solve it.
webdis.Dockerfile
FROM nicolas/webdis:0.1.19
EXPOSE 6379
EXPOSE 7379
RUN sed -i "s/127.0.0.1/0.0.0.0/g" /etc/redis.conf
docker-compose.yaml
version: "3.8"
services:
webdis:
build:
context: .
dockerfile: webdis.Dockerfile
image: webdis_with_redis_expose
container_name: webdis
restart: unless-stopped
ports:
- "6379:6379"
- "7379:7379"
then execute docker-compose up

Connecting to a dockerized REST JaxRS end point from within another container locally

I am attempting to connect to a rest end point of a JaxRS liferay portlet.
If I try and connect through postman using http://localhost:8078/engine-rest/process-definition
It works 200 okay.
I am attempting to connect to the same end point from within another docker container part of the same docker network, I have tried with localhost and I receive the error:
java.net.ConnectException: Connection refused (Connection refused)
I have also tried http://wasp-engine:8078, wasp-engine is the docker name of the container. Still receiving the same error.
Here are the two containers in my compose file:
wasp-engine:
image: in/digicor-engine:test
container_name: wasp-engine
ports:
- "8078:8080"
depends_on:
mysql:
condition: service_healthy
wasp:
image: in/wasp:local2
container_name: Wasp
volumes:
- liferay-document-library:/opt/liferay/data
environment:
- camundaEndPoint=http://wasp-engine:8078
ports:
- "8079:8080"
depends_on:
mysql:
condition: service_healthy
They are both connecting to the mysql fine which is part of the same docker network and referenced via:
jdbc.default.url=jdbc:mysql://mysql/liferay_test
tl;dr
Use http://wasp-engine:8080
The why
In your docker-compose the
ports: - "8078:8080"
field on wasp-engine will expose port 8080 of the docker container to your host computer on port 8078. This is what allows your postman to succeed in connecting to the container over localhost. However, once inside the docker container localhost refers to the docker container itself. This port forwarding no longer applies.
Using docker-compose you can use the name of the container to target the specific docker container. You mentioned you tried this with the URI http://wasp-engine:8078. When you access the container this way the original port is used not the forwarded port for the host machine. This means that the docker container should be targeting port 8080.
Putting it all together, the final URI should be http://wasp-engine:8080.

Is it possible to expose http ports with docker-compose?

I am running postgres and keycloak images on my local machine. Both of this images have exposed ports configured. But docker machine only exposing ports via TCP. So I was able to connect to the postgres via TCP, but was not able to connect to the keycloack's localhost:8080 via HTTP.
So is it possible to connect to the docker exposed ports via HTTP.
docker-compose.yml
postgres:
image: postgres:9.6.3
volumes:
- ./db/init:/docker-entrypoint-initdb.d
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
ports:
- 5432:5432
keycloak:
image: jboss/keycloak-postgres
environment:
- KEYCLOAK_LOGLEVEL=DEBUG
- POSTGRES_DATABASE=user-service
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
links:
- postgres:postgres
ports:
- "8080:8080"
- "9999:9990"
- "443:8443"
volumes:
- ./data:/data
HTTP is a protocol that rides on top of TCP, so if Docker exposes a port via TCP (as opposed to UDP, the other option), then you can connect to that port over HTTP, provided something that speaks HTTP is listening on that port inside the container.
So if you can't connect to http://localhost:8080 with the above compose file, that probably means that the keycloak service either doesn't have anything listening on port 8080 inside the container, or whatever's listening doesn't speak HTTP, or whatever's listening and speaks HTTP is refusing the connection for some other reason.
To completely rule out that the problem is with the port mapping or something on the host, hop inside the container (using docker exec) and try to connect to 8080 from inside; if you can't, then you've confirmed the problem is with whatever's running inside the container, not with anything Docker-related.
It appears that port exposing is about connecting to the docker machine via http://{docker-machine-ip}:{port} and to be able to connect to it with localhost one need to configure ports forwarding within Virtual machine.
After this configuration I was able to connect to all my running containers with http://localhost:{port}

Docker for Mac Host Networking

I'm using Docker for Mac. I have two containers.
1st: A PHP application that is attempting to connect to localhost:3306 to MySQL.
2nd: MySQL
When running with links, they are able to reach each other.
However, I would like to avoid changing any of the code in the PHP application (e.g. changing localhost to "mysql") and stay with using localhost.
Host networking seems to do the trick, the problem is, when I enable host networking I can't access the PHP application on port 80 on my host mac.
If I docker exec -it into the php application and curl localhost, i see the HTML, so it looks like the port is just not forwarding to the host machine?
this is an example for docker-compose
it runs mysql in one container and phpmyadmin in another
the containers are linked together
you can access the containers via your host machine on the ports
3316 and 8889
my_mysql:
image: mysql/mysql-server:latest
container_name: my_mysql
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_DATABASE=test
- MYSQL_USER=test
- MYSQL_PASSWORD=test
ports:
- 3316:3306
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: my_myadmin
links:
- my_mysql:my_mysql
environment:
- PMA_ARBITRARY=0
- PMA_HOST=my_mysql
ports:
- 8889:80
restart: always

Resources