docker port mapping syntax - docker

I am new to docker, and I am a bit confused about what the following command options do specifically for the command I came across.
--name : appname is the name of the image?
-t : Run in terminal?
-d : run as daemon?
-p : for somebody outside the container to talk to port 9090 they have to connect on port 9000?
Same for port 15501 but it is a udp port?
appname2: name assigned to running image?
docker run -t --name=appname -p 9090:9000 -p 15501:15501/udp -d appname2

docker run -t --name=appname -p 9090:9000 -p 15501:15501/udp -d appname2
Q: --name : appname is the name of the image?
No. It's the name of the container that you are creating (optional).
--name string Assign a name to the container
Q: -t : Run in terminal?
-t, --tty Allocate a pseudo-TTY
Q: -d : run as daemon?
Sort of. It means that you want to run your container detached from your terminal.
-d, --detach Run container in background and print container ID
Q: -p : for somebody outside the container to talk to port 9090 they have to connect on port 9000?
9090:9000 means: port 9090 on the host machine binded to port 9000 on the container. To talk to the container port someone outside should talk to 9090.
-p, --publish list Publish a container's port(s) to the host (default [])
Q: Same for port 15501 but it is a udp port?
Right.
Q: appname2: name assigned to running image?
That is the image that you are running on. The container is based on top of it.
Bonus! You can find all of this info here: docker help run
Bonus 2! Try it yourself:
docker run -d -it --name my-container alpine sh
docker inspect my-container
# See all this funny output. It's all about the container that you've created

From https://docs.docker.com/engine/reference/run/
The -d flag means detached. When you run a docker container, you can either run a container in foreground, or you can run it in the background. The choice of how to run your container really depends on your use case. If, for example, you run an OS container with some functionality, you would probably want run the container in foreground in order to use this functionality. But if you run a DB server, you may want to run it in the background.
The -p flag, when used, publishes all exposed ports to the host interfaces. If for example you run a DB server inside a container which has some ports exposed, and you wish to communicate to the server from a distance, you may want to map the ports inside the container to a single or multiple ports of choice on your host system. That way when you connect to the port on your host, you connect to the docker server running inside of it (I hope this is clear). The mapping format is as follows:
ip:hostPort:contain`enter code here`erPort | ip::containerPort | hostPort:containerPort | containerPort
The --name flag gives the running container a nice name. If not used, it would generate a name. It can be used, for example, if you executed a container in detached mode, and then you wanted to get inside the container using the attach command.
The -t flag allocates a text console for the container.
appname2 is the name of the docker image.

Related

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.

portforwarding from within docker container

I am trying to get a container built that has ports forwarded, such that code running in the container can access a remote db for instance.
So I put the line
CMD ssh -L 27017:localhost:27017
in my dockerfile and then run it, but in no case do I see ports forwarded (I tried RUN instead of cmd, and tried both interactively running the container or using -d , but in the former case I don't have forwarding and in the latter case the container exits soon after starting, even after tacking && /bin/bash to the end of the ssh command).
The only way I have succeeded doing this is doing an interactive run
$docker run -it --name cont_name im_name /bin/bash
and then from the interactive shell doing the ssh necessary for port forwarding (which now ties up the shell). Then from another window on my local host, I 'get inside' the first container using
$ docker exec -it cont_name bash
where I now indeed see ports forwarded . Is there a better/automatic way to do this? 'screen' seems to be a hassle to get running in a container.
I think what you want to do is "bind" the port from the container to the host. First thing you need to know is if the port has been exposed via EXPOSE in the docker container you plan on using. The next thing would be to add this to the docker run...:
-p 127.0.0.1:3360:3306
Let's assume it's a mysql instance that you are using, the port that is exposed is 3306, so you would bind that to the host on the same port or whatever port you prefer on the host.

Docker in Docker: Port Mapping

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).

Ambiguous port publish parameters to "docker run"

Docker's shipyard project has a prebuilt container to simplify running its components. It's simply just a run script that launches and links several other containers.
However, I find their usage of the port-publish parameter (-p) confusing in two of the run commands:
sudo docker run -i -t -d -p 80 --link shipyard_redis:redis --name shipyard_router shipyard/router
sudo docker run -i -t -d -p 80:80 --link shipyard_redis:redis --link shipyard_router:app_router --name shipyard_lb shipyard/lb
The first command passes a single parameter to "-p", which doesn't seem legal, since every official usage is suppose to have at least two, colon-separated parts:
-p, --publish=[] Publish a container's port to the host
format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort
(use 'docker port' to see the actual mapping)
The second command is confusing because it seems like this would cause a port collision with the container started in the first command.
Can someone clarify?
When you specify -p with only 1/single port number. Docker automatically assigns a random port mapping (usually starting from port 49150) to the single port exposed in the container ie. 80
what this means is, lets say you run Apache 2 on port 80 inside your container. Then you will have to point your browser to localhost:49150 to access your Apache web server.

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