从PHP脚本登录Docker Hub [英] Login to Docker Hub from PHP script
问题描述
作为CICD流程的一部分,我正在尝试从PHP脚本登录并推送Docker映像。这是代码:
I am trying to login and push Docker images from a PHP script as part of our CICD process. Here is the code:
<?php
include '../php/database.php';
$duser = 'username';
$dpass = 'password';
$dmail = 'email';
$tag = 'from system';
function tagImage($tag) {
$getImageID = "SELECT `imageID` FROM `docker_images` WHERE `tag` = :tag ";
$params = array(':tag' => $tag);
$results = dataQuery($getImageID, $params);
if(!empty($results)) {
$image = $results[0]['imageID'];
global $repo = $results[0]['repo']; // I know this is a bad idea, will change it when all else is working
$last = system("sudo docker tag -f $image $repo 2>&1", $retval);
}
return $retval;
}
$tagStatus = tagImage($tag);
if(0 == $tagStatus) {
echo '<pre>';
$login = system("sudo docker login --username=$duser", $retval);
var_dump($login);
var_dump($retval);
// push it real good
$last = system("sudo docker push $repo 2>&1", $retval1);
var_dump($last);
var_dump($retval1);
}
?>
这将返回以下内容:
string(0) ""
int(1)
The push refers to a repository [app/ap-name] (len: 1)
21d623eb89a9: Image push failed
Please login prior to push:
Username: EOF
string(13) "Username: EOF"
int(1)
推送失败,因为登录无法通过PHP脚本进行,但是,当我从命令行登录时,登录是成功的。
The push is failing because the login is not working from the PHP script, however, when I login from the command line the login is successful.
我在做什么错?我可以像这样用PHP登录Docker Hub吗?还是应该使用不同的技术?
What am I doing wrong? Can I login to Docker Hub with PHP like this? Or should the technique be different?
编辑::PHP脚本将通过AJAX进行调用,从而有效地像运行脚本一样运行从浏览器。我正在通过浏览器运行它以进行测试。
The PHP script will be called via AJAX, effectively making it run as if it were being run from the browser. I am running it from the browser for testing purposes.
推荐答案
登录名需要所有凭据,包括与之关联的密码和电子邮件地址Docker hub仓库:
The login requires all of the credentials including password and email address associated with the Docker hub repo:
$login = system("sudo docker login --username $duser --password $dpass --email $dmail 2>&1", $retval);
var_dump($login);
var_dump($retval);
使用此语法将返回以下(预期值):
Using this syntax returns the following (expected):
WARNING: login credentials saved in /root/.dockercfg.
Login Succeeded
string(15) "Login Succeeded"
int(0)
过去指向 push
的位置正常工作,并且不返回任何错误。
Past that point the push
works properly and returns no errors.
这篇关于从PHP脚本登录Docker Hub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!