Sign In
Sign In

How to Automate Jenkins Setup with Docker

How to Automate Jenkins Setup with Docker
Hostman Team
Technical writer
Docker CI/CD
30.01.2025
Reading time: 19 min

In the modern software development world, Continuous Integration and Continuous Delivery (CI/CD) have become an integral part of the development process. Jenkins, one of the leading CI/CD tools, helps automate application build, testing, and deployment. However, setting up and managing Jenkins can be time-consuming and complex, especially in large projects with many developers and diverse requirements.

Docker, containerization, and container orchestration have come to the rescue, offering more efficient and scalable solutions for deploying applications and infrastructure. Docker allows developers to package applications and their dependencies into containers, which can be easily transported and run on any system with Docker installed.

Benefits of Using Docker for Automating Jenkins Setup

  • Simplified Installation and Setup: Using Docker to deploy Jenkins eliminates many challenges associated with installing dependencies and setting up the environment. You only need to run a few commands to get a fully functional Jenkins server.
  • Repeatability: With Docker, you can be confident that your environment will always be the same, regardless of where it runs. This eliminates problems associated with different configurations across different servers.
  • Environment Isolation: Docker provides isolation of applications and their dependencies, avoiding conflicts between different projects and services.
  • Scalability: Using Docker and orchestration tools such as Docker Compose or Kubernetes allows Jenkins to be easily scaled by adding or removing agents as needed.
  • Fast Deployment and Recovery: In case of failure or the need for an upgrade, Docker allows you to quickly deploy a new Jenkins container, minimizing downtime and ensuring business continuity.

In this article, we will discuss how to automate the setup and deployment of Jenkins using Docker. We will cover all the stages, from creating a Docker file and setting up Docker Compose to integrating Jenkins Configuration as Code (JCasC) for automatic Jenkins configuration. As a result, you'll have a complete understanding of the process and a ready-made solution for automating Jenkins in your projects.

Prerequisites

Before you begin setting up Jenkins with Docker, you need to ensure that you have all the necessary tools and software. In this section, we will discuss the requirements for successfully automating Jenkins and how to install the necessary components.

Installing Docker and Docker Compose

Docker can be installed on various operating systems, including Linux, macOS, and Windows. Below are the steps for installing Docker on the most popular platforms:

Linux (Ubuntu)

  1. Update the package list with the command:

sudo apt update
  1. Install packages for HTTPS support:

sudo apt install apt-transport-https ca-certificates curl software-properties-common
  1. Add the official Docker GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  1. Add the Docker repository to APT sources:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  1. Install Docker:

sudo apt install docker-ce
  1. Verify Docker is running:

sudo systemctl status docker

macOS

  1. Download and install Docker Desktop from the official website: Docker Desktop for Mac.
  2. Follow the on-screen instructions to complete the installation.

Windows

  1. Download and install Docker Desktop from the official website: Docker Desktop for Windows.
  2. Follow the on-screen instructions to complete the installation.

Docker Compose is typically installed along with Docker Desktop on macOS and Windows. For Linux, it requires separate installation:

  1. Download the latest version of Docker Compose:

sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -Po '"tag_name": "\K.*?(?=")')/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. Make the downloaded file executable:

sudo chmod +x /usr/local/bin/docker-compose
  1. Verify the installation:

docker-compose --version

Docker Hub is a cloud-based repository where you can find and store Docker images. The official Jenkins Docker image is available on Docker Hub and provides a ready-to-use Jenkins server.

  1. Go to the Docker Hub website.
  2. In the search bar, type Jenkins.
  3. Select the official image jenkins/jenkins.

Image9

The official image is regularly updated and maintained by the community, ensuring a stable and secure environment.

Creating a Dockerfile for Jenkins

In this chapter, we will explore how to create a Dockerfile for Jenkins that will be used to build a Docker image. We will also discuss how to add configurations and plugins to this image to meet the specific requirements of your project.

Structure of a Dockerfile

A Dockerfile is a text document containing all the commands that a user could call on the command line to build an image. In each Dockerfile, instructions are used to define a step in the image-building process. The key commands include:

  • FROM: Specifies the base image to create a new image from.
  • RUN: Executes a command in the container.
  • COPY or ADD: Copies files or directories into the container.
  • CMD or ENTRYPOINT: Defines the command that will be executed when the container starts.

Basic Dockerfile for Jenkins

Let’s start by creating a simple Dockerfile for Jenkins. This file will use the official Jenkins image as the base and add a few necessary plugins.

  1. Create a new file named Dockerfile in your project directory.
  2. Add the following code:
FROM jenkins/jenkins:lts

RUN jenkins-plugin-cli --plugins workflow-aggregator git

EXPOSE 8080
EXPOSE 50000

This basic Dockerfile installs two plugins: workflow-aggregator and git. It also exposes ports 8080 (for the web interface) and 50000 (for connecting Jenkins agents).

Adding Configurations and Plugins

For more complex configurations, we can add additional steps to the Dockerfile. For example, we can configure Jenkins to automatically use a specific configuration file or add scripts for pre-configuration.

  1. Create a jenkins_home directory to store custom configurations.

  2. Inside the new directory, create a custom_config.xml file with the required configurations:

<?xml version='1.0' encoding='UTF-8'?>
<hudson>
    <numExecutors>2</numExecutors>
    <mode>NORMAL</mode>
    <useSecurity>false</useSecurity>
    <disableRememberMe>false</disableRememberMe>
    <label></label>
    <primaryView>All</primaryView>
    <slaveAgentPort>50000</slaveAgentPort>
    <securityRealm class='hudson.security.SecurityRealm$None'/>
    <authorizationStrategy class='hudson.security.AuthorizationStrategy$Unsecured'/>
</hudson>
  1. Update the Dockerfile as follows:

FROM jenkins/jenkins:lts

RUN jenkins-plugin-cli --plugins workflow-aggregator git docker-workflow

COPY jenkins_home/custom_config.xml /var/jenkins_home/config.xml

COPY scripts/init.groovy.d /usr/share/jenkins/ref/init.groovy.d/

EXPOSE 8080
EXPOSE 50000

In this example, we are installing additional plugins, copying the custom configuration file into Jenkins, and adding scripts to the init.groovy.d directory for automatic initialization of Jenkins during its first startup.

Docker Compose Setup

Docker Compose allows you to define your application's infrastructure as code using YAML files. This simplifies the configuration and deployment process, making it repeatable and easier to manage.

Key benefits of using Docker Compose:

  • Ease of Use: Create and manage multi-container applications with a single YAML file.
  • Scalability: Easily scale services by adding or removing containers as needed.
  • Convenience for Testing: Ability to run isolated environments for development and testing.

Example of docker-compose.yml for Jenkins

Let’s create a docker-compose.yml file to deploy Jenkins along with associated services such as a database and Jenkins agent.

  1. Create a docker-compose.yml file in your project directory.
  2. Add the following code to the file:
version: '3.8'

services:
  jenkins:
    image: jenkins/jenkins:lts
    container_name: jenkins-server
    ports:
      - "8080:8080"
      - "50000:50000"
    volumes:
      - jenkins_home:/var/jenkins_home
    networks:
      - jenkins-network

  jenkins-agent:
    image: jenkins/inbound-agent
    container_name: jenkins-agent
    environment:
      - JENKINS_URL=http://jenkins-server:8080
      - JENKINS_AGENT_NAME=agent
      - JENKINS_AGENT_WORKDIR=/home/jenkins/agent
    volumes:
      - agent_workdir:/home/jenkins/agent
    depends_on:
      - jenkins
    networks:
      - jenkins-network

volumes:
  jenkins_home:
  agent_workdir:

networks:
  jenkins-network:

This file defines two services:

  • jenkins: The service uses the official Jenkins image. Ports 8080 and 50000 are forwarded for access to the Jenkins web interface and communication with agents. The /var/jenkins_home directory is mounted on the external volume jenkins_home to persist data across container restarts.
  • jenkins-agent: The service uses the Jenkins inbound-agent image. The agent connects to the Jenkins server via the URL specified in the JENKINS_URL environment variable. The agent's working directory is mounted on an external volume agent_workdir.

Once you create the docker-compose.yml file, you can start all services with a single command:

  1. Navigate to the directory that contains your docker-compose.yml.
  2. Run the following command to start all services:
docker-compose up -d

The -d flag runs the containers in the background. After executing this command, Docker Compose will create and start containers for all services defined in the file.

You can now check the status of the running containers using the following command:

docker-compose ps

If everything went well, you should see only the jenkins-server container in the output.

Now, let’s set up the Jenkins server and agent.

  1. Open a browser and go to http://localhost:8080/. During the first startup, you will see the following message:

Image11

To retrieve the password, run this command:

docker exec -it jenkins-server cat /var/jenkins_home/secrets/initialAdminPassword
  1. Copy the password and paste it into the Unlock Jenkins form. This will open a new window with the initial setup.

  2. Select Install suggested plugins. After the installation is complete, fill out the form to create an admin user.

  3. Accept the default URL and finish the setup. Then, go to Manage JenkinsManage Nodes.

  4. Click New Node, provide a name for the new node (e.g., "agent"), and select Permanent Agent. Fill in the remaining fields as shown in the screenshot.

After creating the agent, a window will open with a command containing the secret for the agent connection.

Image10

  1. Copy the secret and add it to your docker-compose.yml:

environment:
  - JENKINS_URL=http://jenkins-server:8080
  - JENKINS_AGENT_NAME=agent
  - JENKINS_AGENT_WORKDIR=/home/jenkins/agent
  - JENKINS_SECRET=<your-secret-here>  # Insert the secret here

To restart the services, use the following commands and verify that the jenkins-agent container has started:

docker-compose down
docker-compose up -d

Image4

Configuring Jenkins with Code (JCasC)

Jenkins Configuration as Code (JCasC) is an approach that allows you to describe the entire Jenkins configuration in a YAML file. It simplifies the automation, maintenance, and portability of Jenkins settings. In this chapter, we will explore how to set up JCasC for automatic Jenkins configuration when the container starts.

JCasC allows you to describe Jenkins configuration in a single YAML file, which provides the following benefits:

  • Automation: A fully automated Jenkins setup process, eliminating the need for manual configuration.
  • Manageability: Easier management of configurations using version control systems.
  • Documentation: Clear and easily readable documentation of Jenkins configuration.

Example of a Jenkins Configuration File

First, create the configuration file.

  1. Create a file named jenkins.yaml in your project directory.

  2. Add the following configuration to the file:

jenkins:
  systemMessage: "Welcome to Jenkins configured as code!"
  securityRealm:
    local:
      allowsSignup: false
      users:
        - id: "admin"
          password: "${JENKINS_ADMIN_PASSWORD}"
  authorizationStrategy:
    loggedInUsersCanDoAnything:
      allowAnonymousRead: false
  tools:
    jdk:
      installations:
        - name: "OpenJDK 11"
          home: "/usr/lib/jvm/java-11-openjdk"
  jobs:
    - script: >
        pipeline {
          agent any
          stages {
            stage('Build') {
              steps {
                echo 'Building...'
              }
            }
            stage('Test') {
              steps {
                echo 'Testing...'
              }
            }
            stage('Deploy') {
              steps {
                echo 'Deploying...'
              }
            }
          }
        }

This configuration file defines:

  • System message in the systemMessage block. This string will appear on the Jenkins homepage and can be used to inform users of important information or changes.

  • Local user database and administrator account in the securityRealm block. The field allowsSignup: false disables self-registration of new users. Then, a user with the ID admin is defined, with the password set by the environment variable ${JENKINS_ADMIN_PASSWORD}.

  • Authorization strategy in the authorizationStrategy block. The policy loggedInUsersCanDoAnything allows authenticated users to perform any action, while allowAnonymousRead: false prevents anonymous users from accessing the system.

  • JDK installation in the tools block. In this example, a JDK named OpenJDK 11 is specified with the location /usr/lib/jvm/java-11-openjdk.

  • Pipeline example in the jobs block. This pipeline includes three stages: Build, Test, and Deploy, each containing one step that outputs a corresponding message to the console.

Integrating JCasC with Docker and Docker Compose

Next, we need to integrate our jenkins.yaml configuration file with Docker and Docker Compose so that this configuration is automatically applied when the Jenkins container starts.

  1. Update the Dockerfile to copy the configuration file into the container and install the JCasC plugin:

FROM jenkins/jenkins:lts

RUN jenkins-plugin-cli --plugins configuration-as-code

COPY jenkins.yaml /var/jenkins_home/jenkins.yaml

EXPOSE 8080
EXPOSE 50000
  1. Update the docker-compose.yml to set environment variables and mount the configuration file. Add the following code in the volumes block:

- ./jenkins.yaml:/var/jenkins_home/jenkins.yaml

After the volumes block, add a new environment block (if you haven't defined it earlier):

environment:
 - JENKINS_ADMIN_PASSWORD=admin_password
  1. Build the new Jenkins image with the JCasC configuration:

docker-compose build
  1. Run the containers:

docker-compose up -d

After the containers start, go to your browser at http://localhost:8080 and log in with the administrator account. You should see the system message and the Jenkins configuration applied according to your jenkins.yaml file.

A few important notes:

  • The YAML files docker-compose.yml and jenkins.yaml might seem similar at first glance but serve completely different purposes. The file in Docker Compose describes the services and containers needed to run Jenkins and its environment, while the file in JCasC describes the Jenkins configuration itself, including plugin installation, user settings, security, system settings, and jobs.

  • The .yml and .yaml extensions are variations of the same YAML file format. They are interchangeable and supported by various tools and libraries for working with YAML. The choice of format depends largely on historical community preferences; in Docker documentation, you will more often encounter examples with the .yml extension, while in JCasC documentation, .yaml is more common.

  • The pipeline example provided below only outputs messages at each stage with no useful payload. This example is for demonstrating structure and basic concepts, but it does not prevent Jenkins from successfully applying the configuration. We will not dive into more complex and practical structures. jenkins.yaml describes the static configuration and is not intended to define the details of a specific CI/CD process for a particular project. For that purpose, you can use the Jenkinsfile, which offers flexibility for defining specific CI/CD steps and integrating with version control systems. We will discuss this in more detail in the next chapter.

Key Concepts of Jobs in JCasC

Jobs are a section of the configuration file that allows you to define and configure build tasks using code. This block includes the following:

  • Description of Build Tasks: This section describes all aspects of a job, including its type, stages, triggers, and execution steps.
  • Types of Jobs: There are different types of jobs in Jenkins, such as freestyle projects, pipelines, and multiconfiguration projects. In JCasC, pipelines are typically used because they provide a more flexible and powerful approach to automation.
  • Declarative Syntax: Pipelines are usually described using declarative syntax, simplifying understanding and editing.

Example Breakdown:

  • pipeline: The main block that defines the pipeline job.

  • agent any: Specifies that the pipeline can run on any available Jenkins agent.

  • stages: The block that contains the pipeline stages. A stage is a step in the process.

Additional Features:

  • Triggers: You can add triggers to make the job run automatically under certain conditions, such as on a schedule or when a commit is made to a repository:

triggers {
    cron('H 4/* 0 0 1-5')
}
  • Post-Conditions: You can add post-conditions to execute steps after the pipeline finishes, such as sending notifications or archiving artifacts.

  • Parameters: You can define parameters for a job to make it configurable at runtime:

parameters {
    string(name: 'BRANCH_NAME', defaultValue: 'main', description: 'Branch to build')
}

Automating Jenkins Deployment in Docker with JCasC

Using Scripts for Automatic Deployment

  • Use Bash scripts to automate the installation, updating, and running Jenkins containers.
  • Leverage Jenkins Configuration as Code (JCasC) to automate Jenkins configuration.

Script Examples

Script for Deploying Jenkins in Docker:

#!/bin/bash

# Jenkins Parameters
JENKINS_IMAGE="jenkins/jenkins:lts"
CONTAINER_NAME="jenkins-server"
JENKINS_PORT="8080"
JENKINS_AGENT_PORT="50000"
VOLUME_NAME="jenkins_home"
CONFIG_DIR="$(pwd)/jenkins_configuration"

# Create a volume to store Jenkins data
docker volume create $VOLUME_NAME

# Run Jenkins container with JCasC
docker run -d \
    --name $CONTAINER_NAME \
    -p $JENKINS_PORT:8080 \
    -p $JENKINS_AGENT_PORT:50000 \
    -v $VOLUME_NAME:/var/jenkins_home \
    -v $CONFIG_DIR:/var/jenkins_home/casc_configs \
    -e CASC_JENKINS_CONFIG=/var/jenkins_home/casc_configs \
    $JENKINS_IMAGE

The JCasC configuration file jenkins.yaml was discussed earlier.

Setting Up a CI/CD Pipeline for Jenkins Updates

To set up a CI/CD pipeline, follow these steps:

  1. Open Jenkins and go to the home page.

  2. Click on Create Item.

  3. Enter a name for the new item, select Pipeline, and click OK.

  4. If this section is missing, you need to install the plugin in Jenkins. Go to Manage JenkinsManage Plugins. In the Available Plugins tab, search for Pipeline and install the Pipeline plugin.

  5. Similarly, install the Git Push plugin.

  1. After installation, go back to Create Item.

  2. Select Pipeline, and under Definition, choose Pipeline script from SCM. Select Git as the SCM.

  3. Add the URL of your repository; if it's private, add the credentials. In the Branch Specifier field, specify the branch that contains the Jenkinsfile (e.g., */main). Note that the Jenkinsfile should be created without an extension. If it's located in a subdirectory, specify it in the Script Path field.

  4. Click Save.

Example of a Jenkinsfile

pipeline {
    agent any

    environment {
        JENKINS_CONTAINER_NAME = 'new-jenkins-server'
        JENKINS_IMAGE = 'jenkins/jenkins:lts'
        JENKINS_PORT = '8080'
        JENKINS_VOLUME = 'jenkins_home'
    }

    stages {
        stage('Setup Docker') {
            steps {
                script {
                    // Install Docker on the server if it's not installed
                    sh '''
                    if ! [ -x "$(command -v docker)" ]; then
                      curl -fsSL https://get.docker.com -o get-docker.sh
                      sh get-docker.sh
                    fi
                    '''
                }
            }
        }
        stage('Pull Jenkins Docker Image') {
            steps {
                script {
                    // Pull the latest Jenkins image
                    sh "docker pull ${JENKINS_IMAGE}"
                }
            }
        }
        stage('Cleanup Old Jenkins Container') {
            steps {
                script {
                    // Stop and remove the old container if it exists
                    def existingContainer = sh(script: "docker ps -a -q -f name=${JENKINS_CONTAINER_NAME}", returnStdout: true).trim()
                    
                    if (existingContainer) {
                        echo "Stopping and removing existing container ${JENKINS_CONTAINER_NAME}..."
                        sh "docker stop ${existingContainer} || true"
                        sh "docker rm -f ${existingContainer} || true"
                    } else {
                        echo "No existing container with name ${JENKINS_CONTAINER_NAME} found."
                    }
                }
            }
        }
        stage('Run Jenkins Container') {
            steps {
                script {
                    // Run Jenkins container with port binding and volume mounting
                    sh '''
                    docker run -d --name ${JENKINS_CONTAINER_NAME} \
                      -p ${JENKINS_PORT}:8080 \
                      -p 50000:50000 \
                      -v ${JENKINS_VOLUME}:/var/jenkins_home \
                      ${JENKINS_IMAGE}
                    '''
                }
            }
        }
        stage('Configure Jenkins (Optional)') {
            steps {
                script {
                    // Additional Jenkins configuration through Groovy scripts or REST API
                    sh '''
                    # Example script for performing initial Jenkins setup
                    curl -X POST http://localhost:${JENKINS_PORT}/scriptText --data-urlencode 'script=println("Jenkins is running!")'
                    '''
                }
            }
        }
    }

    post {
        always {
            echo "Jenkins setup and deployment process completed."
        }
    }
}
  1. On the page of your new pipeline, click Build Now.

  2. Go to Console Output. In case of a successful completion, you should see the following output.

Image13

For this pipeline, we used the following files. 

Dockerfile:

FROM jenkins/jenkins:lts

USER root
RUN apt-get update && apt-get install -y docker.io

docker-compose.yml:

version: '3.7'

services:
  jenkins:
    build: .
    ports:
      - "8081:8080" 
      - "50001:50000"
    volumes:
      - jenkins_home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - JAVA_OPTS=-Djenkins.install.runSetupWizard=false
    networks:
      - jenkins-network

volumes:
  jenkins_home:

networks:
  jenkins-network:

Ports 8081 and 50001 are used here so that the newly deployed Jenkins can occupy ports 8080 and 50000, respectively. This means that the main Jenkins, from which the pipeline is running, is currently located at http://localhost:8081/. One way to check if Jenkins has been deployed is to go to http://localhost:8080/, as we specified this in the pipeline. Since this is a new image, a welcome message with authentication will appear on the homepage.

Image3

Conclusion

Automating the deployment, updates, and backups of Jenkins is crucial for ensuring the reliability and security of CI/CD processes. Using modern tools enhances this process with a variety of useful features and resources.

If you're further interested in exploring Jenkins capabilities, we recommend the following useful resources that can assist with automating deployments:

Docker CI/CD
30.01.2025
Reading time: 19 min

Similar

Docker

Installing and Using Nexus Repository for Artifact Storage

In software development, "artifact" is a component of a developed software product. Artifacts include executable and binary files, software packages, and more. As the product's architecture and functionality increase, the number of artifacts grows exponentially, and you need a reliable storage solution that can manage large volumes of artifacts efficiently.  One such solution is Nexus Repository by Sonatype.  Nexus supports over 15 artifact formats, including APT, Docker, Go, Helm, Maven, npm, PyPi, and more.  There are two versions of Nexus Repository: Free version – Includes essential repository management features. Commercial version – Offers customer support and a broader range of supported artifact types. In this guide, we will install the free version of Nexus Repository. Prerequisites You can install Nexus Repository on a Linux, Windows, or MacOS machine. In this guide, we will use a Hostman cloud server running Ubuntu. The server must meet the following minimum requirements: 4-core CPU. 8-core or higher is recommended. 8 GB RAM  50 GB of free disk space (excluding OS and other installed software). If there is insufficient space, Nexus will not start. The official Sonatype website provides recommended system requirements based on the number of repositories and stored artifacts. Installing Nexus Repository Step 1: Install Java First, update the package list: sudo apt update Then, install Java 8 using OpenJDK: sudo apt -y install openjdk-8-jre-headless After installation, verify Java's version: java -version If you have multiple Java versions installed, switch to Java 8: sudo update-alternatives --config java Select the correct version using the TAB key and confirm with Enter. Step 2: Download and Extract Nexus We will install Nexus in the /opt directory. Download the latest Nexus archive: sudo wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz Extract the archive to /opt: sudo tar -zxvf latest-unix.tar.gz -C /opt Rename the extracted directory (for easier management): sudo mv /opt/nexus-3.61.0-02 /opt/nexus Make sure to specify the correct version number in your case.  Step 3: Create a Dedicated User for Nexus Create a new user named nexus: sudo adduser nexus Disable direct login for this user: sudo usermod nexus -s Set the correct ownership for Nexus directories: sudo chown -R nexus:nexus /opt/nexus /opt/sonatype-work Step 4: Configure Nexus Edit the Nexus configuration file: sudo nano /opt/nexus/bin/nexus.rc Uncomment (remove #) and set the user as nexus: run_as_user="nexus" Save and exit the file. Step 5: Create a Systemd Service for Nexus Create a new service file: sudo nano /etc/systemd/system/nexus.service Add the following content: [Unit] Description=Nexus Repository Manager After=network.target [Service] Type=forking LimitNOFILE=65536 ExecStart=/opt/nexus/bin/nexus start ExecStop=/opt/nexus/bin/nexus stop User=nexus Restart=on-abort [Install] WantedBy=multi-user.target Save and exit the file. Step 6: Start and Enable Nexus Start Nexus: sudo systemctl start nexus Check Nexus status: systemctl status nexus If the status is active, Nexus is running successfully. Enable Nexus to start on boot: sudo systemctl enable nexus Step 7: Verify Nexus Startup Nexus takes 2-3 minutes to fully start. To check the logs: tail -f /opt/sonatype-work/nexus3/log/nexus.log Once you see: Started Sonatype Nexus OSS Nexus is ready. By default, the Nexus web interface is accessible on port 8081: http://your-server-ip:8081 Initial Setup of Nexus Repository Step 1: Log into Nexus Open the Nexus web interface. Click the Sign in button (top-right corner). Retrieve the default admin password by running: cat /opt/sonatype-work/nexus3/admin.password Use this password to log in as admin. Step 2: Initial Configuration Set a new password for the admin user. Enable or disable anonymous access: Enabled: Any user with the repository URL can browse/download artifacts without authentication. Disabled: Authentication (login/password) is required. Nexus is now ready to use. Creating a Docker Repository in Nexus Step 1: Create a Docker Repository Log in as admin. Go to Server Administration and Configuration (gear icon). In the left menu, select Repositories. Click Create repository. Choose docker (hosted). Step 2: Configure the Repository Nexus supports three types of repositories: Hosted: Stores artifacts directly in Nexus. Proxy: Fetches artifacts from remote repositories (e.g., APT). Group: Combines multiple repositories into one unified endpoint. For a Docker repository: Set a repository name, e.g., docker-images. Enable HTTP access (check the box). Assign a port number (e.g., 8083). Save the settings. Step 3: Configure Docker to Work with Nexus By default, Docker does not allow HTTP connections. To enable HTTP access: Edit or create the Docker daemon configuration file: sudo nano /etc/docker/daemon.json Add the following configuration: { "insecure-registries": ["166.1.227.189:8083"] } (Replace 166.1.227.189:8083 with your Nexus server IP and port.) Save and exit the file. Restart the Docker service: sudo systemctl restart docker Warning: Restarting Docker affects running containers. Containers without the --restart=always flag may not restart automatically. Step 4: Push a Docker Image to Nexus Log in to Nexus via Docker: docker login 166.1.227.189:8083 You can use the admin username and the password you set earlier. Tag an image (e.g., alpine): docker tag alpine:latest 166.1.227.189:8083/alpine-test Verify the new tag: docker images Push the image to Nexus: docker push 166.1.227.189:8083/alpine-test Confirm the upload: Open the Nexus web interface. Go to the docker-images repository. The alpine-test image should be listed. Your Nexus repository is now set up for Docker image storage! HTTPS Configuration The correct approach when working with Docker repositories is to use the HTTPS protocol. To configure HTTPS in Nexus, follow these steps: Navigate to the /opt/nexus/etc/ssl directory: cd /opt/nexus/etc/ssl Use keytool to generate a self-signed certificate: sudo keytool -genkeypair -keystore keystore.jks -storepass test12345 -keypass test12345 -alias jetty -keyalg RSA -keysize 2048 -validity 1000 -dname "CN=*.${NEXUS_DOMAIN}, OU=test, O=test1, L=Unspecified, ST=Unspecified, C=RU" -ext "SAN=DNS:nexus-repo.com,IP:166.1.227.189" -ext "BC=ca:true" Replace the following values with your own: -keystore keystore.jks — Name of the file where the key will be stored, using the .jks format. -storepass test12345 — Password for accessing the key store. -keypass test12345 — Password for accessing the private key. -ext "SAN=DNS:nexus-repo.com,IP:166.1.227.189" — Specifies your Nexus server's domain name and IP address. If a local domain is used, it must be added to the /etc/hosts file (for Linux) or C:\Windows\System32\drivers\etc\hosts in Windows. Next, extract the server certificate from the generated keystore.jks file using the following command: sudo keytool -export -alias jetty -keystore keystore.jks -rfc -file nexus.cert The output will be a certificate file named nexus.cert. Now, edit the Nexus configuration file nexus-default.properties located in /opt/nexus/etc: sudo nano /opt/nexus/etc/nexus-default.properties Find the section with the comment # Jetty section, and add the following parameter: application-port-ssl=8443 This specifies the port where HTTPS will be available. In the same section, add this line: nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-https.xml,${jetty.etc}/jetty-requestlog.xml Next, edit the jetty-https.xml file located in /opt/nexus/etc/jetty: sudo nano /opt/nexus/etc/jetty/jetty-https.xml Locate the block named sslContextFactory. In the fields KeyStorePassword, KeyManagerPassword, TrustStorePassword, enter the password used during certificate generation. In this example, the password is test12345. In the parameters KeyStorePath and TrustStorePath, specify the name of the generated certificate. After making these changes, restart the Nexus service: sudo systemctl restart nexus Installing the Certificate on Linux In Linux, we must install the certificate at the OS level. Otherwise, when trying to authenticate to the repository using the docker login command, the following error may occur: x509: certificate signed by unknown authority To install the certificate on Ubuntu/Debian, follow these steps: Install the ca-certificates package: sudo apt -y install ca-certificates The certificate must be in .crt format, as other formats are not supported. Since the certificate was generated as .cert, rename it to .crt: sudo mv nexus.cert nexus.crt Copy the certificate to /usr/local/share/ca-certificates: sudo cp nexus.crt /usr/local/share/ca-certificates Install the certificate using the following command: sudo update-ca-certificates Configuring HTTPS in the Nexus Repository To configure HTTPS in the repository: Open the Nexus web interface and log in as an administrator. Navigate to the Server Administration and Configuration section (gear icon). In the left menu, select Repositories, find the required repository, check the box next to HTTPS, and specify a port that is not already in use on the server, such as 8344. 4 Save the changes. On the server, run the docker login command, specifying the repository address and HTTPS port: docker login nexus-repo.com:8344 You can log in using the admin account. Pushing an Image to the Repository To test pushing an image, let's use nginx: Assign a tag to the image. The format for Docker image tags is as follows: image:tag registry_address:repository_port/image_name Example: docker tag nginx:latest nexus-repo.com:8344/nginx-test Verify that the new image exists: docker images Push the image to the repository: docker push nexus-repo.com:8344/nginx-test Open the Nexus web interface, navigate to the repository, and confirm that the image was successfully uploaded. Conclusion Nexus Repository is an excellent solution for storing and managing artifacts. Supporting a wide range of artifact formats, it is suitable for both small development teams and larger enterprises.
11 March 2025 · 8 min to read
Docker

How To Install and Use Docker Compose on Ubuntu

Docker Compose has fundamentally changed how developers approach containerized applications, particularly when coordinating services that depend on one another. This tool replaces manual container management with a structured YAML-driven workflow, enabling teams to define entire application architectures in a single configuration file.  For Ubuntu environments, this translates to reproducible deployments, simplified scaling, and reduced operational overhead. This guide provides a fresh perspective on Docker Compose installation and usage, offering deeper insights into its practical implementation. Prerequisites Before you begin this tutorial, you'll need a few things in place: Deploy an Ubuntu cloud server instance on Hostman. Ensure you have a user account with sudo privileges or root access. This allows you to install packages and manage Docker. Install Docker and have it running on your server, as Docker Compose works on top of Docker Engine. Why Docker Compose Matters Modern applications often involve interconnected components like APIs, databases, and caching layers. Managing these elements individually with Docker commands becomes cumbersome as complexity grows. Docker Compose addresses this by allowing developers to declare all services, networks, and storage requirements in a docker-compose.yml file. This approach ensures consistency across environments—whether you’re working on a local Ubuntu machine or a cloud server. For example, consider a web application comprising a Node.js backend, PostgreSQL database, and Redis cache. Without Docker Compose, each component requires separate docker run commands with precise networking flags. With Compose, these relationships are organized once, enabling one-command setups and teardowns. Docker Compose Installation Follow these steps to install Docker Compose on your Ubuntu machine: Step 1: Verify that the Docker Engine is Installed and Running Docker Compose functions as an extension of Docker, so verify its status with: sudo systemctl status docker Example output: ● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2025-02-20 08:55:04 GMT; 5min ago TriggeredBy: ● docker.socket Docs: https://docs.docker.com Main PID: 2246435 (dockerd) Tasks: 9 Memory: 53.7M CPU: 304ms CGroup: /system.slice/docker.service └─2246435 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock If inactive, start it using sudo systemctl start docker. Step 2: Update System Packages Refresh your package lists to ensure access to the latest software versions: sudo apt-get update You will see: Hit:1 https://download.docker.com/linux/ubuntu jammy InRelease Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease Hit:4 http://security.ubuntu.com/ubuntu jammy-security InRelease Hit:5 http://repo.hostman.com/ubuntu focal InRelease Hit:6 http://archive.ubuntu.com/ubuntu jammy-updates InRelease Hit:7 http://archive.ubuntu.com/ubuntu jammy-backports InRelease Hit:3 https://prod-cdn.packages.k8s.io/repositories/isv:/kubernetes:/core:/stable:/v1.31/deb InRelease Hit:8 https://packages.redis.io/deb jammy InRelease Reading package lists... Done Step 3: Install Foundational Utilities Secure communication with Docker’s repositories requires these packages: sudo apt-get install ca-certificates curl  Step 4: Configure Docker’s GPG Key Authenticate Docker packages by adding their cryptographic key: sudo install -m 0755 -d /etc/apt/keyringssudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.ascsudo chmod a+r /etc/apt/keyrings/docker.asc This step ensures packages haven’t been altered during transit. Step 5: Integrate Docker’s Repository Add the repository tailored to your Ubuntu version: echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null The command auto-detects your OS version using VERSION_CODENAME. Step 6: Install the Docker Compose Plugin Update repositories and install the Compose extension: sudo apt updatesudo apt-get install docker-compose-plugin Step 7: Validate the Installation Confirm successful setup with: docker compose version The output displays the Docker Compose version: Docker Compose version v2.33.0 Building a Practical Docker Compose Project Let’s deploy a web server using Nginx to demonstrate Docker Compose’s capabilities. Step 1. Initialize the Project Directory Create a dedicated workspace: mkdir ~/compose-demo && cd ~/compose-demo Step 2. Define Services in docker-compose.yml Create the configuration file: nano docker-compose.yml Insert the following content: services: web: image: nginx:alpine ports: - "8080:80" volumes: - ./app:/usr/share/nginx/html In the above YAML file: services: Root element declaring containers. web: Custom service name. image: Uses the Alpine-based Nginx image for reduced footprint. ports: Maps host port 8080 to container port 80. volumes: Syncs the local app directory with the container’s web root. Step 3. Create Web Content Build the HTML structure: mkdir app nano app/index.html Add this HTML snippet: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Docker Compose Test</title> </head> <body> <h1>Hello from Docker Compose!</h1> </body> </html> Orchestrating Containers: From Launch to Shutdown Let’s explore how you can use Docker Compose for container orchestration: Start Services in Detached Mode Launch containers in the background: docker compose up -d Example output: [+] Running 2/2 ✔ Network compose-demo_default Created ✔ Container compose-demo-web-1 Started Docker Compose automatically pulls the Nginx image if missing and configures networking. Verify Container Status Check operational containers: docker compose ps -a Access the Web Application Visit http://localhost:8080 locally or http://<SERVER_IP>:8080 on remote servers. The test page should display your HTML content. Diagnose Issues via Logs If the page doesn’t load or if you encounter any issues, you can inspect container logs: docker compose logs web Example output: web-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration web-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh web-1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf web-1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf web-1 | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh … Graceful Shutdown and Cleanup Stop containers temporarily: docker compose stop Example output: [+] Stopping 1/1 ✔ Container compose-demo-web-1  Stopped Remove all project resources: docker compose down Example output: [+] Running 2/2 ✔ Container compose-demo-web-1  Removed ✔ Network compose-demo_default  Removed Command Reference: Beyond Basic Operations While the workflow above covers fundamentals, these commands enhance container management: docker compose up --build: Rebuild images before starting containers. docker compose pause: Freeze containers without terminating them. docker compose top: Display running processes in containers. docker compose config: Validate and view the compiled configuration. docker compose exec: Execute commands in running containers (e.g., docker compose exec web nginx -t tests Nginx’s configuration). Conclusion Docker Compose transforms multi-container orchestration from a manual chore into a streamlined, repeatable process. By adhering to the steps outlined—installing Docker Compose, defining services in YAML, and leveraging essential commands—you can manage complex applications with confidence.
26 February 2025 · 7 min to read
Docker

Docker Exec: How to Use It to Run Commands in a Container

Docker is an effective and versatile environment built to assist you in the matter of running, creating, as well as deploying apps within containers. One of the significant utilities in it is docker exec. It permits you to run code within a particular container. Furthermore, you can maintain as well as build a reliable, compact container through it. During creation or installation, it is significant to analyze different operations/configurations and examine the current condition or resolve bugs. Therefore, it offers an environment where commands can be run in dockerized apps. This tutorial will cover docker exec, complete with possible use cases and explanations. Prerequisites You must meet certain prerequisites before beginning the article: Installation: Verify that Docker is already installed. If not, check our tutorial to install it. Permissions: The user account should have permissions/privileges to run the script. Running Container: A container needs to be accessible as well as running at the moment. Through the docker ps, you can determine the ID or name of the container. General Concepts: You should be familiar with core concepts of Docker. Familiarity with Linux systems and Docker basics will help in troubleshooting any issues during configuration. These requirements are necessary before beginning the setup. Basic Introduction  docker exec permits greater control, enhanced privacy, as well as better security for your apps. It helps users with regarding, management, monitoring, and debugging running apps within the particular container. Explore its features to boost your productivity and automate workflows. In this way, you can run direct commands by performing several operations like opening sessions, shell commands, and even running scripts. This significantly enhances workflow by enabling interaction with the active/operational app. You can address issues as well as make configurations without the need for a full container restart, which improves efficiency. General Syntax  The general syntax is: docker exec [OPTIONS] CONTAINER CODE [ARG...] OPTIONS: These are flags for customizing the behaviour of the particular command. Several options are as below: -i: It indicates STDIN is launched even if not connected. -t: It addresses the allocation of a pseudo-TTY. -u USER: It indicates the particular user for running the command. -w WORKDIR: It indicates the directory which is working for the particular command. CONTAINER: It indicates the container ID or name, where instructions are executed. CODE:  This is the script or command that you require to run inside the container ARG: It represents the additional parameters that are required to be passed to the particular CODE. How to Use Docker Exec to Run Commands in a Container Through this utility, you can run programs, check logs, and perform other admin operations inside the particular running container by accessing its CLI. It is beneficial for effective management since it increases adaptability and gives more hold of dockerized apps. Testing with a Sample Container Before running the specific command, you should have the minimum of one container that is currently operational. If you do not have it yet, execute the below command by with the particular container name. In our case, we use mynginx:  docker run -d --name mynginx nginx Finding the Active Container ID Before beginning, you are required to know the ID or name of the running container. Let’s run the below command to obtain the info on all dockerized apps that are currently operational: docker ps In the figure, the operational instance ID is b51dc8e05c77 and the name is mynginx.  Working With a Particular Directory In this first example, you can run the command in the particular directory of the operational container. To achieve this, the --workdir or -w option is used by mentioning the folder name. Look at a use case where the pwd is run within the operational container mynginx: docker exec --workdir /tmp mynginx pwd Here: docker exec: It is the core command to run the command within the operational container. --workdir /tmp: This OPTION indicates our working directory. mynginx: It indicates the CONTAINER name.  pwd: It indicates the executed CODE within the container. In the figure, the pwd executes within the particular mynginx instance and allocates the working directory to /tmp.  Single Command Execution  In this example, execute a single command. For this, first mention the container name or ID, and afterwards, the particular command that you are required to execute. Here, mynginx is the name of the operational container, and the echo "Hello, Hostman Users!" is the command: docker exec mynginx echo "Hello, Hostman Users!" In the figure, there is an execution of the echo "Hello, Hostman Users!" command within mynginx. Several Commands Execution  You can execute several commands in a single line statement by splitting them with semicolon. Let’s look at the below statement: docker exec mynginx /bin/bash ls; free -m; df -h; In the result, ls shows the content inside of the mentioned folder, free -m shows the system memory and df -h disk space usage. It permits you to analyze the memory state, filesystem, and other info in one statement. Enabling the Shell Through Name You can enable the shell within the dockerized app. It permits an interface for the file system as well as script execution. Here, the -it option activates interactive mode and assigns the interface: docker exec -it mynginx /bin/bash The figure enables the bash shell interface within mynginx. But, /bin/bash is not guaranteed to be present in every image of Docker. Therefore, other shells like sh can also be enabled. Now, input exit and press ENTER to close the interface: exit To launch other shells like sh (which is a symbolic link to bash or another shell), use /bin/sh in the below statement line: docker exec -it mynginx /bin/sh In the figure, the code line launches the shell interface, which is operational.  Enabling the Shell Through ID In this particular use case, enable the session through the b51dc8e05c77 container ID inside the Docker app. Furthermore, you have the ability to interact with the interface as though you directly logged in via the -it flag. The -t indicates the assignment of pseudo-TTY, and the -i opens the STDIN. Both are beneficial for analysis, debugging, as well as managerial operations: docker exec -it b51dc8e05c77 bash Furthermore, you can analyse the information of the current folder inside the particular shell (in a detailed format), e.g., file size, owner, group, number of links, modification date, and file permissions: ls -l It gives detailed information on each file as well as the folder that assists you in knowing their attributes and managing them effectively. Working As a Particular User You can execute a command as the specific user through the -u option. It is beneficial when you are permitted to work with specific privileges. It runs the command in the operational container through the particular user and group: docker exec -u <user>:<group> <container_id> <command>  For instance, the whoami runs as the www-data in the mynginx container: docker exec -u www-data mynginx whoami In the figure, www-data verifies that the particular command is executed successfully with the correct user permissions and within the expected interface.  Enabling a Non-Interactive Shell Sometimes, users prefer not to have any interaction. For such circumstances, they can execute the command without any argument: docker exec mynginx tail /etc/passwd  The last 10 lines of the passwd file have been shown. This passwd file is stored in the /etc/passwd folder containing the user information. It helps you monitor the user account information, permitting you to quickly check for troubleshooting or update issues.  Working With a Single Environment Variable You may need to pass environment variables to the command that is run in the operational container. To achieve this, use the -e option as below: docker exec -e MY_VAR=value mynginx printenv MY_VAR In the figure, the printenv MY_VAR is successfully executed in mynginx when the MY_VAR is set to value correctly. Working With Multiple Environment Variables You can set more than one variable through the -e flag.  docker exec -e TEST=john -e ENVIRONMENT=prod mynginx env The figure confirms that the two variables TEST and ENVIRONMENT have been set to john and prod in the mynginx. Working With the Detached Mode You can run commands in the detached mode through the -d flag. Therefore, it runs in the background: docker exec -d mynginx sleep 500 The figure confirms that the mynginx is executing the sleep 500 command. Working With the Privileged Mode Here, the --privileged flag permits you to execute the command, such as mount, with elevated privileges in the running container: docker exec --privileged mynginx mount In the figure, mount permits the system to create a mount point with the particular permissions in the mynginx. More Information on docker exec The --help option shows the manual with a list of available options with concise explanations.  docker exec --help Final Words docker exec is an effective utility for controlling and interacting with active containers. It is helpful for operations like monitoring, managing, and debugging apps without interfering with their functionality. It permits you to run code, launch shells, customize several configuration aspects, and also set environment variables. Once you become familiar with the usage of this utility, you can manage containers easily. It makes your operations much smoother for creating and deploying apps.
29 January 2025 · 8 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support