2 namespace Robo\Task\Docker;
4 use Robo\Common\CommandReceiver;
7 * Performs `docker run` on a container.
11 * $this->taskDockerRun('mysql')->run();
13 * $result = $this->taskDockerRun('my_db_image')
14 * ->env('DB', 'database_name')
15 * ->volume('/path/to/data', '/data')
21 * // retrieve container's cid:
22 * $this->say("Running container ".$result->getCid());
24 * // execute script inside container
25 * $result = $this->taskDockerRun('db')
26 * ->exec('prepare_test_data.sh')
29 * $this->taskDockerCommit($result)
34 * $mysql = $this->taskDockerRun('mysql')
35 * ->name('wp_db') // important to set name for linked container
36 * ->env('MYSQL_ROOT_PASSWORD', '123456')
39 * $this->taskDockerRun('wordpress')
49 class Run extends Base
56 protected $image = '';
79 * @param string $image
81 public function __construct($image)
83 $this->image = $image;
89 public function getPrinted()
91 return $this->isPrinted;
97 public function getCommand()
99 if ($this->isPrinted) {
102 if ($this->cidFile) {
103 $this->option('cidfile', $this->cidFile);
105 return trim('docker run ' . $this->arguments . ' ' . $this->image . ' ' . $this->run);
111 public function detached()
120 public function interactive($interactive = true)
125 return parent::interactive($interactive);
129 * @param string|\Robo\Contract\CommandInterface $run
133 public function exec($run)
135 $this->run = $this->receiveCommand($run);
140 * @param string $from
141 * @param null|string $to
145 public function volume($from, $to = null)
147 $volume = $to ? "$from:$to" : $from;
148 $this->option('-v', $volume);
153 * Set environment variables.
154 * n.b. $this->env($variable, $value) also available here,
155 * inherited from ExecTrait.
160 public function envVars(array $env)
162 foreach ($env as $variable => $value) {
163 $this->setDockerEnv($variable, $value);
169 * @param string $variable
170 * @param null|string $value
174 protected function setDockerEnv($variable, $value = null)
176 $env = $value ? "$variable=$value" : $variable;
177 return $this->option("-e", $env);
181 * @param null|int $port
182 * @param null|int $portTo
186 public function publish($port = null, $portTo = null)
189 return $this->option('-P');
192 $port = "$port:$portTo";
194 return $this->option('-p', $port);
202 public function containerWorkdir($dir)
204 return $this->option('-w', $dir);
208 * @param string $user
212 public function user($user)
214 return $this->option('-u', $user);
220 public function privileged()
222 return $this->option('--privileged');
226 * @param string $name
230 public function name($name)
233 return $this->option('name', $name);
237 * @param string|\Robo\Task\Docker\Result $name
238 * @param string $alias
242 public function link($name, $alias)
244 if ($name instanceof Result) {
245 $name = $name->getContainerName();
247 $this->option('link', "$name:$alias");
256 public function tmpDir($dir)
265 public function getTmpDir()
267 return $this->dir ? $this->dir : sys_get_temp_dir();
273 public function getUniqId()
281 public function run()
283 $this->cidFile = $this->getTmpDir() . '/docker_' . $this->getUniqId() . '.cid';
284 $result = parent::run();
285 $result['cid'] = $this->getCid();
290 * @return null|string
292 protected function getCid()
294 if (!$this->cidFile || !file_exists($this->cidFile)) {
297 $cid = trim(file_get_contents($this->cidFile));
298 @unlink($this->cidFile);