TCP connection between docker containers - docker

I have a server listening on port 6000 inside a docker container and I've exposed port 6000 in the dockerfile:
FROM rust:latest
WORKDIR /usr/src/server
COPY . .
RUN cargo install --path .
EXPOSE 6000
RUN cargo run
then run it using:
docker build -t server
docker run --rm -it -p 6000:6000 server
I then have a client in another container trying to make a tcp connection at port 6000, but it's failing to connect. When they're both run not in containers they can connect no issues, but trying to use docker is causing issues. Do I have to do something with my clients container in order to connect to port 6000 outside of its own container? I think it's probably a very simple issue I'm just new to docker so any help would be greatly appreciated.

The RUN command is a build time execution, I think you are looking for CMD instead. Also the EXPOSE is not necessary, it only serves documentational purposes. Lastly, you need to check whether the container is even running before trying to access it from the client. Do a docker ps -a after starting the server and look at the container status. If it isn't running you can check the logs with docker logs <container name / hash>. Let me know if you have questions.

Related

Communicate with GET request between 2 applications - only one of them run as a docker container

I have created an image of my application and run a container on one machine. my inner container port is: 8081.
I see that the container is running.
I would like to talk with that container from another machine from an application that doesnt run as a docker container, with a GET request, all the machines in the same VLAN.
How do I communicate with that docker container from another machine? with which ip and port ?
in the first trial - I run the container with the exposed port as the inner port :
sudo docker run -d -p 8081:8081 myimage:2.0
in my second trial I run the container like this:
sudo docker run -d -p 80:8081 myimage:2.0
_______
Update:
so I would like to share what was the problem with my container:
so initially I was adding at the end of my docker file the following line so that it will not exit immediately.:
CMD tail -f /dev/null
it seems to run however only when adding in my sh script
while true; do sleep 1000; done
I was able to access my container.
Exposing the port with the -p <outside>:<inside> flag will pass through the all traffic on the outside port of your machine running the container to the inside port in your container. So you can just communicate with the container through either port 80 or port 8081 depending on what command you start your container with. The ip will be the ip of the machine running the container.
In my case I was running sh file from the Dockerfile.
Adding: "while true; do sleep 1000; done" to my sh file solved the issue.
this is in addition to Exposing the port with the -p : flag

Run docker image without port forwarding

I am running a custom image (based on the docker image Locust) locally using the below command
docker run -p 5557:5557 my-stress-test:0.1
My dockerfile looks as below
FROM locustio/locust:latest
COPY ./ /mnt/locust
CMD ["-P", "5557", "-f", "/mnt/locust/locustfile.py"]
Now, I deploy this image on to my cloud service which runs this image generating the command
docker run -p 5557 my-stress-test:0.1
This is the command I cannot change. However, I am not able to run the image without port forwarding, like -p 5557:5557. How can I change my dockerfile or anything to run the image without port forwarding.
In dockers you should know how its network works.
There is 3 type of port configuration:
docker run my-stress-test:0.1
docker run -p 5557:5557 my-stress-test:0.1
docker run -p 127.0.0.1:5557:5557 my-stress-test:0.1
In the first type, only apps in the same network as this app can connect to that port.
In the second type, all apps inside and outside of the container network can connect to that port.
In the third type only apps inside the container network, and other apps inside the host can connect to the app, and apps outside of the host cannot connect to the app.
I think the third type is what you are looking for.
If you have multiple network in your host, and you want other apps from other hosts to access the app, you can bind that network ip to the port. for example
docker run -p 192.168.0.10:5557:5557 my-stress-test:0.1
With Docker, you can't publish ports in the dockerfile or at any other time other than at run by design.
How to publish ports in docker files
This question will be best posed to your cloud provider as to why you're unable to change the command that runs a container of your image.

Docker - Web browser cannot connect to a running web app container on server

I have successfully built my web app image and ran a container on my server, which is an EC2 instance, with no error at all, but when I tried to access the web page it returned no connection, even though I accessed through the binded port of the host server. The build and run processes gave absolutely no error, either build error or connection error. I'm new to both Docker and AWS, so I'm not sure what could be the problem. Any help from you guys is really appreciated. Thanks a lot!
Here is my Dockerfile
FROM ubuntu
WORKDIR /usr/src/app
# install dependencies, nothing wrong
RUN ...
COPY...
#
# exposed ports
EXPOSE 5000
EXPOSE 5100
CMD ...
Docker build
$ sudo docker built -t demo-app
Docker run command
$ sudo docker run -it -p 8080:5000 -p 808:5100 --name demo-app-1 demo-app
I accessed through the binded port of the host server.
It's mean the application is running, and you're able to access using curl localhost:8080.
Now there are mainly two-issue if you're able to access the application after doing ssh to EC2 instance and verify the application responding on localhost of EC2.
Security group not allowing connection on the desired port, allow 8080 and the check
The instance is in private subnet, you can verify the instance.

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/

docker port mapping doesn't work

I'm confused - I have Docker file which use as "FROM" https://hub.docker.com/r/consol/ubuntu-xfce-vnc/ image. It's very fast way to build vnc container with gui and chromium what is require by my project. And everything is fine but i have problem with port mapping - i want to run few similar containers on same server so it's obvious that each container must use different port. I assumed that the easiest way will be run as below:
docker run -p 5902:5901 -t cont1
docker run -p 5903:5901 -t cont2
docker run -p 5904:5901 -t cont3
docker run -p 5905:5901 -t cont4
but when i try to connect via vnc, the connection can by established only with 5901 port - despite the fact that i use -p with mapping. Maybe someone will know what's wrong?
Finally I found the reason of my problem - I have been running my containers locally and I assume that that's was the reason - When I had moved them to external server, port mapping is ok right now! Cheers

Resources