Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port mapping with Docker remote api #2785

Closed
hychan opened this issue Nov 20, 2013 · 10 comments
Closed

Port mapping with Docker remote api #2785

hychan opened this issue Nov 20, 2013 · 10 comments

Comments

@hychan
Copy link

hychan commented Nov 20, 2013

I looked at the inputs for creating a container, there is no parameters that allow me to represent this docker CLI command:

docker run -d -p 90:80 myConainer/apache2 /usr/sbin/apache2 cntl -D FOREGROUND

How do I use the remote API to generate a command as above ?

  1. background mode
  2. port forwarding
  3. the -D FOREGROUND option

Thanks.

@pradeep-mishra
Copy link

if i understand you correctly

for background mode you don't need to do anything , by default remote API run container in detached mode.

for running container you have to create container then start it.
and for port binding you have to pass specific argument while creating container and starting it

eg:

for creating container:
POST /containers/create HTTP/1.1
Content-Type: application/json

{
"Image":"your-image-name",
"ExposedPorts":{"8080/tcp":{}}
}

and for starting container:
POST /containers/{$id}/start HTTP/1.1
Content-Type: application/json

{
"PortBindings": { "8080/tcp": [{ "HostPort": "11022" }]}
}

refer doc

@hychan
Copy link
Author

hychan commented Nov 21, 2013

Thank you so much for the reply. I will try it, very useful info.

However, I would like to ask the following questions form the last 2 days
I was working with Docker API.

  1. I have created a few images/containers on the remote Docker host from
    the client using the remote REST API. When I logged in to the Docker
    host and used Docker CLI such as : docker images and docker ps -a, I could
    not find the images/containers I created via the remote client, only
    those I created directly using Docker CLI are visible. Is there any I can
    access these remotely created images/containers on the Docker host ? If
    there a special way to access them directly on the host ?
  2. I am trying to stream data back from the Docmer server to my client, I
    always got timeout error, Could please show me an example of how data
    can be streamed ? ( Best if you have Ruby examples, others are also
    appreciated )
  3. I want to execute some install scripts inside the containers. I
    just first created an image e.g. 'fromImage' => 'base', then inserted the
    scripts into the image, then create a container from that image with
    commands to execute the scripts. -- Is this the correct approach ?

More questions later....

Again, thanks.

Docker is an exciting and wonderful piece of software, we are very
interesting in making use of it in our project.

HC

From: Pradeep notifications@github.com
To: dotcloud/docker docker@noreply.github.com
Cc: Hoi Chan/Watson/IBM@IBMUS
Date: 11/21/2013 03:07 PM
Subject: Re: [docker] Port mapping with Docker remote api (#2785)

if i understand you correctly

for background mode you don't need to do anything , by default remote API
run container in detached mode.

for running container you have to create container then start it.
and for port binding you have to pass specific argument while creating
container and starting it

eg:

for creating container:
POST /containers/create HTTP/1.1
Content-Type: application/json

{
"Image":"your-image-name",
"ExposedPorts":{"8080/tcp":{}}
}

and for starting container:
POST /containers/{$id}/start HTTP/1.1
Content-Type: application/json

{
"PortBindings": { "8080/tcp": [{ "HostPort": "11022" }]}
}

refer doc


Reply to this email directly or view it on GitHub.

@unclejack
Copy link
Contributor

@hychan Could you post on the docker-user Google group about this, please? It's the right place to get help from other Docker users and discuss about this.

What you're describing isn't a problem with Docker itself. I'll close this issue now. Please feel free to comment.

@hychan
Copy link
Author

hychan commented Dec 4, 2013

Thanks for you reply.

I searched the Docker document base both on Docker.io url and google. I was unable to find the docmentation listing all the options as you described. Could you please point me to the document sources for using the various options ?

for creating container:
POST /containers/create HTTP/1.1
Content-Type: application/json

{
"Image":"your-image-name",
"ExposedPorts":{"8080/tcp":{}}
}

and for starting container:
POST /containers/{$id}/start HTTP/1.1
Content-Type: application/json

{
"PortBindings": { "8080/tcp": [{ "HostPort": "11022" }]}
}

@gezhiwei8899
Copy link

start_container status: 400
start_container reponse {"message":"starting container with non-empty request body was deprecated since API v1.22 and removed in v1.24"}

@thaJeztah
Copy link
Member

@gezhiwei8899 those options should be set on container create not container start.

An easy way to know what requests should be made, is to put the daemon in debug mode; doing so will output the requests that are made by the docker cli in the logs.

For example (starting the daemon manually here, just to show);

$ dockerd --debug

...
...
DEBU[2018-03-16T13:16:07.815388475Z] Registering POST, /networks/{id:.*}/disconnect 
DEBU[2018-03-16T13:16:07.815497235Z] Registering POST, /networks/prune            
DEBU[2018-03-16T13:16:07.815637748Z] Registering DELETE, /networks/{id:.*}        
INFO[2018-03-16T13:16:07.816020034Z] API listen on /var/run/docker.sock    

In another shell, run a container;

docker run -d --name mycontainer -p8080:80 nginx:alpine

And in the daemon logs, you'll see the requests that were made (docker run is a combination of docker create and docker start);

DEBU[2018-03-16T13:17:39.015263823Z] Calling POST /v1.35/containers/create?name=mycontainer 
DEBU[2018-03-16T13:17:39.015720505Z] form data: {"AttachStderr":false,"AttachStdin":false,"AttachStdout":false,"Cmd":null,"Domainname":"","Entrypoint":null,"Env":[],"ExposedPorts":{"80/tcp":{}},"HostConfig":{"AutoRemove":false,"Binds":null,"BlkioDeviceReadBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceWriteIOps":null,"BlkioWeight":0,"BlkioWeightDevice":[],"CapAdd":null,"CapDrop":null,"Cgroup":"","CgroupParent":"","ConsoleSize":[0,0],"ContainerIDFile":"","CpuCount":0,"CpuPercent":0,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpuShares":0,"CpusetCpus":"","CpusetMems":"","DeviceCgroupRules":null,"Devices":[],"DiskQuota":0,"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IOMaximumBandwidth":0,"IOMaximumIOps":0,"IpcMode":"","Isolation":"","KernelMemory":0,"Links":null,"LogConfig":{"Config":{},"Type":""},"Memory":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":-1,"NanoCpus":0,"NetworkMode":"default","OomKillDisable":false,"OomScoreAdj":0,"PidMode":"","PidsLimit":0,"PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"8080"}]},"Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"RestartPolicy":{"MaximumRetryCount":0,"Name":"no"},"SecurityOpt":null,"ShmSize":0,"UTSMode":"","Ulimits":null,"UsernsMode":"","VolumeDriver":"","VolumesFrom":null},"Hostname":"","Image":"nginx:alpine","Labels":{},"NetworkingConfig":{"EndpointsConfig":{}},"OnBuild":null,"OpenStdin":false,"StdinOnce":false,"Tty":false,"User":"","Volumes":{},"WorkingDir":""} 


....
....


DEBU[2018-03-16T13:17:39.138336370Z] Calling POST /v1.35/containers/30e8d348fd0d82fa50b1daa46a0cccb185aadf6336caebf2ab2132c6ec236195/start 

Note that for backward compatibility, it's still possible to set these options on container start, but only if you use the old API version (so use POST /v1.23/containers/create HTTP/1.1 instead of just /containers/create)

You can find the full API documentation (per version) here: https://docs.docker.com/develop/sdk/#api-version-matrix

For example, the API version matching Docker 17.12; https://docs.docker.com/engine/api/v1.35/

@anselmobattisti
Copy link

After 1 hour!!!!

the parameter HostIp is required! You can't leave it blank!

@thaJeztah
Copy link
Member

@anselmobattisti do you have more details? Trying API calls without HostIp specified, and that seems to work;

Create a foobar container, running nginx:alpine that publishes port 80 on
host port 8080:

curl -v \
  --unix-socket /var/run/docker.sock \
  "http://localhost/v1.40/containers/create?name=foobar" \
  -H "Content-Type: application/json" \
  -d '{"Image":"nginx:alpine", "HostConfig":{"PortBindings":{"80/tcp":[{"HostPort":"8080"}]}}}'

Start the container:

curl -v \
  --unix-socket /var/run/docker.sock \
  -X POST \
  "http://localhost/v1.40/containers/foobar/start"

connect to the container and verify that we get the "Welcome to nginx" page

curl -s localhost:8080 | grep Welcome
<title>Welcome to nginx!</title>
<h1>Welcome to nginx!</h1>

@FrankFang
Copy link

After 1 hour!!!!

the parameter HostIp is required! You can't leave it blank!

What is the value of HostIp?

@thaJeztah
Copy link
Member

@FrankFang depends what you want it to be published on, but see my example above; if you want the equivalent of the -p 90:80 in the original comment, it should not be needed to provide HostIp (default is 0.0.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants