2020-05-01 15:28:44 +02:00
/ *
2020-09-22 12:13:00 +02:00
Copyright 2020 Docker Compose CLI authors
2020-06-18 16:13:24 +02:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
2020-05-01 15:28:44 +02:00
* /
2020-05-04 10:28:42 +02:00
package run
2020-05-01 15:28:44 +02:00
import (
"context"
2020-05-13 10:27:39 +02:00
"fmt"
2020-07-02 15:43:16 +02:00
"io"
"os"
2020-10-29 02:11:08 +01:00
"time"
2020-05-01 15:28:44 +02:00
2020-07-02 15:43:16 +02:00
"github.com/containerd/console"
2020-05-01 15:28:44 +02:00
"github.com/spf13/cobra"
2020-05-04 10:28:42 +02:00
2020-09-07 13:23:06 +02:00
"github.com/docker/compose-cli/api/client"
2020-09-21 15:39:05 +02:00
"github.com/docker/compose-cli/api/containers"
2021-01-15 16:31:59 +01:00
"github.com/docker/compose-cli/api/context/store"
2021-01-15 16:24:00 +01:00
"github.com/docker/compose-cli/api/progress"
2020-08-21 17:24:53 +02:00
"github.com/docker/compose-cli/cli/options/run"
2020-05-01 15:28:44 +02:00
)
2020-05-04 23:00:21 +02:00
// Command runs a container
2020-09-21 15:39:05 +02:00
func Command ( contextType string ) * cobra . Command {
2020-05-15 17:52:19 +02:00
var opts run . Opts
2020-05-01 15:28:44 +02:00
cmd := & cobra . Command {
Use : "run" ,
Short : "Run a container" ,
2020-08-24 18:41:16 +02:00
Args : cobra . MinimumNArgs ( 1 ) ,
2020-05-01 15:28:44 +02:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-08-24 18:41:16 +02:00
if len ( args ) > 1 {
opts . Command = args [ 1 : ]
}
2020-10-27 01:57:35 +01:00
return runRun ( cmd . Context ( ) , args [ 0 ] , contextType , opts )
2020-05-01 15:28:44 +02:00
} ,
}
2020-08-24 18:41:16 +02:00
cmd . Flags ( ) . SetInterspersed ( false )
2020-05-01 15:28:44 +02:00
2020-05-15 17:52:19 +02:00
cmd . Flags ( ) . StringArrayVarP ( & opts . Publish , "publish" , "p" , [ ] string { } , "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT" )
2020-05-21 19:28:42 +02:00
cmd . Flags ( ) . StringVar ( & opts . Name , "name" , "" , "Assign a name to the container" )
2020-05-18 10:33:01 +02:00
cmd . Flags ( ) . StringArrayVarP ( & opts . Labels , "label" , "l" , [ ] string { } , "Set meta data on a container" )
2020-09-14 18:32:38 +02:00
cmd . Flags ( ) . StringArrayVarP ( & opts . Volumes , "volume" , "v" , [ ] string { } , "Volume. Ex: storageaccount/my_share[:/absolute/path/to/target][:ro]" )
2020-07-02 15:43:16 +02:00
cmd . Flags ( ) . BoolVarP ( & opts . Detach , "detach" , "d" , false , "Run container in background and print container ID" )
2020-06-24 09:44:47 +02:00
cmd . Flags ( ) . Float64Var ( & opts . Cpus , "cpus" , 1. , "Number of CPUs" )
cmd . Flags ( ) . VarP ( & opts . Memory , "memory" , "m" , "Memory limit" )
2020-07-07 00:48:34 +02:00
cmd . Flags ( ) . StringArrayVarP ( & opts . Environment , "env" , "e" , [ ] string { } , "Set environment variables" )
2020-10-16 18:03:47 +02:00
cmd . Flags ( ) . StringArrayVar ( & opts . EnvironmentFiles , "env-file" , [ ] string { } , "Path to environment files to be translated as environment variables" )
2020-11-06 10:37:53 +01:00
cmd . Flags ( ) . StringVarP ( & opts . RestartPolicyCondition , "restart" , "" , containers . RestartPolicyRunNo , "Restart policy to apply when a container exits (no|always|on-failure)" )
2020-10-27 01:57:35 +01:00
cmd . Flags ( ) . BoolVar ( & opts . Rm , "rm" , false , "Automatically remove the container when it exits" )
2020-10-29 02:11:08 +01:00
cmd . Flags ( ) . StringVar ( & opts . HealthCmd , "health-cmd" , "" , "Command to run to check health" )
cmd . Flags ( ) . DurationVar ( & opts . HealthInterval , "health-interval" , time . Duration ( 0 ) , "Time between running the check (ms|s|m|h) (default 0s)" )
2020-11-13 11:50:10 +01:00
cmd . Flags ( ) . IntVar ( & opts . HealthRetries , "health-retries" , 0 , "Consecutive failures needed to report unhealthy" )
2020-10-29 02:11:08 +01:00
cmd . Flags ( ) . DurationVar ( & opts . HealthStartPeriod , "health-start-period" , time . Duration ( 0 ) , "Start period for the container to initialize before starting " +
"health-retries countdown (ms|s|m|h) (default 0s)" )
cmd . Flags ( ) . DurationVar ( & opts . HealthTimeout , "health-timeout" , time . Duration ( 0 ) , "Maximum time to allow one check to run (ms|s|m|h) (default 0s)" )
2020-05-01 15:28:44 +02:00
2020-11-30 11:44:13 +01:00
if contextType == store . LocalContextType {
cmd . Flags ( ) . StringVar ( & opts . Platform , "platform" , os . Getenv ( "DOCKER_DEFAULT_PLATFORM" ) , "Set platform if server is multi-platform capable" )
}
2020-09-21 15:39:05 +02:00
if contextType == store . AciContextType {
cmd . Flags ( ) . StringVar ( & opts . DomainName , "domainname" , "" , "Container NIS domain name" )
}
2020-10-30 17:23:02 +01:00
switch contextType {
case store . LocalContextType :
default :
_ = cmd . Flags ( ) . MarkHidden ( "rm" )
}
2020-10-27 01:57:35 +01:00
2020-05-01 15:28:44 +02:00
return cmd
}
2020-10-27 01:57:35 +01:00
func runRun ( ctx context . Context , image string , contextType string , opts run . Opts ) error {
2020-10-30 17:23:02 +01:00
switch contextType {
case store . LocalContextType :
default :
if opts . Rm {
return fmt . Errorf ( ` flag "--rm" is not yet implemented for %q context type ` , contextType )
}
2020-10-27 01:57:35 +01:00
}
2020-05-01 15:28:44 +02:00
c , err := client . New ( ctx )
if err != nil {
return err
}
2020-05-07 04:58:04 +02:00
containerConfig , err := opts . ToContainerConfig ( image )
2020-05-01 15:28:44 +02:00
if err != nil {
return err
}
2020-09-14 10:01:59 +02:00
result , err := progress . Run ( ctx , func ( ctx context . Context ) ( string , error ) {
return containerConfig . ID , c . ContainerService ( ) . Run ( ctx , containerConfig )
2020-06-17 22:19:08 +02:00
} )
2020-07-02 15:43:16 +02:00
if err != nil {
return err
}
2020-07-09 10:50:48 +02:00
2020-07-02 15:43:16 +02:00
if ! opts . Detach {
var con io . Writer = os . Stdout
2020-07-09 10:50:48 +02:00
req := containers . LogsRequest {
Follow : true ,
}
2020-07-02 15:43:16 +02:00
if c , err := console . ConsoleFromFile ( os . Stdout ) ; err == nil {
2020-07-09 10:50:48 +02:00
size , err := c . Size ( )
if err != nil {
return err
}
req . Width = int ( size . Width )
2020-07-02 15:43:16 +02:00
con = c
}
2020-07-09 10:50:48 +02:00
req . Writer = con
2020-07-02 15:43:16 +02:00
return c . ContainerService ( ) . Logs ( ctx , opts . Name , req )
2020-06-18 12:10:36 +02:00
}
2020-07-09 10:50:48 +02:00
2020-09-14 10:01:59 +02:00
fmt . Println ( result )
2020-07-02 15:43:16 +02:00
return nil
2020-05-01 15:28:44 +02:00
}