2018-02-05 21:05:59 +00:00
package daemon // import "github.com/docker/docker/daemon"
2014-07-31 20:36:42 +00:00
import (
2022-08-09 12:42:50 +00:00
"context"
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 15:53:35 +00:00
"fmt"
2016-08-23 04:52:56 +00:00
"net"
2016-06-07 07:45:21 +00:00
"runtime"
2016-05-27 09:32:26 +00:00
"strings"
2016-07-20 23:11:28 +00:00
"time"
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 15:53:35 +00:00
2020-10-15 23:01:17 +00:00
"github.com/containerd/containerd/platforms"
2016-09-06 18:18:12 +00:00
"github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container"
2022-07-25 15:24:03 +00:00
imagetypes "github.com/docker/docker/api/types/image"
2016-09-06 18:18:12 +00:00
networktypes "github.com/docker/docker/api/types/network"
2015-11-12 19:55:17 +00:00
"github.com/docker/docker/container"
2021-02-17 20:41:12 +00:00
"github.com/docker/docker/daemon/images"
2018-01-11 19:53:06 +00:00
"github.com/docker/docker/errdefs"
2015-07-20 17:57:15 +00:00
"github.com/docker/docker/image"
2015-11-18 22:20:54 +00:00
"github.com/docker/docker/pkg/idtools"
2016-05-24 08:13:54 +00:00
"github.com/docker/docker/runconfig"
2020-10-15 23:01:17 +00:00
v1 "github.com/opencontainers/image-spec/specs-go/v1"
2020-05-05 13:35:03 +00:00
"github.com/opencontainers/selinux/go-selinux"
2018-03-22 21:11:03 +00:00
"github.com/pkg/errors"
2017-07-26 21:42:13 +00:00
"github.com/sirupsen/logrus"
2022-03-28 03:21:22 +00:00
archvariant "github.com/tonistiigi/go-archvariant"
2014-07-31 20:36:42 +00:00
)
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
type createOpts struct {
params types . ContainerCreateConfig
managed bool
ignoreImagesArgsEscaped bool
}
2016-06-14 02:52:49 +00:00
// CreateManagedContainer creates a container that is managed by a Service
2022-10-26 16:13:17 +00:00
func ( daemon * Daemon ) CreateManagedContainer ( ctx context . Context , params types . ContainerCreateConfig ) ( containertypes . CreateResponse , error ) {
return daemon . containerCreate ( ctx , createOpts {
2022-08-23 20:05:09 +00:00
params : params ,
managed : true ,
} )
2016-06-14 02:52:49 +00:00
}
// ContainerCreate creates a regular container
2022-10-26 16:13:17 +00:00
func ( daemon * Daemon ) ContainerCreate ( ctx context . Context , params types . ContainerCreateConfig ) ( containertypes . CreateResponse , error ) {
return daemon . containerCreate ( ctx , createOpts {
2022-08-23 20:05:09 +00:00
params : params ,
} )
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
}
// ContainerCreateIgnoreImagesArgsEscaped creates a regular container. This is called from the builder RUN case
// and ensures that we do not take the images ArgsEscaped
2022-10-26 16:13:17 +00:00
func ( daemon * Daemon ) ContainerCreateIgnoreImagesArgsEscaped ( ctx context . Context , params types . ContainerCreateConfig ) ( containertypes . CreateResponse , error ) {
return daemon . containerCreate ( ctx , createOpts {
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
params : params ,
2022-08-23 20:05:09 +00:00
ignoreImagesArgsEscaped : true ,
} )
2016-06-14 02:52:49 +00:00
}
2022-10-26 16:13:17 +00:00
func ( daemon * Daemon ) containerCreate ( ctx context . Context , opts createOpts ) ( containertypes . CreateResponse , error ) {
2016-07-20 23:11:28 +00:00
start := time . Now ( )
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
if opts . params . Config == nil {
2022-03-05 22:58:46 +00:00
return containertypes . CreateResponse { } , errdefs . InvalidParameter ( errors . New ( "Config cannot be empty in order to create a container" ) )
2015-06-06 16:41:42 +00:00
}
2021-06-11 19:01:18 +00:00
warnings , err := daemon . verifyContainerSettings ( opts . params . HostConfig , opts . params . Config , false )
2015-04-16 06:31:52 +00:00
if err != nil {
2022-03-05 22:58:46 +00:00
return containertypes . CreateResponse { Warnings : warnings } , errdefs . InvalidParameter ( err )
2015-01-23 03:29:21 +00:00
}
2014-09-25 21:23:59 +00:00
2021-06-11 19:01:18 +00:00
if opts . params . Platform == nil && opts . params . Config . Image != "" {
2022-08-09 12:42:50 +00:00
img , err := daemon . imageService . GetImage ( ctx , opts . params . Config . Image , imagetypes . GetImageOpts { Platform : opts . params . Platform } )
if err != nil {
return containertypes . CreateResponse { } , err
}
if img != nil {
2022-03-28 03:21:22 +00:00
p := maximumSpec ( )
2021-06-11 19:01:18 +00:00
imgPlat := v1 . Platform {
OS : img . OS ,
Architecture : img . Architecture ,
Variant : img . Variant ,
}
2020-10-15 23:01:17 +00:00
2021-06-11 19:01:18 +00:00
if ! images . OnlyPlatformWithFallback ( p ) . Match ( imgPlat ) {
warnings = append ( warnings , fmt . Sprintf ( "The requested image's platform (%s) does not match the detected host platform (%s) and no specific platform was requested" , platforms . Format ( imgPlat ) , platforms . Format ( p ) ) )
}
2020-10-15 23:01:17 +00:00
}
}
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
err = verifyNetworkingConfig ( opts . params . NetworkingConfig )
2016-01-21 22:24:35 +00:00
if err != nil {
2022-03-05 22:58:46 +00:00
return containertypes . CreateResponse { Warnings : warnings } , errdefs . InvalidParameter ( err )
2016-01-21 22:24:35 +00:00
}
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
if opts . params . HostConfig == nil {
opts . params . HostConfig = & containertypes . HostConfig { }
2015-11-30 05:10:18 +00:00
}
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
err = daemon . adaptContainerSettings ( opts . params . HostConfig , opts . params . AdjustCPUShares )
2015-11-30 05:10:18 +00:00
if err != nil {
2022-03-05 22:58:46 +00:00
return containertypes . CreateResponse { Warnings : warnings } , errdefs . InvalidParameter ( err )
2015-11-30 05:10:18 +00:00
}
2015-09-07 03:07:12 +00:00
2022-10-26 16:13:17 +00:00
ctr , err := daemon . create ( ctx , opts )
2014-07-31 20:36:42 +00:00
if err != nil {
2022-03-05 22:58:46 +00:00
return containertypes . CreateResponse { Warnings : warnings } , err
2014-07-31 20:36:42 +00:00
}
2016-07-20 23:11:28 +00:00
containerActions . WithValues ( "create" ) . UpdateSince ( start )
2016-10-14 20:28:47 +00:00
2019-01-22 14:27:31 +00:00
if warnings == nil {
warnings = make ( [ ] string , 0 ) // Create an empty slice to avoid https://github.com/moby/moby/issues/38222
}
2022-03-05 22:58:46 +00:00
return containertypes . CreateResponse { ID : ctr . ID , Warnings : warnings } , nil
2014-07-31 20:36:42 +00:00
}
// Create creates a new container from the given configuration with a given name.
2022-10-26 16:13:17 +00:00
func ( daemon * Daemon ) create ( ctx context . Context , opts createOpts ) ( retC * container . Container , retErr error ) {
2014-07-31 20:36:42 +00:00
var (
2019-08-09 12:10:07 +00:00
ctr * container . Container
img * image . Image
imgID image . ID
err error
2021-07-27 10:12:11 +00:00
os = runtime . GOOS
2014-07-31 20:36:42 +00:00
)
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
if opts . params . Config . Image != "" {
2022-08-09 12:42:50 +00:00
img , err = daemon . imageService . GetImage ( ctx , opts . params . Config . Image , imagetypes . GetImageOpts { Platform : opts . params . Platform } )
2014-10-28 21:06:23 +00:00
if err != nil {
2015-09-06 17:26:40 +00:00
return nil , err
2014-10-28 21:06:23 +00:00
}
2021-07-27 10:12:11 +00:00
os = img . OperatingSystem ( )
2022-01-25 11:26:14 +00:00
imgID = img . ID ( )
2021-06-11 19:01:18 +00:00
} else if isWindows {
os = "linux" // 'scratch' case.
2014-07-31 20:36:42 +00:00
}
2014-10-28 21:06:23 +00:00
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
// On WCOW, if are not being invoked by the builder to create this container (where
// ignoreImagesArgEscaped will be true) - if the image already has its arguments escaped,
// ensure that this is replicated across to the created container to avoid double-escaping
// of the arguments/command line when the runtime attempts to run the container.
if os == "windows" && ! opts . ignoreImagesArgsEscaped && img != nil && img . RunConfig ( ) . ArgsEscaped {
opts . params . Config . ArgsEscaped = true
}
if err := daemon . mergeAndVerifyConfig ( opts . params . Config , img ) ; err != nil {
2017-11-29 04:09:37 +00:00
return nil , errdefs . InvalidParameter ( err )
2014-07-31 20:36:42 +00:00
}
2015-08-12 02:27:33 +00:00
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
if err := daemon . mergeAndVerifyLogConfig ( & opts . params . HostConfig . LogConfig ) ; err != nil {
2017-11-29 04:09:37 +00:00
return nil , errdefs . InvalidParameter ( err )
2016-03-12 12:50:37 +00:00
}
2019-08-09 12:10:07 +00:00
if ctr , err = daemon . newContainer ( opts . params . Name , os , opts . params . Config , opts . params . HostConfig , imgID , opts . managed ) ; err != nil {
2015-09-06 17:26:40 +00:00
return nil , err
2014-07-31 20:36:42 +00:00
}
2015-08-11 14:21:52 +00:00
defer func ( ) {
if retErr != nil {
2021-08-20 22:14:25 +00:00
err = daemon . cleanupContainer ( ctr , types . ContainerRmConfig {
ForceRemove : true ,
RemoveVolume : true ,
} )
if err != nil {
logrus . WithError ( err ) . Error ( "failed to cleanup container on create error" )
2015-08-11 14:21:52 +00:00
}
}
} ( )
2019-08-09 12:10:07 +00:00
if err := daemon . setSecurityOptions ( ctr , opts . params . HostConfig ) ; err != nil {
2015-12-21 19:23:20 +00:00
return nil , err
}
2019-08-09 12:10:07 +00:00
ctr . HostConfig . StorageOpt = opts . params . HostConfig . StorageOpt
2016-03-20 04:42:58 +00:00
2015-12-21 19:23:20 +00:00
// Set RWLayer for container after mount labels have been set
2019-08-09 12:10:07 +00:00
rwLayer , err := daemon . imageService . CreateLayer ( ctr , setupInitLayer ( daemon . idMapping ) )
2018-02-02 22:18:46 +00:00
if err != nil {
2017-11-29 04:09:37 +00:00
return nil , errdefs . System ( err )
2015-12-21 19:23:20 +00:00
}
2019-08-09 12:10:07 +00:00
ctr . RWLayer = rwLayer
2015-12-21 19:23:20 +00:00
2021-07-02 17:27:45 +00:00
current := idtools . CurrentIdentity ( )
if err := idtools . MkdirAndChown ( ctr . Root , 0710 , idtools . Identity { UID : current . UID , GID : daemon . IdentityMapping ( ) . RootPair ( ) . GID } ) ; err != nil {
2015-09-06 17:26:40 +00:00
return nil , err
2014-07-31 20:36:42 +00:00
}
2021-07-02 17:27:45 +00:00
if err := idtools . MkdirAndChown ( ctr . CheckpointDir ( ) , 0700 , current ) ; err != nil {
2016-05-12 14:52:00 +00:00
return nil , err
}
2015-11-18 22:20:54 +00:00
2019-08-09 12:10:07 +00:00
if err := daemon . setHostConfig ( ctr , opts . params . HostConfig ) ; err != nil {
2015-09-06 17:26:40 +00:00
return nil , err
2014-09-25 21:23:59 +00:00
}
2015-05-19 20:05:25 +00:00
2019-08-09 12:10:07 +00:00
if err := daemon . createContainerOSSpecificSettings ( ctr , opts . params . Config , opts . params . HostConfig ) ; err != nil {
2015-09-06 17:26:40 +00:00
return nil , err
2014-11-11 16:17:33 +00:00
}
2015-07-16 21:14:58 +00:00
2016-01-08 00:18:34 +00:00
var endpointsConfigs map [ string ] * networktypes . EndpointSettings
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
if opts . params . NetworkingConfig != nil {
endpointsConfigs = opts . params . NetworkingConfig . EndpointsConfig
2016-01-08 00:18:34 +00:00
}
2016-05-24 08:13:54 +00:00
// Make sure NetworkMode has an acceptable value. We do this to ensure
// backwards API compatibility.
2019-08-09 12:10:07 +00:00
runconfig . SetDefaultNetModeIfBlank ( ctr . HostConfig )
2016-01-08 00:18:34 +00:00
2019-08-09 12:10:07 +00:00
daemon . updateContainerNetworkSettings ( ctr , endpointsConfigs )
if err := daemon . Register ( ctr ) ; err != nil {
2017-02-22 22:02:20 +00:00
return nil , err
}
2019-08-09 12:10:07 +00:00
stateCtr . set ( ctr . ID , "stopped" )
daemon . LogContainerEvent ( ctr , "create" )
return ctr , nil
2014-07-31 20:36:42 +00:00
}
2014-12-01 16:44:13 +00:00
2017-04-18 13:26:36 +00:00
func toHostConfigSelinuxLabels ( labels [ ] string ) [ ] string {
for i , l := range labels {
labels [ i ] = "label=" + l
}
return labels
}
2017-02-01 21:52:36 +00:00
func ( daemon * Daemon ) generateSecurityOpt ( hostConfig * containertypes . HostConfig ) ( [ ] string , error ) {
for _ , opt := range hostConfig . SecurityOpt {
con := strings . Split ( opt , "=" )
if con [ 0 ] == "label" {
// Caller overrode SecurityOpts
return nil , nil
}
}
ipcMode := hostConfig . IpcMode
pidMode := hostConfig . PidMode
privileged := hostConfig . Privileged
2016-05-25 19:59:55 +00:00
if ipcMode . IsHost ( ) || pidMode . IsHost ( ) || privileged {
2020-05-05 13:35:03 +00:00
return toHostConfigSelinuxLabels ( selinux . DisableSecOpt ( ) ) , nil
2014-11-10 21:14:17 +00:00
}
2016-05-06 18:56:03 +00:00
var ipcLabel [ ] string
var pidLabel [ ] string
ipcContainer := ipcMode . Container ( )
pidContainer := pidMode . Container ( )
if ipcContainer != "" {
2015-12-11 17:39:28 +00:00
c , err := daemon . GetContainer ( ipcContainer )
2014-12-16 23:06:35 +00:00
if err != nil {
return nil , err
2014-11-10 21:14:17 +00:00
}
2020-05-05 13:35:03 +00:00
ipcLabel , err = selinux . DupSecOpt ( c . ProcessLabel )
2019-03-21 08:58:13 +00:00
if err != nil {
return nil , err
}
2016-05-06 18:56:03 +00:00
if pidContainer == "" {
2017-04-18 13:26:36 +00:00
return toHostConfigSelinuxLabels ( ipcLabel ) , err
2016-05-06 18:56:03 +00:00
}
}
if pidContainer != "" {
c , err := daemon . GetContainer ( pidContainer )
if err != nil {
return nil , err
}
2014-11-10 21:14:17 +00:00
2020-05-05 13:35:03 +00:00
pidLabel , err = selinux . DupSecOpt ( c . ProcessLabel )
2019-03-21 08:58:13 +00:00
if err != nil {
return nil , err
}
2016-05-06 18:56:03 +00:00
if ipcContainer == "" {
2017-04-18 13:26:36 +00:00
return toHostConfigSelinuxLabels ( pidLabel ) , err
2016-05-06 18:56:03 +00:00
}
}
if pidLabel != nil && ipcLabel != nil {
for i := 0 ; i < len ( pidLabel ) ; i ++ {
if pidLabel [ i ] != ipcLabel [ i ] {
return nil , fmt . Errorf ( "--ipc and --pid containers SELinux labels aren't the same" )
}
}
2017-04-18 13:26:36 +00:00
return toHostConfigSelinuxLabels ( pidLabel ) , nil
2014-11-10 21:14:17 +00:00
}
return nil , nil
}
2015-06-12 13:25:32 +00:00
2016-05-24 15:49:26 +00:00
func ( daemon * Daemon ) mergeAndVerifyConfig ( config * containertypes . Config , img * image . Image ) error {
if img != nil && img . Config != nil {
if err := merge ( config , img . Config ) ; err != nil {
return err
}
}
2016-06-18 21:16:05 +00:00
// Reset the Entrypoint if it is [""]
if len ( config . Entrypoint ) == 1 && config . Entrypoint [ 0 ] == "" {
config . Entrypoint = nil
}
2016-05-24 15:49:26 +00:00
if len ( config . Entrypoint ) == 0 && len ( config . Cmd ) == 0 {
return fmt . Errorf ( "No command specified" )
}
return nil
}
2016-05-27 09:32:26 +00:00
// Checks if the client set configurations for more than one network while creating a container
2016-08-23 04:52:56 +00:00
// Also checks if the IPAMConfig is valid
2017-07-19 14:20:13 +00:00
func verifyNetworkingConfig ( nwConfig * networktypes . NetworkingConfig ) error {
2016-08-23 04:52:56 +00:00
if nwConfig == nil || len ( nwConfig . EndpointsConfig ) == 0 {
return nil
}
2019-03-20 09:39:28 +00:00
if len ( nwConfig . EndpointsConfig ) > 1 {
l := make ( [ ] string , 0 , len ( nwConfig . EndpointsConfig ) )
for k := range nwConfig . EndpointsConfig {
l = append ( l , k )
}
return errors . Errorf ( "Container cannot be connected to network endpoints: %s" , strings . Join ( l , ", " ) )
}
for k , v := range nwConfig . EndpointsConfig {
if v == nil {
return errdefs . InvalidParameter ( errors . Errorf ( "no EndpointSettings for %s" , k ) )
}
if v . IPAMConfig != nil {
if v . IPAMConfig . IPv4Address != "" && net . ParseIP ( v . IPAMConfig . IPv4Address ) . To4 ( ) == nil {
return errors . Errorf ( "invalid IPv4 address: %s" , v . IPAMConfig . IPv4Address )
2018-01-21 00:30:26 +00:00
}
2019-03-20 09:39:28 +00:00
if v . IPAMConfig . IPv6Address != "" {
n := net . ParseIP ( v . IPAMConfig . IPv6Address )
// if the address is an invalid network address (ParseIP == nil) or if it is
// an IPv4 address (To4() != nil), then it is an invalid IPv6 address
if n == nil || n . To4 ( ) != nil {
return errors . Errorf ( "invalid IPv6 address: %s" , v . IPAMConfig . IPv6Address )
2016-08-23 04:52:56 +00:00
}
}
}
2016-05-27 09:32:26 +00:00
}
2019-03-20 09:39:28 +00:00
return nil
2016-05-27 09:32:26 +00:00
}
2022-03-28 03:21:22 +00:00
// maximumSpec returns the distribution platform with maximum compatibility for the current node.
func maximumSpec ( ) v1 . Platform {
p := platforms . DefaultSpec ( )
if p . Architecture == "amd64" {
p . Variant = archvariant . AMD64Variant ( )
}
return p
}