Is there a way to know which port is been published from within a docker container? - docker

I have a python-gunicorn web server that runs on docker. I want to run multiple servers on the same machine so I assigned a "random" port to each container like this:
$ docker run -d -p 0:80 image
If I run $ docker ps, I can see the port been used by the container:
0.0.0.0:32771->80/tcp
Now, I want to retrieve this number (32771) from within the container. Is there anyway to do this?
Edit:
I want this information because I need to connect to these servers from another machine and the way it is implemented requires that the server sends its url via HTTP Post: IP:Port/path

Related

Dockers call by http request

i'm new of docker's world. I'm trying to import two Dockers model downloaded from peltarion on Ubuntu version 20.04.
I download docker, setup the two images and create the first container with command
docker run -p 8000:8000 model-export
when i call xxx.xx.xx.xx:8000 with http request i receive answer by it.
My problem is that when i create the second container
docker run -p 8090:8090 model-export
I get the following configuration on the port
enter image description here
At this point i try to call with xxx.xx.xx.xx:8090, but no response.
How can i configure the container so i can call it with http request?
Thanks in advance to anyone who helps me
TLDR: your parameter values should be -p 8090:8000.
The order of -p/--publish parameter values should be:
published_port:container_exposed_port
published_port is a port used by host (your machine), where Docker is installed. And that can be used outside like a public-available service. For example it can be visible by other computers in your local network, or by Internet.
container_exposed_port is a port that is exposed from Docker container to Docker host by image (for example by EXPOSE 8000 instruction in image Dockerfile). It's visible only for your Docker host, or for other Docker containers (Docker networking) too.

How can I make a request coming from inside of a docker container appear to be coming from my local machine?

A browser running in docker container needs to make a POST to a login service running on a test API in our network. The service is very picky about where POST can come from so it's rejecting the POST because it's coming from host.docker.internal instead of localhost.company.com.
It's very unlikely I'd be able to get host.docker.internal added to the whitelist.
The POST will work fine if the browser is running on my local machine but fails when the browser is running inside a container on my local machine.
I've tried docker run --add-host='localhost.mycompany.com:127.0.0.1' and docker run --add-host='localhost:127.0.0.1', neither one worked. The latter seems silly; it was kind of a shot in the dark...
A possible further complication: the browser is running in testcafe inside a Docker container, so my request will have headers like 'Origin: http://172.17.0.2:1337' 'Referer: http://172.17.0.2:1337/WBrtZV38p/http://host.docker.internal:3000/app/'
Short of making a proxy of some sort on my local machine, is there a way to make a POST from the docker container appear to be coming from my local machine?
Start container in the host OS network space with docker run --network host ... - container will be running in the network of your local machine directly. But you will lose container network isolation, so you should to review security of this approach.
Doc: https://docs.docker.com/network/host/

docker app serving on https and connecting to external rethinkdb

I'm trying to launch a docker container that is running a tornado app in python 3.
It serves a few API calls and is writing data to a rethinkdb service on the system. RethinkDB does not run inside a container.
The system it runs on is ubuntu 16.04.
Whenever I tried to launch the docker with docker-compose, it would crash saying the connection to localhost:28015 was refused.
I went researching the problem and realized that docker has its own network and that external connections must be configured prior to launching the container.
I used this command from a a question I found to make it work:
docker run -it --name "$container_name" -d -h "$host_name" -p 9080:9080 -p 1522:1522 "$image_name"
I've changed the container name, host name, ports and image name to fit my own application.
Now, the docker is not crashing, but I have two problems:
I can't reach it from a browser by pointing to https://localhost/login
I lose the docker-compose usage. This is problematic if we want to add more services that talk to each other in the future.
So, how do I launch a docker that can talk to my rethinkdb database without putting that DB into a container?
Please, let me know if you need more information to answer this question.
I'd appreciate your guidance in this.
The end result is that the docker will serve requests coming over https.
for exmaple I have an end-point called /getURL.
The request includes a token verified in the DB. The URL is like this:
https://some-domain.com/getURL
after verification with the DB it will send back a relevant response.
the docker needs to be able to talk on 443 and also on 28015 with the rethinkdb service.
(Since 443 and https include the use of certificates, I'd appreciate a solution that handles this on regular http with some random port too and I'll take it from there)
Thanks!
P.S. The service works when I launch it without a docker on pycharm it's the docker configuration I have problems with.
I found a solution.
I needed to add this so that the container can connect to both the database and the rethinkdb:
--network="host"
Since this solution works for me right now, but it isn't the best solution, I won't mark this as the answer for now.

How to access a docker container through SSH?

I am currently thinking of building a docker image for my ipython parallel nodes. Because its a pain to configure each manually with commands. Will i be able to access this image (located on a different PC on my LAN) simply by typing ssh user#ip on my laptop (Master Node)? How do i get the ip of the docker image running on my Node?
Will i be able to access this image (located on a different PC on my LAN) simply by typing ssh user#ip on my laptop (Master Node)?
You cannot ssh into a container unless you arrange to run sshd inside that container. Normally that's not necessary; as this answer explains you can simply use docker exec to access a running container.
How do i get the ip of the docker image running on my Node?
First, a note about nomenclature: an image is just a collection of files. A container is what you get when you start services from an image. In other words, it doesn't make sense to ask questions about accessing or getting the ip address of an image.
You can get the ip address of a container using the docker container inspect command, which will show you a variety of information about your container. However, this may not be what you want: the ip address of the container will be a private ip address on a docker internal network that is only accessible from the host where you're running docker.
You provide remote access to services by using port forwarding (the -p flag to docker run). For example, if you're running a webserver on port 8080 inside a container, you could make that available on port 80 on your host doing something like:
docker run -p 80:8080 mywebserver
This document describes in more detail some of the options related to port forwarding.

Some questions of Docker -p and Dockerfile

1: docker run -d -p 3000:3000 images
If i up a localhost:3000 server in container,how can i open it in my machine browser, what's the ip?
I've tried localhost:3000 or 0.0.0.0:3000.
2: I use docker pull ubuntu and docker run it, after updating and deploying the server i commmited it.So now i have one ubuntu and a new image.
Next time i run a container using this new image,
The shell scripts still needs to be sourced, also the server needs to reopened again.
How can i commit the image that it can source the scripts and deployed by itself when i docker run it.
Thanks.
I don't quite understand questions 2 or 3, can you add more context?
Regarding your question about using -p, you should be able to visit in your browser using http://localhost:3000/ . However that assumes a couple of things are true.
First, you used -p 3000:<container-port> - looks good on this point.
Second, the image you have run exposed port 3000 (EXPOSE 3000).
And third, the service running in the container is listening on 0.0.0.0:3000. If it is listening on localhost inside the container, then the port export will not work. Each container has its own localhost which is usable only inside the container. So it needs to be listening on all IPs inside the container, in order for outside connections to reach the service from outside of the container.

Resources