2016-09-06 14:46:37 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
Don't use AutoRemove on older daemons
Docker 1.13 moves the `--rm` flag to the daemon,
through an AutoRemove option in HostConfig.
When using API 1.24 and under, AutoRemove should not be
used, even if the daemon is version 1.13 or above and
"supports" this feature.
This patch fixes a situation where an 1.13 client,
talking to an 1.13 daemon, but using the 1.24 API
version, still set the AutoRemove property.
As a result, both the client _and_ the daemon
were attempting to remove the container, resulting
in an error:
ERRO[0000] error removing container: Error response from daemon:
removal of container ce0976ad22495c7cbe9487752ea32721a282164862db036b2f3377bd07461c3a
is already in progress
In addition, the validation of conflicting options
is moved from `docker run` to `opts.parse()`, so
that conflicting options are also detected when
running `docker create` and `docker start` separately.
To resolve the issue, the `AutoRemove` option is now
always set to `false` both by the client and the
daemon, if API version 1.24 or under is used.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-01-12 19:05:39 -05:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-09-06 14:46:37 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type configWrapper struct {
|
|
|
|
*container.Config
|
|
|
|
HostConfig *container.HostConfig
|
|
|
|
NetworkingConfig *network.NetworkingConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerCreate creates a new container based in the given configuration.
|
|
|
|
// It can be associated with a name, but it's not mandatory.
|
2016-10-14 16:28:47 -04:00
|
|
|
func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) {
|
|
|
|
var response container.ContainerCreateCreatedBody
|
2016-11-02 20:43:32 -04:00
|
|
|
|
|
|
|
if err := cli.NewVersionError("1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
Don't use AutoRemove on older daemons
Docker 1.13 moves the `--rm` flag to the daemon,
through an AutoRemove option in HostConfig.
When using API 1.24 and under, AutoRemove should not be
used, even if the daemon is version 1.13 or above and
"supports" this feature.
This patch fixes a situation where an 1.13 client,
talking to an 1.13 daemon, but using the 1.24 API
version, still set the AutoRemove property.
As a result, both the client _and_ the daemon
were attempting to remove the container, resulting
in an error:
ERRO[0000] error removing container: Error response from daemon:
removal of container ce0976ad22495c7cbe9487752ea32721a282164862db036b2f3377bd07461c3a
is already in progress
In addition, the validation of conflicting options
is moved from `docker run` to `opts.parse()`, so
that conflicting options are also detected when
running `docker create` and `docker start` separately.
To resolve the issue, the `AutoRemove` option is now
always set to `false` both by the client and the
daemon, if API version 1.24 or under is used.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-01-12 19:05:39 -05:00
|
|
|
// When using API 1.24 and under, the client is responsible for removing the container
|
|
|
|
if hostConfig != nil && versions.LessThan(cli.ClientVersion(), "1.25") {
|
|
|
|
hostConfig.AutoRemove = false
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
query := url.Values{}
|
|
|
|
if containerName != "" {
|
|
|
|
query.Set("name", containerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
body := configWrapper{
|
|
|
|
Config: config,
|
|
|
|
HostConfig: hostConfig,
|
|
|
|
NetworkingConfig: networkingConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
|
|
|
|
if err != nil {
|
|
|
|
if serverResp.statusCode == 404 && strings.Contains(err.Error(), "No such image") {
|
|
|
|
return response, imageNotFoundError{config.Image}
|
|
|
|
}
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(serverResp.body).Decode(&response)
|
|
|
|
ensureReaderClosed(serverResp)
|
|
|
|
return response, err
|
|
|
|
}
|