I am running 2 applications using separate Docker containers on the same server.
The first application uses the port 8000. Dockerfile:
EXPOSE 8000
docker-compose.yml:
command: uvicorn app.main:app --host 0.0.0.0 --port 8000
ports:
- "8000:8000"
The second application uses the port 8001. Dockerfile:
EXPOSE 8001
docker-compose.yml:
command: uvicorn app.main:app --host 0.0.0.0 --port 8001
ports:
- "8001:8001"
I have created DNS type A record:
api.mydomain.com to 123.123.123.123
api.mydomain.com:8000 and api.mydomain.com:8001 are both functioning. I want to route url path to port. For example:
api.mydomain.com/appone/ to api.mydomain.com:8000
api.mydomain.com/apptwo/ to api.mydomain.com:8001
Is it possible to do this using Docker? If yes, how can I do it?
Is it possible to do this using Docker?
Directly no, indirectly yes. Docker runs applications.
how can I do it?
You can run a proxy HTTP server that forwards domains to specific ports. Nginx and Apache are the most popular HTTP servers. There is also Fabio proxy.
You can run that server inside docker.
Related
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
I tried to deploy gRPC server and mongodb in docker. After that I trying to binding docker ports to my local ports. mongodb ports binding was working fine. But, gRPC server ports are not binding my local port
ports:
- "50051:50051"
like this i tried in docker-compose.yml
docker-compose.yml
services:
auth_server:
container_name: auth_service
build: .
command: go run server.go
volumes:
- .:/go/src/auth_server
working_dir: /go/src/auth_server
ports:
- "50051:50051"
environment:
PORT: 50051
In client gRPC file I used host and port like, 0.0.0.0:50051
conn, err := grpc.Dial("0.0.0.0:50051", grpc.WithInsecure())
but it was not working. I can't find any bug, so I assume I am doing something incorrectly.
You should use 127.0.0.1:50051 when connecting from a client on the host machine, or auth_server:50051 if you are connecting from docker-compose network.
If you're running this on windows I would check the "reserved port ranges" with command
netsh interface ipv4 show excludedportrange protocol=tcp
Also see this thread on github.
If it's linux check that nothing on the host is binding on that port.
[https://github.com/gtriggiano/ngrok-tunnel ] runs ngrok inside a container. Ngrok is required to run in the container to avert security risks. But am facing problems after running the scripts, which generates the url
$ docker pull gtriggiano/ngrok-tunnel
$ docker run -it -e "TARGET_HOST=localhost" -e "TARGET_PORT=3000" -p 4040 gtriggiano/ngrok-tunnel
am running my rails app on localhost:3000
is it my problem or can it be fixed by altering the scripts(inside the repo)?
I couldn't get this working but switched to https://github.com/shkoliar/docker-ngrok and it works brilliantly.
In my case I added it to my docker-compose.yml file:
ngrok:
image: shkoliar/ngrok:latest
ports:
- 4551:4551
links:
- web
environment:
- PARAMS=http -region=eu -authtoken=${NGROK_AUTH_TOKEN} localdev.docker:80
networks:
dev_net:
ipv4_address: 10.5.0.10
And it's started with everything else when I do docker-compose up -d
Then there's a web UI at http://localhost:4551/ for you to see the status, requests, the ngrok URLs, etc.
The Github page does have examples of running it manually from the command line too though, rather than via docker-compose:
Command-line Example The example below assumes that you have running
web server docker container named dev_web_1 with exposed port 80.
docker run --rm -it --link dev_web_1 shkoliar/ngrok ngrok http dev_web_1:80
With command line usage, ngrok session is active until it
won't be terminated by Ctrl+C combination.
No. if you execute -p with single number it's container port - host port is randomly assigned.
Using -p, --publish ip:[hostPort]:containerPort at docker run can specify the the host port with the container port.
as of now the 4040 of container is exposed. Not sure if your service listens by default on it.
To get localhost port execute
docker ps
you'll see the actual port it's not listening on.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1aaaeffe789d gtriggiano/ngrok-tunnel "npm start" About a minute ago Up About a minute 0.0.0.0:32768->4040/tcp wizardly_poincare
here it's listening on localhost:32768
this composer works for me. Note that in the entrypoint command for ngrok you have to reference the other service by name
version: '3'
services:
yourwebserver:
build:
context: ./
dockerfile: ...
target: ...
container_name: yourwebserver
volumes:
- ...
ports:
- ...
extra_hosts:
- 'host.docker.internal:host-gateway'
depends_on:
- ngrok
ngrok:
image: ngrok/ngrok:alpine
environment:
NGROK_AUTHTOKEN: '...'
command: 'http yourwebserver:80'
ports:
- '4040:4040'
expose:
- '4040'
I'm not sure if you have already solved this but when I was getting this error I could only solve it like this:
# docker-compose.yml
networks:
- development
I also needed to expose the 3000 port of my web container because it still wasn't exposed.
# docker.compose.yml
web:
expose:
- "3000"
My container for the server running on development is also under the development network. The only parameters, I believe, you should pass for the container to execute are image, ports, environment with DOMAIN and PORT for the server container, a link, and an expose on your web container:
# docker-compose.yml
ngrok:
image: shkoliar/ngrok
ports:
- 4551:4551
links:
- web
networks:
- development
environment:
- DOMAIN=squad_web
- PORT=3000
Actually to make ngrok work with your docker container you can install it outside of your project just like the manual on their website says. And then add
nginx:
labels:
- "traefik.http.routers.${PROJECT_NAME}_nginx.rule=Host(`${PROJECT_BASE_URL}`, `aaa-abc-xxx-140-177.eu.ngrok.io`)"
This particular example is for docker4drupal docker-compose file and traefik mapped as 80:80
I have Grafana hosted on Google Cloud Platform using docker - https://github.com/kamon-io/docker-grafana-graphite. I confirmed docker is running on GCE and as GCE only allows port 8080 upwards I change the Grafana port to 8080. I tried previewing using the console, and it returned
Error: Could not connect to Cloud Shell on port 8080.
Ensure your server is listening on port 8080 and try again.
This error does not pertain to this app alone but all the apps I have hosted on GCE, so I seeking a valid way to preview webapps on GCE.
This is the docker file docker-compose.yml
version: '2'
services:
grafana_graphite:
build: .
image: kamon/grafana_graphite
container_name: kamon-grafana-dashboard
ports:
- '8080:8080'
- '8181:8181'
- '8125:8125/udp'
- '8126:8126'
- '2003:2003'
volumes:
- ./data/whisper:/opt/graphite/storage/whisper
- ./data/grafana:/opt/grafana/data
- ./log/graphite:/opt/graphite/storage/log
- ./log/supervisor:/var/log/supervisor
The Grafana backend binds to port 3000 by default, even if you open the firewall on port 8080 it may not work. You have to use one of the following alternatives:
Redirect port 8080 to the Grafana port using:
$ sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to- port 3000
Put a webserver like Nginx or Apache in front of Grafana and have them proxy requests to Grafana.
Further information on Grafana configurations options can be found on this documentation link.
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.