I think this may be an XY problem, so I'll include the context of the question since I don't know the best way to solve this problem.
I have a kubernetes environment set up on AWS such that I have two parts, an nginx container, and a backend service (which I'll call SvcA). Since the backend service can come and go, in my nginx config I have something that looks like:
resolver kube-dns.kube-system.svc.cluster.local valid=60s ipv6=off;
server {
# stuff
location / {
set $backend "SvcA.default.svc.cluster.local:8000";
proxy_pass http://$backend;
}
}
This setup works well on kubernetes, but I want a way have having the (almost) exact same set up on my local machine for testing/development but without all the overhead of using kubernetes. What I want to do is stick these two containers (nginx, SvcA) into a docker-compose file and have it working that way. The problem I've run into, is that the resolver for the nginx is hard-coded to be the url on kubernetes, and the solution I think may work is to have container that is a dns, and it's sole entry is to point "SvcA.default.svc.cluster.local" to the name docker-compose assigns.
I'm unsure if this is the best way to solve the problem, and if it is, I don't know enough about DNS configuration to set this up. Is this the best solution to my problem, and if so, how would I configure a dns server to handle this?
i am not sure if you need a dns.
you can do something like this, even if the URL is hardcoded
eg docker-compose.yml
version: '2'
services:
service1:
...
networks:
back-tier:
ipv4_address: "172.16.238.10"
service2:
...
networks:
back-tier:
ipv4_adress: "172.16.238.11"
networks:
back-tier:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
gateway: 172.16.238.1
you can even set hostnames for each container, etc, you could also make a link between them. if you need it to be circular you can do it with extra_hosts parameter.
if should be able to find all the info you need over here:
https://docs.docker.com/compose/compose-file/
if the containers are on the same network they should be able to talk to each other
Related
I'd like to analize the traffic placing mimtproxy between the load balancer (traefik or nginx) and the service but I can't really understand how to do that. I don't want to set mitmproxy as ordinary proxy (that works like a charm) as I'd like to understand how the load balancers modify the requests.
I read the documentation on available mode of operation but I didn't recognize which situation suits me. I tend to exclude transparent mode (that I used on firewalls) and I don't really understand what is the --mode reverse:http://...: I thought it was a way to forward anything to the given address, so I tried it setting:
mitmweb:
image: mitmproxy/mitmproxy
tty: true
ports:
- "8080:8080" # proxy
- "8081:8081" # web-interface
command: mitmweb --web-host 0.0.0.0 --no-web-open-browser -p 8080 --mode reverse:http://django:8000/
...
but mitmproxy complains that
403: To protect against DNS rebinding, mitmweb can only be accessed by
IP at the moment. (https://github.com/mitmproxy/mitmproxy/issues/3234)
Is it any possible and how?
I misinterpreted the message that was not refusing to proxy. It just hinted me that I needed to access the web interface via an ip rather than a dns name.
The result is awesome. You get the traffic and can introspect the request/response cicle in a very useful way.
I'm currently trying to host two of my asp.net core services in docker. I am able to get them both up and running but the problem is that I don't know how to communicate between the two containers.
I'm using docker-compose to get my applications up and running and to allocate the ports on my host machine.
The urls to the services are placed in the appsettings.json. I think the problem lies here because I don't know where to get the right IP of the running containers.
I already tried to use the host network in both docker-compose files but I wasn't able to get that working.
I also tried to get the container's IP by using docker container inspect. but those IP's are unreachable.
docker-compose of service 1:
version: '3.4'
services:
leave.api:
build:
context: .
dockerfile: app1/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- 5002:5002
docker-compose of service 2:
version: '3.4'
services:
backoffice:
build:
context: .
dockerfile: BackOffice/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- 5001:5001
I hope to find a way to be able to communicate between the two services.
You could use external_links options, seems the most fit solution in your case:
https://docs.docker.com/compose/compose-file/#external_links
This explains bridging containers https://docs.docker.com/v17.09/engine/userguide/networking/default_network/container-communication/
If your communication is http based I would recommend having a http gateway that keeps track of your containers and their service endpoints.
You can communicate between two services in docker container using the name of service.
In your case, you have backoffice and leave.api services in your docker composed file.
So, you can access the backoffice service from leave.api using https://backoffice:5001/.
Thanks for all the answers, but I found another solution that works best for my situation.
I added the following line to both of my docker-compose files:
network_mode: bridge
This way both of my containers use the same network. After that I could communicate between the containes using my host ip address with the port the service is bound to. I placed this IP in the appsettings.json the same way as I had it before.
I am currently running a development stack using Docker-Compose in my company, to provide to developers everything they need to code our applications.
It includes in particular:
a Gitlab container (sameersbn/gitlab) to manage private GIT repositories,
a Jenkins container (library/jenkins) for building and continuous integration,
an Archiva container (ninjaben/archiva-docker) to manage Maven repositories.
In order to secure the services through HTTPS, and exposing them to the outside world, I installed the excellent nginx-proxy container (jwilder/nginx-proxy) which allows automated nginx proxy configuration using environment variables on containers, and automated HTTP to HTTPS redirection.
DNS are configured to map each public URL of dockerized services to the IP of the host.
Finally, using Docker-Compose, my docker-compose.yml file looks like this :
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- /var/config/nginx-proxy/certs:/etc/nginx/certs:ro
postgresql:
# Configuration of postgresql container ...
gitlab:
image: sameersbn/gitlab
ports:
- "10022:22"
volumes:
- /var/data/gitlab:/home/git/data
environment:
# Bunch of environment variables ...
- VIRTUAL_HOST=gitlab.my-domain.com
- VIRTUAL_PORT=80
- CERT_NAME=star.my-domain.com
archiva:
image: ninjaben/archiva-docker
volumes:
- /var/data/archiva:/var/archiva
environment:
- VIRTUAL_HOST=archiva.my-domain.com
- VIRTUAL_PORT=8080
- CERT_NAME=star.my-domain.com
jenkins:
image: jenkins
volumes:
- /var/data/jenkins:/var/jenkins_home
environment:
- VIRTUAL_HOST=jenkins.my-domain.com
- VIRTUAL_PORT=8080
- CERT_NAME=star.my-domain.com
For a developer workstation, everything works as expected. One can access the difference services through https://gitlab.my-domain.com, https://repo.my-domain.com and https://jenkins.my-domain.com.
The problem occurs when one of the dockerized service access another dockerized service. For instance, If I try to access https://archiva.my-domain.com from jenkins docker, I will get a timeout error from the proxy.
It seems that even if archiva.my-domain.com is resolved as the public host IP from the docker container, requests coming from dockerized services are not proxied by nginx-proxy.
As far as I understood, docker-nginx is handling requests coming from the host network, but does not care about the ones coming from the internal container network (_dockerconfig_default_ for a Docker-Compose stack).
You could say, why would I need to use the proxy from a container ? Of course, I could use URL http://archiva:8080 from Jenkins container, and it would work. But this kind of configuration is not scalable.
For example, using a Gradle build to compile one application, the build.gradle needs to declare my private repository through https://archiva.my-domain.com. It will work if build is launched from a developer workstation, but not through the jenkins container ...
Another example is an authentication in Jenkins by OAuth GitLab service, where the same URL GitLab authentication needs to be both available from the outside, and inside the Jenkins container.
My question here is then : How to configure nginx-proxy to proxy a request from a container to another container ?
I did not see any topic discussing this problem, and I do not understand enough the problem to build a solution on nginx configuration.
Any help would be really appreciated.
BMitch, the odds were good, it was indeed a iptables rules problem, and not a misconfiguration of nginx-proxy.
The default policy of chain INPUT for the table filter was DROP, and no rules was made to ACCEPT requests from the container IPs (127.20.X.X).
So for the record, I give some details of the situation if other people face the same problem.
To access containers from the outside world, Docker put rules on PREROUTING and FORWARD rules to allow external IPs to be DNATed from the host IP to the container IPs. Theses default rules allow any external IPs, and that is why limiting access to containers requires some advanced iptables customizations.
See this link for an example : http://rudijs.github.io/2015-07/docker-restricting-container-access-with-iptables/
But if your containers need to access host resources (services runing on the host, or in my case, a nginx-proxy container listening to HTTP/HTTPS host port and proxying to containers), you need to take care about the iptables rules of the INPUT chain.
In fact, a request coming from the container and addressed to the host will be routed to the host network stack by the Docker daemon, but will then need to pass the INPUT chain (as the request src IP is the host one). So if you want to protect host resources and let containers access them, do not remember to add something like this :
iptables -A INPUT -s 127.20.X.X/24 -j ACCEPT
Where 127.20.X.X/24 is the virtual network on which your containers are running.
Thank you a lot for your help.
I am new to docker and I have been dockerizing all my application in a single server. So far, everything is fine and working. However, I don't understand one thing. I am using docker-compose for everything (I haven't created dockerfile for my projects yet) and there is this ports attribute in docker-compose. If I write something like this:
ports:
8085:80
It will listen on 0.0.0.0:8085, which means outside world has access to my server. After some discussions and google-ing, I found that I can take an IP address in my docker bridge network and do port mappings easily:
ports:
172.17.0.1:8085:80
This will listen only on 172.17.0.1:8085, which is great as it is only listens on internally and my nginx proxies the traffic to the necessary ports. (e.g proxy_pass http://172.17.0.1:8085). After knowing more about docker and understanding how they work, I realized that all these containers have their own IP addresses with ports exposed only to those addresses. For example, one of my "web" containers has IPv4 address of 172.17.0.10 and port 80 is exposed. If I do docker inspect on one of these containers, I will see the IP address of the container.
Now, I want to use these IP addresses in my nginx. Instead of proxy_pass http://172.17.0.1:8085, I want to do http://172.17.0.10. I personally think that this is a much elegant interface but there is one thing that concerns me. What happens if I restart my machine? All the containers will start in some kind of order. If I have 5 web containers and they start in random order, can I be sure that the IPs for these containers will be the same? Or will they change? Should I always use ports in docker-compose for use by nginx? If yes, how can I have different IPs per container instead of different ports with the same IP? Will it be okay if I create another docker network interface (let's say in subnet 172.17.1.0), and assign different IPs from that interface to the exposed "public" ports? By this I mean basically using 172.17.1.1:80:80 in one container, 172.17.1.2:80:80 in another etc.
Not an expert in the docker-networking domain but I'll try my best to answer the questions you got there.
Q: What happens if I restart my machine? All the containers will start in some kind of order.
A: Unless you use the links or depends_on keywords, otherwise you cannot guarantee the start sequence.
Q: If I have 5 web containers and they start in random order, can I be sure that the IPs for these containers will be the same?
A: I did a little experiment on my machine by taking note of the ipaddresses of my existing 2 containers (postgresDB and influxDB).
They are running on
Postgres: 172.17.0.2
InfluxDB: 172.17.0.3
Shut it down and boot again. Probably due to them booting up the same order this time, the ip addresses seems to have been maintained. Added the depends_on keyword to force the InfluxDB container to start first before Postgres can, now the IP addresses of both containers are;
Postgres: 172.17.0.3
InfluxDB: 172.17.0.2
I think the IP is distributed based on a first come first serve basis. If you didn't specify an ordering for booting up the containers I think there is a small chance which the ip can be different for the containers. Really depends on who runs first.
Q: Should I always use ports in docker-compose for use by nginx?
A: Yes, if you would like to port forward an nginx instance's port to the outside world. Otherwise no one will be able to hit that web server. E.g. exposing port 443 to let HTTPs traffic come through.
Q: how can I have different IPs per container instead of different ports with the same IP?
A: I don't know whether that is possible or not but after doing some research for you on the docker-compose documentation it seems possible by using the ipam keyword.
See:
https://docs.docker.com/compose/compose-file/#/ipam
That looks scary to me so what I did for my own project was to use the service_name instead.
Example:
container_bbb:
image: banana
my_nginx:
image: apple
environment:
- MOUNT_SRC0=http://container_bbb:80
- MOUNT_DEST0=/
links:
- container_bbb
For this instance in the my_nginx container, the service name container_bbb would be resolved into the hostname of that container.
Then I will have a python script that would dynamically generate the ngix config using this information at the container's entrypoint script area.
Sounds a bit overkill but that gives me more control over what I want to do with my nginx.
So in my /etc/nginx/conf.d/default-locations/ the configs would be something like;
location /container_bbb/ {
proxy_pass http://container_bbb:3000/;
}
Note:
I am using this nginx server instance as a reverse proxy server.
I guess what I am trying to say here is that you can essentially use the hostname instead of the ip address. And to get to the container next-door you would pass in http://CONTAINER_SERVICE_NAME:PORT
You can't rely on the containers' IP addresses. If all your services are on the same docker-compose config, they will automatically be part of the same internal network and you can simply use the service names as the hostnames.
E.g., If your web app was named php, your nginx proxy config would look something like:
location / {
proxy_pass http://php;
proxy_set_header Host $host;
proxy_redirect off;
}
(Also note you might want to enable your firewall, just in case any of the port mappings leaks out to the public IP address of the host.)
I'm currently trying to run two containers on a single host, one being an application (Ruby on Rails) and the other Nginx as a reverse proxy and cache. The app is running on TCP port 80. What I want to be able to do is bring down my application container, remove it and then bring it up again without having to restart nginx. The problem is that Nginx only seems to look up the IP of the container once, so if it goes down then back up at a different address then Nginx will just complain that there's nothing there.
I've tried a few things:
Using resolver 127.0.0.11 valid=5 to use Docker's DNS
Using an upstream block
Using a variable to try to get nginx to resolve at runtime.
I'm not sure where else to look but none of these options work if the application is brought up on a different IP address. Is there something I'm missing making this impossible?
Thanks.
Ended up reading through the 12 factor app which inspired me to remove the Nginx proxying to Rails upstream altogether, and instead used it as a proxy cache which has an upstream of the external DNS name.