gitlab-ci中没有这样的图像与docker-in-docker

时间:2019-11-13 11:13:32

标签: ruby docker gitlab-ci docker-api docker-in-docker

我在Rails项目中使用docker-api

我需要使用自定义图片创建容器。

.gitlab-ci.yml:

  variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""
  DB_HOST: postgres
  RAILS_ENV: test

build:
  stage: build
  image: docker:19.03.0 
  services:
    - docker:19.03.0-dind
  script:
    - docker build -t my_image docker/my_image/.

rspec:
  image: ruby:2.6.3
  services:
    - postgres:10.1
    - docker:19.03.0-dind
  script:
    - export DOCKER_URL=tcp://docker:2375
    - cp config/database.yml.gitlab_ci config/database.yml
    - gem install bundler
    - bundle install
    - rails db:create
    - rails db:migrate
    - rspec 

我有错误:

Failure/Error:
  container = Docker::Container.create('Cmd' => ['tail', '-f', '/dev/null'],
                                            'Image' => 'my_image')

Docker::Error::NotFoundError:
  No such image: my_image:latest

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您指定的每个作业将使用dind的不同实例。 您可能需要从第一份工作中推入图像,然后从第二份工作中拉出图像。

答案 1 :(得分:0)

.gitlab-ci.yml:

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""
  DB_HOST: postgres
  RAILS_ENV: test

rspec:
  image: ruby:2.6.3
  services:
    - postgres:10.1
    - docker:19.03.0-dind
  script:
    - cp config/database.yml.gitlab_ci config/database.yml
    - gem install bundler
    - bundle install
    - export DOCKER_URL=tcp://docker:2375
    - rails db:create
    - rails db:migrate
    - rake create_image
    - rspec 

rubocop:
  image: ruby:2.6.3
  script:
    - gem install bundler
    - bundle install
    - bundle exec rubocop
  artifacts:
    expire_in: 10 min

create_image.rake

task :create_image do
  image = Docker::Image.build_from_dir('./docker/my_image/.')
  image.tag('repo' => 'my_image', 'tag' => 'latest', force: true)
end