How to set Docker container name using REST API
I am trying to create and run a container via REST API. That is, using the following API:
curl --http1.0 --request POST --header "Content-Type: application.json" http://$DOCKER_HOST:4243/containers/create --data @create.json
where create.json is your desired JSON properties file. I would like to assign a specific name to the container. This can be done via the docker CLI using the -name parameter in the run command:
docker run --name "my_name" my_image
The documentation for creating a container ( https://docs.docker.com/reference/api/docker_remote_api_v1.15/#create-a-container ) provides an example JSON payload. No name attribute specified. I tried the following variations on the name attribute:
"Name": "my_name"
"Names": [ "my_name" ]
"Names": [ "/my_name" ]
"Name": "/my_name"
all to no avail. Changes based on query results:
curl --http1.0 http://$DOCKER_HOST:4243/containers/json
which returns records that include:
"Names":["/elegant_mccarthy"]
source to share
You are including name
as a request parameter, not in JSON.
In the docs (on the page you linked):
Request parameters:
name - assign the specified name to the container. Must match /? [A-zA-Z0-9 _-] +.
Perhaps something like:
curl --http1.0 --request POST --header "Content-Type: application.json" http://$DOCKER_HOST:4243/containers/create?name=your-name --data @create.json
In case of doubt:
You can use socat to sit between your Docker CLI calls and the unix socket it uses to talk to the daemon to see exactly what happens when you use the docker
client, as it just talks to the API itself.
In one terminal:
socat -t100 -v UNIX-LISTEN:/tmp/proxysocket.sock,mode=644,reuseaddr,fork UNIX-CONNECT:/var/run/docker.sock
And in another terminal:
export DOCKER_HOST="unix:///tmp/proxysocket.sock"
docker run -ti --name=test ubuntu:14.04 /bin/bash
And you should be able to see in the output of the first terminal:
2014/10/26 02: 20: 15.176744 length = 564 from = 0 to = 563
POST / v1.15 / containers / create? Name = test HTTP / 1.1 \ r
Host: /tmp/proxysocket.sock \ r
User-agent: Docker-Client / 1.2.0-dev \ r
Content-Length: 370 \ r
Content-Type: application / json \ r
Accept-Encoding: gzip \ r
source to share