如何在Alpine内部使用Docker/Gitlab CI中的任何Docker图像

发布于 2025-02-09 22:37:30 字数 1449 浏览 1 评论 0原文

我想使用位于其他私人存储库中的Dockerfile构建和测试我的应用程序。 为此,我正在使用Alpine官方Docker Image,其中我运行了一个bash脚本来克隆我的私人仓库并运行Docker来构建Docker Image。这就是我的.gitlab-ci.yml的样子。

    image: alpine:3.15
stages:
  - main

main-job:
  stage: main
  script:
    - apk add --update docker openrc
    - rc-update add docker boot
    - apk add bash git curl
    - bash build.sh $GH_TOKEN $REPO

我在build.sh中有简单的脚本,

git clone https://${GH_TOKEN}@github.com/${REPO} source
cd source || exit 1
docker container prune --force || true
docker build . --rm --force-rm --compress --no-cache=true --pull --file Dockerfile -t test-app
docker image ls
docker run --privileged --rm -i test-app

但是Docker不会启动和垃圾邮件错误。

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

还尝试使用ubuntu docker中的其他命令尝试服务start dockerdockerd服务restart docker等。 但是似乎没有什么可用的,因为我想我们不能在码头机内运行Docker。

我们可以有其他选择吗?

I want to build and test my app using dockerfile located in other private repository.
For that I'm using Alpine official docker image in which i run a bash script for cloning my private repo and running docker for building the docker image. This is how my .gitlab-ci.yml looks like.

    image: alpine:3.15
stages:
  - main

main-job:
  stage: main
  script:
    - apk add --update docker openrc
    - rc-update add docker boot
    - apk add bash git curl
    - bash build.sh $GH_TOKEN $REPO

And I have simple script in build.sh

git clone https://${GH_TOKEN}@github.com/${REPO} source
cd source || exit 1
docker container prune --force || true
docker build . --rm --force-rm --compress --no-cache=true --pull --file Dockerfile -t test-app
docker image ls
docker run --privileged --rm -i test-app

But Docker don't start and spams error.

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

Also tried with other command in ubuntu docker like service start docker , dockerd, service restart docker and others.
But nothing seems to works as i guess we can't run docker inside a docker or something.

Can we have any alternative idea to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情徒 2025-02-16 22:37:31

看起来您没有运行Docker代理。您可以通过添加以下内容来使用Docker服务中的Docker:

services:
  - docker:dind

有关更多信息,请参见构建Docker Images的Gitlab-Ci Docs: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

Looks like you don’t have a docker agent running. You can use the docker in docker service by adding the following:

services:
  - docker:dind

See the GitLab-ci docs on building docker images for more info: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

烂柯人 2025-02-16 22:37:31

我遇到了同样的问题。可能是您必须允许gitlab-runner在主机上。

sudo usermod -aG docker gitlab-runner

I've meet the same issue. May be you have to permit gitlab-runner on your host.

sudo usermod -aG docker gitlab-runner
若相惜即相离 2025-02-16 22:37:31

我建议您构建并将构建的图像推到Dockerhub。然后,您可以启动引用预制图像的容器。

I‘d suggest you build and push your built image to dockerhub. Then you can start the container referencing your prebuilt image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文