Since a few months ago this is possible using gitlab-runner:

gitlab-runner exec docker my-job-name

Note that you need both docker and gitlab-runner installed on your computer to get this working.

You also need the image key defined in your .gitlab-ci.yml file. Otherwise won't work.

Here's the line I currently use for testing locally using gitlab-runner:

gitlab-runner exec docker test --docker-volumes "/home/elboletaire/.ssh/id_rsa:/root/.ssh/id_rsa:ro"

Note: You can avoid adding a --docker-volumes with your key setting it by default in /etc/gitlab-runner/config.toml. See the official documentation for more details. Also, use gitlab-runner exec docker --help to see all docker-based runner options (like variables, volumes, networks, etc.).

Due to the confusion in the comments, I paste here the gitlab-runner --help result, so you can see that gitlab-runner can make builds locally:

   gitlab-runner --help
NAME:
   gitlab-runner - a GitLab Runner

USAGE:
   gitlab-runner [global options] command [command options] [arguments...]
   
VERSION:
   1.1.0~beta.135.g24365ee (24365ee)
   
AUTHOR(S):
   Kamil Trzciński <[email protected]> 
   
COMMANDS:
   exec         execute a build locally
   [...]
   
GLOBAL OPTIONS:
   --debug          debug mode [$DEBUG]
   [...]

As you can see, the exec command is to execute a build locally.

Even though there was an issue to deprecate the current gitlab-runner exec behavior, it ended up being reconsidered and a new version with greater features will replace the current exec functionality.

Note that this process is to use your own machine to run the tests using docker containers. This is not to define custom runners. To do so, just go to your repo's CI/CD settings and read the documentation there. If you wanna ensure your runner is executed instead of one from gitlab.com, add a custom and unique tag to your runner, ensure it only runs tagged jobs and tag all the jobs you want your runner to be responsible of.

Unofficial alternative gitlab-ci-local

There's an unofficial package which looks promising, with more features than the official gitlab-runner exec: https://github.com/firecow/gitlab-ci-local

It allows you to run gitlab pipelines locally as shell executor or docker executor.

Answer from elboletaire on Stack Overflow
Top answer
1 of 11
237

Since a few months ago this is possible using gitlab-runner:

gitlab-runner exec docker my-job-name

Note that you need both docker and gitlab-runner installed on your computer to get this working.

You also need the image key defined in your .gitlab-ci.yml file. Otherwise won't work.

Here's the line I currently use for testing locally using gitlab-runner:

gitlab-runner exec docker test --docker-volumes "/home/elboletaire/.ssh/id_rsa:/root/.ssh/id_rsa:ro"

Note: You can avoid adding a --docker-volumes with your key setting it by default in /etc/gitlab-runner/config.toml. See the official documentation for more details. Also, use gitlab-runner exec docker --help to see all docker-based runner options (like variables, volumes, networks, etc.).

Due to the confusion in the comments, I paste here the gitlab-runner --help result, so you can see that gitlab-runner can make builds locally:

   gitlab-runner --help
NAME:
   gitlab-runner - a GitLab Runner

USAGE:
   gitlab-runner [global options] command [command options] [arguments...]
   
VERSION:
   1.1.0~beta.135.g24365ee (24365ee)
   
AUTHOR(S):
   Kamil Trzciński <[email protected]> 
   
COMMANDS:
   exec         execute a build locally
   [...]
   
GLOBAL OPTIONS:
   --debug          debug mode [$DEBUG]
   [...]

As you can see, the exec command is to execute a build locally.

Even though there was an issue to deprecate the current gitlab-runner exec behavior, it ended up being reconsidered and a new version with greater features will replace the current exec functionality.

Note that this process is to use your own machine to run the tests using docker containers. This is not to define custom runners. To do so, just go to your repo's CI/CD settings and read the documentation there. If you wanna ensure your runner is executed instead of one from gitlab.com, add a custom and unique tag to your runner, ensure it only runs tagged jobs and tag all the jobs you want your runner to be responsible of.

Unofficial alternative gitlab-ci-local

There's an unofficial package which looks promising, with more features than the official gitlab-runner exec: https://github.com/firecow/gitlab-ci-local

It allows you to run gitlab pipelines locally as shell executor or docker executor.

2 of 11
189

Deprecated since 2024-05 (see edit)

Only for gitlab-runner < 17.0.

  • See gitlab issue.
  • See gitlab docu

I use this docker-based approach:

Edit: 2024-05

docker run --entrypoint bash --rm -w "$PWD" -v "PWD" -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:v15.11.1 -c 'git config --global --add safe.directory "*";gitlab-runner exec docker test'

Thanks to user Ivo Smits who reported a successful test with gitlab-runner:v15.11.1

Edit: 2022-10

For all git versions > 2.35.2. You must add safe.directory within the container to avoid fatal: detected dubious ownership in repository at.... This also true for patched git versions < 2.35.2. The old command will not work anymore.

Details

0. Create a git repo to test this answer

mkdir my-git-project
cd my-git-project
git init
git commit --allow-empty -m"Initialize repo to showcase gitlab-runner locally."

1. Go to your git directory

cd my-git-project

2. Create a .gitlab-ci.yml

Example .gitlab-ci.yml

image: alpine

test:
  script:
    - echo "Hello Gitlab-Runner"

3. Create a docker container with your project dir mounted

docker run -d \
  --name gitlab-runner \
  --restart always \
  -v "PWD" \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

(-d) run container in background and print container ID

(--restart always) or not?

(-v "PWD") Mount current directory into the current directory of the container - Note: On Windows you could bind your dir to a fixed location, e.g. -v "${PWD}:/opt/myapp". Also $PWD will only work at powershell not at cmd

(-v /var/run/docker.sock:/var/run/docker.sock) This gives the container access to the docker socket of the host so it can start "sibling containers" (e.g. Alpine).

(gitlab/gitlab-runner:latest) Just the latest available image from dockerhub.

4. Execute with

Avoid fatal: detected dubious ownership in repository at... More info

docker exec -it -w $PWD gitlab-runner git config --global --add safe.directory "*"

Actual execution

docker exec -it -w $PWD gitlab-runner gitlab-runner exec docker test
#                ^          ^           ^            ^     ^      ^
#                |          |           |            |     |      |
#               (a)        (b)         (c)          (d)   (e)    (f)

(a) Working dir within the container. Note: On Windows you could use a fixed location, e.g. /opt/myapp.

(b) Name of the docker container

(c) Execute the command "gitlab-runner" within the docker container

(d)(e)(f) run gitlab-runner with "docker executer" and run a job named "test"

5. Prints

...
Executing "step_script" stage of the job script
$ echo "Hello Gitlab-Runner"
Hello Gitlab-Runner
Job succeeded
...

Note: The runner will only work on the commited state of your code base. Uncommited changes will be ignored. Exception: The .gitlab-ci.yml itself does not have be commited to be taken into account.

Note: There are some limitations running locally. Have a look at limitations of gitlab runner locally.

🌐
GitLab
docs.gitlab.com › runner
GitLab Runner | GitLab Docs
Runner: A configured instance of GitLab Runner that can execute jobs. Depending on the type of executor, this machine could be local to the runner manager (shell or docker executor) or a remote machine created by an autoscaler (docker-autoscaler or kubernetes).
🌐
GitLab
docs.gitlab.com › runner › install
Install GitLab Runner | GitLab Docs
For security and performance reasons, install GitLab Runner on a machine separate from the machine that hosts your GitLab instance.
🌐
GitHub
github.com › firecow › gitlab-ci-local
GitHub - firecow/gitlab-ci-local: Tired of pushing to test your .gitlab-ci.yml? · GitHub
Run gitlab pipelines locally as shell executor or docker executor.
Starred by 3.8K users
Forked by 202 users
Languages   TypeScript 96.7% | JavaScript 3.0%
🌐
GitLab
forum.gitlab.com › gitlab ci/cd
How to setup both GitLab and Runner on local machine correctly? - GitLab CI/CD - GitLab Forum
July 5, 2023 - To accomplish this, I decided to setup 2 containers on my local machine (GitLab Instance and Runner) via docker compose and it worked fine until I tried to run my job. Problem: job (runner container) can’t see localhost hosted git repo Job error: Docker compose YAML: version: '3.6' services: ...
🌐
GitLab
docs.gitlab.com › runner › commands
GitLab Runner commands | GitLab Docs
When GitLab Runner is installed on a host and runs local executors, it starts additional processes for operations like downloading or uploading artifacts, or handling cache. These processes are executed as gitlab-runner commands, which means that you can use pkill -QUIT gitlab-runner or killall QUIT gitlab-runner to kill them.
🌐
GitLab
docs.gitlab.com › tutorials › create_register_first_runner
Tutorial: Create, register, and run your own project runner | GitLab Docs
July 5, 2023 - Before you can create, register, and run a runner, you must install GitLab Runner on a local computer.
🌐
BrowserStack
browserstack.com › home › guide › how to run test on gitlab ci locally
How to run test on GitLab CI Locally | BrowserStack
May 30, 2023 - When your gitlab-ci.yml configuration file is added to the repository, GitLab can ascertain it and execute your scripts with the GitLab Runner app. This app functions precisely like your terminal and predominantly helps you reproduce production-like scripts. ... # Adding variables to set maven local environment variables: MAVEN_OPTS: -Dmaven.repo.local=.m2/repository # Docker image to use latest maven distribution image: maven:latest # setting up maven lifecycle stages stages: - build - test - package - deploy # setting up cache paths cache: paths: - .m2/repository - target build_job: stage: b
Find elsewhere
🌐
GitLab
docs.gitlab.com › runner › register
Registering runners | GitLab Docs
sudo -u gitlab-runner -H /usr/local/bin/gitlab-runner register --non-interactive \ --url "https://gitlab.com/" \ --registration-token "$PROJECT_REGISTRATION_TOKEN" \ --executor "docker" \ --docker-image alpine:latest \ --description "docker-runner" \ --maintenance-note "Free-form maintainer notes about this runner" \ --tag-list "docker,aws" \ --run-untagged="true" \ --locked="false" \ --access-level="not_protected"
🌐
Testmuai
testmuai.com › testmu ai › blog › how to use gitlab ci to run tests locally | testmu ai
How To Use GitLab CI To Run Tests Locally | TestMu AI (Formerly LambdaTest)
4 days ago - That is why it is super helpful to run the CI docker image locally, to iron out quirks based on the rest of the world. And by running the GitLab CI Runner itself, you can do just that. It will pick up the .gitlab-ci.yml of your project and run it via Docker locally.
🌐
GitHub
github.com › daaru00 › gitlab-runners-local
GitHub - daaru00/gitlab-runners-local: Run Gitlab runners locally · GitHub
version: '3' services: project1: build: . container_name: "gitlab-runner-project1" #change this with your repository name environment: REGISTRATION_TOKEN: "<registration token here>" #add repository-specific registration token volumes: - /var/run/docker.sock:/var/run/docker.sock project2: build: .
Author   daaru00
🌐
DEV Community
dev.to › ishmam_abir › cicd-on-local-gitlab-server-setup-gitlab-runner-self-hosted-gitlab-37nf
CI/CD on Local Gitlab server| Setup GitLab Runner | Self-hosted GitLab - DEV Community
November 15, 2025 - Our runner container is running, but it's not authenticated with our server. We need to "register" it. ... Log into your local GitLab server (http://localhost:8080) as the root user.
🌐
Baeldung
baeldung.com › home › devops › guide to using gitlab ci to run tests locally
Guide to Using GitLab CI to Run Tests Locally | Baeldung on Ops
January 31, 2025 - Let’s install version 13.3.0 of the gitlab-runner tool. We can follow the install guide for our operating system and replace the latest keyword with v13.3.0 in the download path: $ sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/v13.3.0/bi...
🌐
GitLab
docs.gitlab.com › runner › install › osx
Install GitLab Runner on macOS | GitLab Docs
The gitlab-runner install command creates a LaunchAgent plist at ~/Library/LaunchAgents/gitlab-runner.plist and registers it with launchctl. If you encounter errors, see troubleshooting. For more information about configuration options, see advanced configuration. ... sudo curl -o /usr/local/bin/gitlab-runner \ "https://s3.dualstack.us-east-1.amazonaws.com/gitlab-runner-downloads/latest/binaries/gitlab-runner-darwin-amd64"
🌐
GitLab
forum.gitlab.com › gitlab ci/cd
Gitlab-runner local build - GitLab CI/CD - GitLab Forum
August 13, 2024 - I am trying to followed this example: https://stackoverflow.com/questions/32933174/use-gitlab-ci-to-run-tests-locally The above suggests: gitlab-runner exec docker my-job-name I am using GITLAB-RUNNER VERSION: 17.2.0 (6428c288) I do not use docker and I do not want to use docker.
🌐
Medium
medium.com › @tahitoa.merlin › test-a-gitlab-pipeline-ci-cd-in-local-858667772c24
Test a GitLab pipeline CI/CD in local | by Tahitoa Merlin | Medium
June 5, 2024 - Connect to the GitLab instance running at the URL : http://localhost · You should be able to reach it, and create a new project, create the pipeline file: .gitlab-ci.yml (by default). From the admin area, or for a project specific only, go into the CI/CD settings: ... Maybe you’ve a page saying that you cannot access the url, just edit the /etc/hosts and add an entry to redirect the URL filled in the compose file, and recreate the runner (or change the URL by the localhost).
🌐
Reddit
reddit.com › r/gitlab › running the entire gitlab ci pipeline locally with external gitlab runners
r/gitlab on Reddit: Running the entire GitLab CI pipeline locally with external GitLab runners
November 13, 2022 -

Is there a way to run the entire GitLab CI pipeline on the local host, where it uses the same GitLab runners that are located on external compute resources, such as an external build server?

I know of the gitlab-runner exec command, but as I understand it, that command uses the local host as the GitLab runner. Is there a way of using the command the same way, but with the external GitLab runners that are already used in the CI pipeline when pushing to the cloud? A thought that came to mind is to use a script to ssh into an external host and then use the GitLab runner command to perform all jobs in the gitlab-ci.yml file. However, this will require the local host to link the repository to the external host somehow before executing the pipeline. This method also seems tedious when having multiple external computers with a GitLab runner. Does anyone have any better suggestions?

🌐
GitLab
gitlab.com › gitlab.org › gitlab-runner › #1359
How to run gitlab runner full localy? (#1359) · Issues · GitLab.org / gitlab-runner · GitLab
May 30, 2016 - I want to test some gitlab runner features related to my project, but I cant without committing and pushing changes to a repository. Every time after firing a...
🌐
Medium
medium.com › @umutuluer › how-to-test-gitlab-ci-locally-f9e6cef4f054
How to Test Gitlab Ci Locally. I have a gitlab-ci.yml file and I want… | by Umut Uluer | Medium
October 31, 2018 - These will show in your Project Settings > CI / CD > Runners tab. Now, lets create gitlab-ci.yml file in project root directory. I will use two stage in this section. Test and deploy. Also, I will define cache file locations, variables that used by script and lastly, commands are in stages. stages: - test - deploy cache: paths: - vendor/ - node_modules/ - storage/framework/cache variables: DEPLOYMENT_PATH: /usr/local/my_project PROJECT_ROOT: /home/gitlab-runner/my_project my_project_test: stage: test script: - composer install - php vendor/bin/phpunit --colors my_project_deploy: stage: deploy script: - rsync -a -e 'ssh -p 1881' --delete-before -r ${PROJECT_ROOT}/* root@$STAGE_SERVER:${DEPLOYMENT_PATH}