You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Uses an official Node.js image based on Alpine Linux, which is lightweight and efficient.
ARG ENV=development
Defines an argument that defaults to development. You can pass production during build time if needed.
ENV NODE_ENV $ENV
Sets the environment variable NODE_ENV to development or production based on the argument passed.
WORKDIR
Sets the working directory for your app in the container.
COPY package.json ./*
Copies only package.json and package-lock.json first, so that npm install runs faster by utilizing cached layers.
RUN npm install
Installs all dependencies defined in package.json.
COPY . .: Copies the rest of the project files into the container.
EXPOSE 3000: Exposes the port that your app will run on (adjust if needed).
CMD ["npm", "start"]
The default command to start the app. Make sure npm start is configured in package.json
Docker Registry Setup
Step 1: Pull the Docker Registry Image
sudo docker pull registry:2
This will download the latest version of the official Docker registry image from Docker Hub.
Step 2: Run the Docker Registry Container
To start a local Docker registry on port 5000, use the following command:
docker run -d -p 5000:5000 --name registry registry:2
Explanation:
-d: Run in detached mode
-p 5000:5000: Maps port 5000 on local machine to port 5000 inside the container (-p 5000:5000).
--name registry: Name the container 'registry
Uses the Docker registry version 2 image (registry:2).
Step 3: Verify the Registry is Running
Check if the registry is up and running:
sudo docker ps
We should see a container with the name registry running on port 5000.
Inspect logs for issues: sudo docker logs registry
Run Jenkins inside a Docker container
1. Pull the Jenkins Docker Image
First, we'll need to pull the official Jenkins Docker image from Docker Hub.
Jenkins provides an official image that we can easily use.
To do this, open the terminal and run the following command: docker pull jenkins/jenkins:lts
jenkins/jenkins:lts: This is the official Jenkins image, and lts stands for the Long-Term Support version of Jenkins, which is recommended for stability and security.
This command will download the Jenkins image to local machine.
2. Create a Jenkins Container
Once the Jenkins image has been downloaded, we can run it in a container.
We need to use the following command to start Jenkins:
1. docker run -d: This runs the Jenkins container in detached mode (in the background).
2. --name jenkins: Assigns the name jenkins to the container for easier management.
3. -p 8080:8080: Maps port 8080 on your host machine to port 8080 in the container.
Jenkins’ web UI will be accessible on port 8080.
#####4. -p 50000:50000: Maps port 50000 for Jenkins agents to communicate with the Jenkins master.
5. --volume jenkins_home:/var/jenkins_home
This mounts a persistent volume to store Jenkins data (e.g., job configurations, plugins, and build history). The volume is named jenkins_home.
#####6. jenkins/jenkins:lts: The Docker image you're using for the Jenkins container (LTS version).
Unlock Jenkins
When we first access Jenkins, it will ask for an unlock key.
To find this key, we need to run the following command to get the container’s logs:
docker logs jenkins
Manage Jenkins (Start/Stop)
Start Jenkins: If you stop the Jenkins container, you can restart it using: docker start jenkins
Stop Jenkins: To stop the container, run: docker stop jenkins
Remove Jenkins container: docker rm jenkins
To view Jenkins logs for debugging purposes, use: docker logs jenkins
Jenkins Pipeline for Docker Image Automation
This Jenkins pipeline automates the process of building, tagging, pushing, deploying, and testing a Docker image from a Git repository. The pipeline is designed to be flexible by using parameterized inputs, making it easy to customize the build and deployment process.
Pipeline Structure
The pipeline consists of the following key components:
1. Agent Definition:
pipeline {
agent any
Uses agent any to execute the pipeline on any available Jenkins agent.
2. Parameterized Inputs:
parameters {
string(name: 'BRANCH', description: 'Branch to build')