Push Docker Image to GitLab Container Registry [ 7 Steps]

In this article we are going to cover push docker image to gitlab container registry | How to Push a Dockerized Node.js App to GitLab Container Registry.

Prerequisites:

  • GitLab Account
  • Linux or Windows Instance with GitLab Runner Installed

Step #1:Install GitLab Runner and Register Gitlab Runner with NodeJS GitLab Project

Please find below article to Install and Register GitLab Runner with GitLab on Linux and Windows

How to Install GitLab Runner on Ubuntu 20.04 LTS

How to Install GitLab Runner on Windows [2 Steps]

Step #2:Install Docker on GitLab Runner | Docker to build Docker images in GitLab CI

To use Docker in your GitLab CI you have to configure Docker with GitLab Runner, Below are options to use Docker with GitLab CI

  • Shell Executor in GitLab Runner
  • Docker-in-Docker
  • Docker Socket Binding

To use Shell Executor in GitLab Runner install docker on GitLab Runner or select docker executor on GitLab Runner

How to Install Docker on Ubuntu 19.10/18.04/16.04 LTS

Please follow official GitLab CI official page for more information

Step #3:Create node.js Hello world application

In this step we need to create node.js code Hello world code using below code

Create package.json file

package.json

{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "First Last <[email protected]>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.18.2"
  }
}

Create server.js file

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(PORT, HOST, () => {
  console.log(`Running on http://${HOST}:${PORT}`);
});

Create nodejs Dockerfile

A Dockerfile for a Node.js application allows you to create a container image that includes all the necessary dependencies and configurations to run your Node.js application. Below is an example of a simple Dockerfile for a Node.js application:

# Use the official Node.js image as the base image
FROM node:18

# Set the working directory inside the container
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install Node.js dependencies
RUN npm install
# If you are building your code for production
# RUN npm ci --omit=dev

# Bundle app source
COPY . .

# Expose the port on which your Node.js application will run
EXPOSE 8080

# Command to run your Node.js application
CMD [ "node", "server.js" ]

Create .dockerignore file

node_modules
npm-debug.log

Here’s a breakdown of each step:

  1. FROM node:18: This sets the base image for the container. In this case, we are using the official Node.js image with version 14. You can choose a different version based on your application’s requirements.
  2. WORKDIR /usr/src/app: This sets the working directory inside the container to /usr/src/app. It is a good practice to use a consistent location for your application files.
  3. COPY package*.json ./: This copies the package.json and package-lock.json files from the host (where your Dockerfile is located) to the working directory inside the container.
  4. RUN npm install: This installs the Node.js dependencies based on the package.json file. It is executed during the image build process, ensuring the application’s dependencies are installed within the container.
  5. COPY . .: This copies all the remaining application files from the host to the working directory inside the container.
  6. EXPOSE 3000: This exposes port 3000 on the container. If your Node.js application runs on a different port, modify this value accordingly.
  7. CMD ["npm", "start"]: This specifies the command that will be executed when the container starts. In this case, it runs npm start, which should be defined in your package.json as the start script for your Node.js application.

Step #4:Authenticating GitLab Container Registry

Below are ways to authenticate with Gitlab Container Registry, Please follow official Authenticate with the Container Registry gitlab

  • Personal access token.
  • Deploy token
  • Project access token
  • Group access token

In this task we are going to use Deploy to enable authentication with GitLab Container Registry, Please follow below instructions to create Deploy Token

Create a deploy token in Gitlab Project

Deploy tokens in GitLab are tokens specifically designed for authentication during the deployment process, allowing secure access to repositories and registries. They have limited permissions and are often used for automated deployment scripts and systems.

  1. Log in to GitLab: Log in to your GitLab account that has access to the repository you want to deploy.
  2. Navigate to Your Project: Go to the project where you want to create the deploy token.
  3. Access Repository Settings: Click on the “Settings” tab in your project’s navigation menu.
  4. Click on Repository
Push Docker Image to GitLab Container Registry [ 7 Steps] 1
  1. Navigate to Deploy Tokens:click on “Expand Deploy Tokens.”
Push Docker Image to GitLab Container Registry [ 7 Steps] 2

Configure the Deploy Token: You’ll need to fill in some information for the deploy token:

Name: Choose a name for your deploy token. This is for your reference.

Expires at: You can set an expiration date for the token if desired.

Scopes: Choose the scope for the token. This defines what the token will have access to. For deployment purposes, you might need to select “read_repository” and “read_registry” scopes.

Generate Token: After configuring the token, click on the “Create deploy token” button. GitLab will generate a deploy token for you.

Push Docker Image to GitLab Container Registry [ 7 Steps] 3

Copy the Token: Once the token is generated, you will see the token’s value displayed on the screen. This is the only time the token will be shown, so make sure to copy it and keep it secure.

Now you can use this deploy token in your deployment scripts, CI/CD pipelines, or other automated systems to authenticate with your GitLab repository or Container Registry without using your personal access token or password.

Remember that deploy tokens are designed for specific purposes and have limited permissions, so they are more secure to use in automated processes compared to personal access tokens.

Step #5:Adding GitLab Container Registry Deploy token in GitLab Project Variables

add Deploy token username , secrets and Registry name in GitLab Project Variables.

Find GitLab Registry name for your project

You can find Gitlab Container Registry as shown in below screenshot

For Example for our Project Registry Name: registry.gitlab.com/devopshintkubernetes/push-docker-image-to-gitlab-registry

Push Docker Image to GitLab Container Registry [ 7 Steps] 4

add Below variables in GitLab CI

Push Docker Image to GitLab Container Registry [ 7 Steps] 5

Step #6:Push Docker Image to GitLab Container Registry

Below gitlab-ci.yaml to Push Docker Image to GitLab Container Registry.

default:
  before_script:
    - docker info

stages:
  - docker build
  - docker push
 

Build docker image:
  stage: docker build
  before_script:
    - docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY .
 
Push to registry:
  stage: docker push
  before_script:
    - docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
  script:
    - docker push $CI_REGISTRY

Run the GitLab CI Pipeline

Push Docker Image to GitLab Container Registry [ 7 Steps] 6

Please Navigate GitLab Registry in GitLab to check docker image.

Push Docker Image to GitLab Container Registry [ 7 Steps] 7

on above screenshot docker images pushed to GitLab Container Registry | push docker image to gitlab container registry.

Conclusion:

We have covered push docker image to gitlab container registry | How to Push a Dockerized Node.js App to GitLab Container Registry.

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap