Sure! Here is a step-by-step tutorial on how to dockerize a GitLab CI/CD pipeline:
Step 1: Set up a GitLab repository
– Create a new repository on GitLab or use an existing one.
– Clone the repository to your local machine.
Step 2: Create a Dockerfile
– In the root directory of your repository, create a file named "Dockerfile".
– Open the Dockerfile in a text editor and add the following content:
“`
# Use an official Python runtime as the base image
FROM python:3.8
# Set the working directory in the container
WORKDIR /app
# Copy the requirements file to the working directory
COPY requirements.txt .
# Install the dependencies
RUN pip install –no-cache-dir -r requirements.txt
# Copy the rest of the code to the working directory
COPY . .
# Set the command to run when the container starts
CMD [ "python", "app.py" ]
“`
Step 3: Create a .gitlab-ci.yml file
– In the root directory of your repository, create a file named ".gitlab-ci.yml".
– Open the file in a text editor and add the following content:
“`
stages:
– build
– test
– deploy
build:
stage: build
image: docker:latest
services:
– docker:dind
script:
– docker build -t myapp .
– docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
– docker tag myapp $CI_REGISTRY_IMAGE/myapp:$CI_COMMIT_REF_NAME
– docker push $CI_REGISTRY_IMAGE/myapp:$CI_COMMIT_REF_NAME
test:
stage: test
image: docker:latest
services:
– docker:dind
script:
– docker pull $CI_REGISTRY_IMAGE/myapp:$CI_COMMIT_REF_NAME
– docker run $CI_REGISTRY_IMAGE/myapp:$CI_COMMIT_REF_NAME python -m unittest discover
deploy:
stage: deploy
image: docker:latest
services:
– docker:dind
script:
– docker pull $CI_REGISTRY_IMAGE/myapp:$CI_COMMIT_REF_NAME
– docker run -d -p 80:80 $CI_REGISTRY_IMAGE/myapp:$CI_COMMIT_REF_NAME
“`
Step 4: Commit and push the Dockerfile and .gitlab-ci.yml
– Add the Dockerfile and .gitlab-ci.yml to the repository using the following commands:
“`
git add Dockerfile .gitlab-ci.yml
git commit -m "Add Dockerfile and .gitlab-ci.yml"
git push origin master
“`
Step 5: Set up GitLab CI/CD
– Go to your GitLab repository and navigate to "Settings" > "CI/CD".
– Enable the CI/CD pipeline and save the changes.
That’s it! Your GitLab CI/CD pipeline is now dockerized. Whenever you push changes to your repository, GitLab will automatically build, test, and deploy your application using Docker.