Docker in Docker: Port Mapping - docker

I have found a similar thread, but failed to get it to work. So, the use case is
I start a container on my Linux host
docker run -i -t --privileged -p 8080:2375 mattgruter/doubledocker
When in that container, I want to start another one with GAE SDK devserver running.
At that, I need to access a running app from the host system browser.
When I start a container in the container as
docker run -i -t -p 2375:8080 image/name
I get an error saying that 2375 port is in use. I start the app, and can curl 0.0.0.0:8080 when inside both containers (when using another port 8080:8080 for example) but cannot preview the app from the host system, since lohalhost:8080 listens to 2375 port in the first container, and that port cannot be used when launching the second container.

I'm able to do that using the image jpetazzo/dind. The test I have done and worked (as an example):
From my host machine I run the container with docker installed:
docker run --privileged -t -i --rm -e LOG=file -p 18080:8080
jpetazzo/dind
Then inside the container I've pulled nginx image and run it with
docker run -d -p 8080:80 nginx
And from the host environment I can browse the nginx welcome page with http://localhost:18080
With the image you were using (mattgruter/doubledocker) I have some problem running it (something related to log attach).

Related

Run a .war on a Docker container

I'm running a Java web application on a Docker cluster running those commands:
PS C:\Users\Marco\test_workspace> docker run -v test_web_application.war:/usr/local/tomcat/webapps/TestWebApplication.war -it -p 8080:8080 --network "host" -d Tomcat
The actual output confirms that the container is running:
At this point i want to access to the container through it's IP address from my host and i'm using the command inspect to identify the IP:
But, as the screenshot shows, i don't see any IP assigned.
Thus, my questions are:
Why the command --network "host" to assign an IP address shared with the host didn't worked ?
Finally, how can i access to my web application from the host ?
Command option --network="host" isn't supported for Docker for Windows (more information: https://docs.docker.com/network/host/).
You can access your application on localhost:8080 with launch option -p 8080:8080.

docker visit container from LAN

I have a program running inside a docker container and I expose the port 8888. However, when I try to connect the program from a device (in the same LAN as host machine but not the host machine) it failed.
Here is my docker file
FROM golang:1.10.1
......
RUN go build -buildmode=plugin -o plugin.so plugin.go
EXPOSE 8666:8888
And I start the container with
docker run -it -P --network host plugin:v0.3 bash
and run
go run program.go
in bash.
It says
2018/07/30 01:51:43 listening port 8888
But I cannot connect to the port from other device(which is in the same LAN as host machine but not the host machine)
then I tried
docker ps -a
It looks different in that the ports column are empty(Usually there should be a mapping?)
Expose won’t create port mappings for you, they’re just a “note from the designer to the user”. Add -p 8666:8888 to your Docker run command line.
I've solved the problem by replacing
go run program.go
afer
docker run -it -P --network host plugin:v0.3 bash
with
docker run -p 8666:8888 plugin:v0.5 go run program.go
It's like magic I still don't know why but it works.(seems like docker will only do the port forwarding when the container starts)
I'll dig into it later.

Cannot access web on running docker containers

I have two containers, wds and apache. Both of them are running and have clear logs. I also checked if apache is running inside apache container and It is. My problem is that if I try to connect at localhost:80 which is the port that apache container listents to, I get only ERR_TIMED_OUT. Can you point me in which direction to look ? Containers were builded succesfully, no errors in logs, apache is running. I don't know where to look.
did you expose the port in Dockerfile and used -p 80:80 while using docker run command?
There is a specific logic to be followed while running or interacting with containers.
I do not know what commands or arguments you want to use so I will put an example here with basic explanation assuming you want to run a container with exposed port 80 in terminal interactive
docker run [container ID] -ti -p 80:80 /bin/bash
used commands:
-t tty - allocate a terminal so you can directly interact with the docker command
-i - interactive - connects STDIN to the allocated terminal. Any command you enter after this will go to the terminal.
-p - binds port
https://docs.docker.com/network/host/
https://docs.docker.com/engine/reference/run/

How to connect to server on Docker from host machine?

Ok, I am pretty new to Docker world. So this might be a very basic question.
I have a container running in Docker, which is running RabbitMQ. Let's say the name of this container is "Rabbit-container".
RabbitMQ container was started with this command:
docker run -d -t -i --name rmq -p 5672:5672 rabbitmq:3-management
Python script command with 2 args:
python ~/Documents/myscripts/migrate_data.py amqp://rabbit:5672/ ~/Documents/queue/
Now, I am running a Python script from my host machine, which is creating some messages. I want to send these messages to my "Rabbit-container". Hence I want to connect to this container from my host machine (Mac OSX).
Is this even possible? If yes, how?
Please let me know if more details are needed.
So, I solved it by simply mapping the RMQ listening port to host OS:
docker run -d -t -i --name rmq -p 15672:15672 -p 5672:5672 rabbitmq:3-management
I previously had only -p 15672:15672 in my command. This is mapping the Admin UI from Docker container to my host OS. I added -p 5672:5672, which mapped RabbitMQ listening port from Docker container to host OS.
If you're running this container in your local OSX system then you should find your default docker-machine ip address by running:
docker-machine ip default
Then you can change your python script to point to that address and mapped port on <your_docker_machine_ip>:5672.
That happens because docker runs in a virtualization engine on OSX and Windows, so when you map a port to the host, you're actually mapping it to the virtual machine.
You'd need to run the container with port 5672 exposed, perhaps 15672 as well if you want WebUI, and 5671 if you use SSL, or any other port for which you add tcp listener in rabbitmq.
It would be also easier if you had a specific IP and a host name for the rabbitmq container. To do this, you'd need to create your own docker network
docker network create --subnet=172.18.0.0/16 mynet123
After that start the container like so
docker run -d --net mynet123--ip 172.18.0.11 --hostname rmq1 --name rmq_container_name -p 15673:15672 rabbitmq:3-management
note that with rabbitmq:3-management image the port 5672 is (well, was when I used it) already exposed so no need to do that. --name is for container name, and --hostname obviously for host name.
So now, from your host you can connect to rmq1 rabbitmq server.
You said that you have never used docker-machine before, so i assume you are using the Docker Beta for Mac (you should see the docker-icon in the menu bar at the top).
Your docker run command for rabbit is correct. If you now want to connect to rabbit, you have two options:
Wrap your python script in a new container and link it to rabbit:
docker run -it --rm --name migration --link rmq:rabbit -v ~/Documents/myscripts:/app -w /app python:3 python migrate_data.py
Note that we have to link rmq:rabbit, because you name your container rmq but use rabbit in the script.
Execute your python script on your host machine and use localhost:5672
python ~/Documents/myscripts/migrate_data.py amqp://localhost:5672/ ~/Documents/queue/

How to assign a host port to container port using docker if container is already created and running?

We can create a new container and define your application port in docker run command like
sudo docker run -d -p 5000:5000 training/webapp python app.py
or
sudo docker run -d -P training/webapp python app.py
But, what if someone forgot to specify -p or -P option in docker run command? The container get created and runs the application locally. Now how could I assign a port on which application is running locally in container to the port of my Ubuntu host machine?
Kindly, help on this.
Thanks.
Short: You can't. You need to stop the container (or not) and start a new one with the proper parameters.
Docker spins up a local proxy and setup the iptables for proper NAT. If you really can't start a new container, you could manually setup the iptables and spin up a socat. You can take a look at the network part of the Docker code for more info.

Resources