How do I run my docker image from .gitlab-ci.yml after building the image?

8/10/2020

I followed how to builf the docker image from: https://gitlab.com/guided-explorations/containers/kaniko-docker-build/-/blob/master/.gitlab-ci.yml

The job works and throws no error when tested using gitlab from browser.

But now I want to be able to run the image as a container with some command

The build section in .gitlab-ci.yml

build-repo:
  extends: .build_with_kaniko
  environment:
    name: push-to-repo-registry
  tags:
    - shared-runner-tag

What do I need to add to the .gitlab-ci.yml file to be able to run the image as a container with a command like I would from the terminal:

$sudo docker run --name <image_instance> -i -t <image_name> [some command]

I'm just looking for a quick solution on how to run the image as a container. Every guide with regards to kaniko (I have to use it for my case) only speaks about building the image and pushing it to docker and the gitlab registry, but never on how to run it.

Thank You

-- it2901
continuous-integration
docker
gitlab
kubernetes

1 Answer

8/11/2020

In .gitlab-ci.yml, you can execute docker command on remote server using option -H or environment variable DOCKER_HOST. You can find the documentation to enable TLS and execute command on remote host here.

export DOCKER_HOST=tcp://<host>:<port>
export DOCKER_CERT_PATH=<certificate_dir>
export DOCKER_TLS_VERIFY=1
docker run -d <image>

EDIT: Of course, you need to have docker installed in your gitlab runner.

-- Cyril G.
Source: StackOverflow