简体   繁体   中英

No such image in gitlab-ci with docker-in-docker

I'm use docker-api in my rails project.

I'm need create containers with my custom image.

.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 

I'm got error:

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

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

How solve this problem?

Each job you specified will use a different instance of dind . You probably need to push the image from first job and pull it in the second job.

.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM