Launch browser using puppeteer on docker container - node.js

I am trying to launch the browser using puppeteer on docker container.
However, when I am trying load the browser by hitting the API, I am seeing the following error
localhost:3000 is my client running locally. I am not sure if docker can access this address. I am thinking maybe this could be the reason for the connection failure. Please correct me if I am wrong.
When I try the above scenario without docker, it is working fine, I am able to see the puppeteer opening chromium browsers and show the page. To make it work on the docker container, what should I do?

The localhost on your host, where your application running on port 3000 is, is in a different namespace compared to the localhost for your puppeteer instance running in docker.
To fix this, you can either:
Make them be in the same namespace (Host Mode)
Create a bridge between them (Bridge Mode)
Host Mode
This puts your container in the same network namespace as the host. localhost will refer to the same thing inside and outside the container. Add --net-host to your docker run command
Bridge Mode
You can form a bridge between the container and host, by adding --add-host host.docker.internal:host-gateway to the docker run command, and changing puppeteer to use host.docker.internal:3000

Related

Unable to communicate between docker and external service

I have a docker container running in localhost:3000
Also, I have a node app running in localhost:8081
Now if I want to make post or get request from localhost:3000 to localhost:8001 its not working at all.
Now if run the service as a binary (not a docker file) on localhost:3000 the same API requests works.
How do I communicate if using docker?
Each container runs on it's own bridge network where localhost means the container itself.
To access the host, you can add the option --add-host=host.docker.internal:host-gateway to the docker run command. Then you can access the host using the host name host.docker.internal. I.e. if you have a REST service on the host, your URL would look something like http://host.docker.internal:8081/my/service/endpoint.
The name host.docker.internal is the common name to use for the host, but you can use any name you like.
You need docker version 20.10 for this to work.

"The connection was reset" after starting my server [duplicate]

I'm running a webpack-dev-server application inside a Docker container (node:4.2.1). If I try to connect to the server port from within the container - it works fine. However, trying to connect it from the host computer results in reset connection (the port is published, of course). How can I fix it?
This issue is not a docker problem.
Add --host=0.0.0.0 to your webpack command.
You need to connect to your page like this:
http://host:port/webpack-dev-server/index.html
Look to the iframe mode
You need to make sure:
you docker container has mapped the EXPOSE'd port to a host port
docker run -p x:y
your VM (if you are using docker machine with a VM) has forwarded that mapped port to the actual host (the host of the VM).
See "How to access tomcat running in docker container from browser?"

Exposing node Server running on docker doesn't work

I am running a angular app on node server and in server.js I have specified app.listen(8084,localhost)..So when i run npm start in the docker container and try to -p 8084:8084 in docker run I was not able to get anything, even though the curl command inside my container curl localhost:8084 was giving me right result.
So i change the app.listen(8084) and the -p 8084:8084 started working..I am not sure why ?
When you open socket, you need to bind it to some interface in your system. There are predefined values:
0.0.0.0 - all interfaces, your service will be available from any interface
locahost, 127.0.0.1 - bind locally. That means service is NOT available from oustide -- this is your case.
You also can specify particular interface IP address to bind to it.
When you start your container, by default docker start default bridge network, so your container is being put into separate network and to access it, you need to allow incoming remote connections in container.
You bind your service to localhost into a container, so no communication is possible outside the container. localhost for your node server is not the same than localhost for your container.

Connecting to a Docker container using domain name from the same physical location

I have a situation where node-container can reach mongo-container if it uses mongo-container's name such that Docker translates it to its internal IP (which I think is how it works). They are running at the same Docker host.
However, if developing locally (not on the Docker host server), we reach mongo by giving domain-name:port to the node-container. This configuration works fine both locally (with/without Docker) and at the server, but only without Docker (so npm start "directly").
The Docker containers at the host are connected to a Docker bridge network.
We would like to use the domain-name:port configuration everywhere ideally, so that we don't have to think about the Docker side of things.
I would like to understand what happens in terms of networking. My rough networking understanding thinks this happens:
WORKING SITUATION # SERVER BY SKIPPING DOCKER:
Host[npm start]-->Node[need to check <domain-name:port>]-->DNS server[this is the IP you need]-->Host[Yes my IP + that port leads to mongo-container].
BROKEN SITUATION # SERVER BY USING NODE BEHIND DOCKER:
Host[docker-compose up etc... npm start via node container]-->Node[need to check <domain-name:port>]-->DNS server[Problem here?].
Thanks for any insights.

Can't get docker to accept request over the internet

So, I'm trying to get Jenkins working inside of docker as an exercise to get experience using docker. I have a small linux server, running Ubuntu 14.04 in my house (computer I wasn't using for anything else), and have no issues getting the container to start up, and connect to Jenkins over my local network.
My issue comes in when I try to connect to it from outside of my local network. I have port 8080 forwarded to the serve with the container, and if I run a port checker it says the port is open. However, when I actually try and go to my-ip:8080, I will either get nothing if I started the container just with -p 8080:8080 or "Error: Invalid request or server failed. HTTP_Proxy" if I run it with -p 0.0.0.0:8080:8080.
I wanted to make sure it wasn't jenkins, so I tried getting just a simple hello world flask application to work, and had the exact same issue. Any recommendations? Do I need to add anything extra inside Ubuntu to get it to allow outside connections to go to my containers?
EDIT: I'm also just using the official Jenkins image from docker hub.
If you are running this:
docker run -p 8080:8080 jenkins
Then to connect to jenkins you will have to connect to (in essence you are doing port forwarding):
http://127.0.0.1:8080 or http://localhost:8080
If you are just running this:
docker run jenkins
You can connect to jenkins using the container's IP
http://<containers-ip>:8080
The Dockerfile when the Jenkins container is built already exposes port 8080
The Docker Site has a great amount of information on container networks.
https://docs.docker.com/articles/networking
"By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers."
You will need to provide special options when invoking docker run in order for containers to accept incoming connections.
Use the -P or --publish-all=true|false for containers to accept incoming connections.
The below should allow you to access it from another network:
docker run -P -p 8080:8080 jenkins
if you can connect to Jenkins over local network from a machine different than the one docker is running on but not from outside your local network, then the problem is not docker. In this case the problem is what ever machine who is receiving outside connection (normally your router, modem or ...) does not know to which machine the outside request should be forwarded.
You have to make sure you are forwarding the proper port on your external IP to proper port on the machine which is running Docker. This can be normally done on your internet modem/router.

Resources