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

Docker Exec: Access, Commands, and Use Cases

docker exec is a utility that allows you to connect to an already running Docker container and execute commands without restarting or stopping it. This is very convenient for technical analysis, configuration, and debugging applications. For example, you can check logs, modify configurations, or restart services. And on a cloud server in Hostman, this command helps manage running applications in real time, without rebuilding containers or interfering with the image. How to Use docker exec: Parameters and Examples Before using it, make sure Docker is installed and the container is running. If you are just starting out, check out the installation guide for Docker on Ubuntu 22.04. The basic syntax of docker exec is: docker exec [options] <container> <command> Where: <container> is the name or ID of the target container; <command> is the instruction to be executed inside it. Main Parameters: -i — enables input mode; -t — attaches a pseudo-terminal, useful for running bash; -d — runs the task in the background; -u — allows running the command as a specified user; -e — sets environment variables; -w — sets the working directory in which the command will be executed. Example of launching bash inside a container: docker exec -it my_container /bin/bash This way, you can access the container’s environment and run commands directly without stopping it. Usage Examples List files inside the container: ls /app Run commands with root access: docker exec -u root my_container whoami Pass environment variables: docker exec -e DEBUG=true my_container env Set working directory: docker exec -w /var/www my_container ls Run background tasks: docker exec -d my_container touch /tmp/testfile Check Nginx configuration inside a container before restarting it: docker exec -it nginx_container nginx -t Advanced Use Cases Let’s consider some typical but slightly more complex scenarios that may be useful in daily work: running as another user, passing multiple environment variables, specifying a working directory, etc. Run as web user: docker exec -u www-data my_container ls -la /var/www Set multiple environment variables at once: docker exec -e DEBUG=true -e STAGE=dev my_container env Set working directory with admin rights: docker exec -u root -w /opt/app my_container ls Example with Laravel in Hostman If you deploy a Laravel application in a container on a Hostman server, docker exec will be very handy. Suppose you have a container with Laravel and a database in a separate service. To connect to the server: ssh root@your-server-ip After connecting, you can run Artisan commands—Laravel’s built-in CLI—inside the container. Clear application cache: docker exec -it laravel_app php artisan cache:clear Run migrations: docker exec -it laravel_app php artisan migrate Check queue status: docker exec -it laravel_app php artisan queue:listen Set permissions: docker exec -u www-data -it laravel_app php artisan config:cache Make a backup of a database deployed in a separate container: docker exec -it mariadb_container mysqldump -u root -p laravel_db > backup.sql Before running the last command, make sure that a volume for /backup is mounted, or use SCP to transfer the file to your local machine. This approach does not require changing the image or direct container access, which makes administration safe and flexible. Extended Capabilities of docker exec In this section, we will look at less common but more flexible uses of the docker exec command: for example, running psql in a PostgreSQL container, executing Node.js scripts, or connecting to stopped containers. These cases show how flexible the command can be if something non-standard is required. The command is not limited to basic tasks: in addition to launching shell or bash, you can work with environments, interact with databases, run Node.js scripts, and connect to any running container. Connect to PostgreSQL CLI: docker exec -it postgres_container psql -U postgres -d my_db Run a Node.js script (if you have script.js): docker exec -it node_app node script.js Run a stopped container: docker start my_container   docker exec -it my_container bash Manage users explicitly with -u: docker exec -u www-data my_container ls -la /var/www Quickly remove temporary files: docker exec -it my_container rm -rf /tmp/cache/* This approach is convenient in cron jobs or when manually cleaning temporary directories. When Not to Use the Command Despite its convenience, docker exec is a manual tool for interacting with containers. In production environments, its use can be risky. Why not use docker exec in production: Changes are not saved in Dockerfile. This can break reproducibility and infrastructure integrity. No command logging, so it’s difficult to track actions. Possible desynchronization with CI/CD pipeline. Instead, it is recommended to use: Dockerfile and docker-compose.yml for reproducible builds; CI/CD for automating tasks via GitHub Actions or GitLab CI; Monitoring for log processes with Prometheus, Grafana, and Loki. Troubleshooting Common Errors No such container Cause: container not found or stopped Solution: docker ps The command shows a list of running containers. If your container is not listed, it’s not running or hasn’t been created. exec failed: container not running Cause: attempt to run a command in a stopped container Solution: docker start <container_name> After starting the container, you can use docker exec again. permission denied Cause: insufficient user permissions Solution: docker exec -u root <container> <command> The -u root flag runs the command as root, providing extended access inside the container. This is especially useful when working with system files or configurations. Difference Between docker exec and docker attach In addition to docker exec, there is another way to interact with a container—the docker attach command. It connects you directly to the main process running inside the container, as if you launched it in the terminal. This is convenient if you need to monitor logs or enter data directly, but there are risks: any accidental key press (for example, Ctrl+C) can stop the container. That’s why it’s important to understand the differences. Also, docker attach requires TTY (a terminal emulator) for correct work with interactive apps like bash or sh. Parameter docker exec docker attach Requires TTY Optional Yes Multiple connections Yes No Interferes with main process No Yes Usable for debugging Yes Partially (may harm app) Use docker exec for auxiliary tasks—it provides flexibility and reduces risks. Conclusion The docker exec command is an effective tool for managing containers without interfering with their lifecycle. It allows you to run commands as different users, pass variables, check logs, and perform administrative tasks. When working in cloud services such as Hostman, this is especially useful: you can perform targeted actions without rebuilding the image and without risking the main process. It is important to remember: docker exec is a manual tool and does not replace automated DevOps approaches. For system-level changes, it is better to use Dockerfile and CI/CD, keeping your infrastructure reproducible and secure.
05 September 2025 · 6 min to read
Docker

How to Install Docker on Ubuntu 22.04

Docker is a free, open-source tool for application containerization. Containers are isolated environments similar to virtual machines (VMs), but they are more lightweight and portable across platforms, requiring fewer system resources. Docker uses OS-level virtualization, leveraging features built into the Linux kernel. Apps order after installing Docker on Ubuntu Although it applies to other Ubuntu versions as well, this tutorial explains how to install Docker on Ubuntu 22.04. We'll also download Docker Compose, which is a necessary tool for effectively managing several containers. For this guide, we will use a Hostman cloud server. System Requirements According to Docker's documentation, the following 64-bit Ubuntu versions are supported: Ubuntu Oracular 24.10 Ubuntu Noble 24.04 (LTS) Ubuntu Jammy 22.04 (LTS) Ubuntu Focal 20.04 (LTS) Docker works on most popular architectures. The resource requirements for your device will depend on your intended use and how comfortably you want to work with Docker. The scale of applications you plan to deploy in containers will largely dictate the system needs. Some sources recommend a minimum of 2 GB of RAM. Additionally, a stable internet connection is required. Installing Docker on Ubuntu 22.04 Installing Docker on Ubuntu 22.04 involves executing a series of terminal commands. Below is a step-by-step guide with explanations. The steps are also applicable to server versions of Ubuntu. 1. Update Package Indexes The default repository may not always contain the latest software releases. Therefore, we will download Docker from its official repository to ensure the latest version. First, update the package indexes: sudo apt update 2. Install Additional Packages To install Docker, you’ll need to download four additional packages: curl: Required for interacting with web resources. software-properties-common: Enables software management via scripts. ca-certificates: Contains information about certification authorities. apt-transport-https: Necessary for data transfer over the HTTPS protocol. Download these packages with the following command: sudo apt install curl software-properties-common ca-certificates apt-transport-https -y The -y flag automatically answers "Yes" to all terminal prompts. 3. Import the GPG Key Software signatures must be verified using the GPG key. Docker's repository must be added to the local list. Use the command to import the GPG key: wget -O- https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor | sudo tee /etc/apt/keyrings/docker.gpg > /dev/null During the import process, the terminal may display a warning before confirming the successful execution of the command. 4. Add Docker Repository Add the repository for your version of Ubuntu, named "Jammy." For other versions, use their respective code names listed in the "System Requirements" section. Run the following command: echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null During execution, the terminal will prompt you to confirm the operation. Press Enter. 5. Update Package Indexes Again After making these changes, update the package indexes once more using the familiar command: sudo apt update 6. Verify the Repository Ensure that the installation will proceed from the correct repository by running the following command: apt-cache policy docker-ce Output example: Depending on the most recent Docker releases, the result could change. Verifying that the installation will be carried out from Docker's official repository is crucial. 7. Installing Docker After configuring the repositories, proceed with the Docker installation: sudo apt install docker-ce -y The installation process will begin immediately. To confirm a successful installation, check Docker's status in the system: sudo systemctl status docker Output example: The output should indicate that the Docker service is active and running. And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS. Installing Docker Compose Docker Compose is a Docker tool designed for managing multiple containers. It is commonly used in projects where many containers must work together as a unified system. Managing this process manually can be challenging. Instead, you describe the entire configuration in a single YAML file containing the settings and configurations for all containers and their applications. There are several ways to install Docker Compose. If you need the latest version, make sure to use manual installation and installation via the Git version control system. Installation via apt-get If having the latest version is not critical for you, Docker Compose can be installed directly from the Ubuntu repository. Run the following command: sudo apt-get install docker-compose Installing via Git First, install Git: sudo apt-get install git Verify the installation by checking the Git version: git --version The output should show the Git version. Next, clone the Docker Compose repository. Navigate to the Docker Compose GitHub page and copy the repository URL. Run the following command to clone the repository: git clone https://github.com/docker/compose.git The cloning process will begin, and the repository will be downloaded from GitHub. Manual Installation Go to the Docker Compose GitHub repository and locate the latest release version under the Latest tag. At the time of writing, the Latest version of Docker Compose is v2.31.0. Let's download it: sudo curl -L "https://github.com/docker/compose/releases/download/v2.31.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose In this command, the parameters $(uname -s) and $(uname -m) automatically account for the system characteristics and architecture. After the download finishes, change the file's permissions: sudo chmod +x /usr/local/bin/docker-compose Right order of your infrastructure after installation of Docker on Ubuntu Conclusion In this guide, we covered the installation of Docker on Ubuntu 22.04, along with several ways to install Docker Compose. You can order a cloud server at Hostman for your experiments and practice.
22 August 2025 · 5 min to read
Docker

Running Selenium with Chrome in Docker

Sometimes, it’s useful to work with Selenium in Python within a Docker container. This raises questions about the benefits of using such tools, version compatibility between ChromeDriver and Chromium, and the nuances of their implementation. In this article, we’ll cover key considerations and provide solutions to common issues. And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS. Why Run Selenium in Docker? Running Selenium in a container offers several advantages: Portability: Easily transfer the environment between different machines, avoiding version conflicts and OS-specific dependencies. Isolation: The Selenium container can be quickly replaced or updated without affecting other components on the server. CI/CD Compatibility: Dockerized Selenium fits well into CI/CD pipelines — you can spin up a clean test environment from scratch each time your system needs testing. Preparing an Ubuntu Server for Selenium with Docker First, make sure Docker and Docker Compose are installed on the server: docker --version && docker compose version In some Docker Compose versions, the command is docker-compose instead of docker compose. If the tools are installed, you’ll see output confirming their versions. If not, follow this guide. Selenium in Docker Example When deploying Selenium in Docker containers, consider the host architecture, functional requirements, and performance. Official selenium/standalone-* images are designed for AMD64 (x86_64) CPUs, while seleniarm/standalone-* images are adapted for ARM architectures (e.g., Apple silicon or ARM64 server CPUs). First, create a docker-compose.yml file in your project root. It will contain two services: version: "3" services: app: build: . restart: always volumes: - .:/app depends_on: - selenium platform: linux/amd64 selenium: image: selenium/standalone-chromium:latest # For AMD64 # image: seleniarm/standalone-chromium:latest # For ARM64 container_name: selenium-container restart: unless-stopped shm_size: 2g ports: - "4444:4444" # Selenium WebDriver API - "7900:7900" # VNC Viewer environment: - SE_NODE_MAX_SESSIONS=1 - SE_NODE_OVERRIDE_MAX_SESSIONS=true - SE_NODE_SESSION_TIMEOUT=300 - SE_NODE_GRID_URL=http://localhost:4444 - SE_NODE_DETECT_DRIVERS=false You must choose the correct image for your system architecture by uncommenting the appropriate line. The app service will run your main Python code. Let’s define a standard Dockerfile for this service: # Use a minimal Python image FROM python:3.11-slim # Set working directory WORKDIR /app # Install Python dependencies COPY requirements.txt /app/ RUN pip install --no-cache-dir -r requirements.txt # Copy project files COPY . /app/ # Set environment variables (Chromium is in a separate container) ENV SELENIUM_REMOTE_URL="http://selenium:4444/wd/hub" # Run Python script CMD ["python", "main.py"] This Dockerfile uses a base Python image and automatically installs the necessary dependencies. Now let’s add the driver initialization script to main.py: import time # Used to create a delay for checking browser functionality import os from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options # WebDriver settings chrome_options = Options() chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--disable-webrtc") chrome_options.add_argument("--hide-scrollbars") chrome_options.add_argument("--disable-notifications") chrome_options.add_argument("--start-maximized") SELENIUM_REMOTE_URL = os.getenv("SELENIUM_REMOTE_URL", "http://selenium:4444/wd/hub") driver = webdriver.Remote( command_executor=SELENIUM_REMOTE_URL, options=chrome_options ) # Open a test page driver.get("https://www.timeweb.cloud") time.sleep(9999) # Shut down WebDriver driver.quit() In the requirements.txt file, list standard dependencies, including Selenium: attrs==25.1.0 certifi==2025.1.31 h11==0.14.0 idna==3.10 outcome==1.3.0.post0 PySocks==1.7.1 selenium==4.28.1 sniffio==1.3.1 sortedcontainers==2.4.0 trio==0.28.0 trio-websocket==0.11.1 typing_extensions==4.12.2 urllib3==2.3.0 websocket-client==1.8.0 wsproto==1.2.0 Now you can launch the containers: docker compose up -d Expected output: Docker will build and launch the containers. To verify everything is running correctly: docker compose ps You should see two running containers which means everything was loaded successfully. You can now integrate a script in main.py to interact with any site. Debugging Selenium in Docker with VNC In official Selenium Docker images (like seleniarm/standalone-chromium, selenium/standalone-chrome, etc.), direct access to the Chrome DevTools Protocol is usually overridden by Selenium Grid. It generates a new port for each session and proxies it via WebSocket. Arguments like --remote-debugging-port=9229 are ignored or overwritten by Selenium, making direct browser port access impossible from outside the container. Instead, these Docker images offer built-in VNC (Virtual Network Computing), similar to TeamViewer or AnyDesk, but working differently. VNC requires headless mode to be disabled, since it transmits the actual screen content — and if the screen is blank, there will be nothing to see. You can connect to the VNC web interface at: http://<server_ip>:7900 When connecting, you'll be asked for a password. To generate one, connect to the selenium-container via terminal: docker exec -it selenium-container bash Then enter: x11vnc -storepasswd You’ll be prompted to enter and confirm a password interactively. Enter the created password into the VNC web interface, and you’ll gain access to the browser controlled by Selenium inside Docker. From there, you can open DevTools to inspect DOM elements or debug network requests. Conclusion Running Selenium in Docker containers simplifies environment portability and reduces the risk of version conflicts between tools. It also allows visual debugging of tests via VNC, if needed. Just make sure to choose the correct image for your system architecture and disable headless mode when a graphical interface is required. This provides a more flexible and convenient infrastructure for testing and accelerates Selenium integration into CI/CD pipelines.
19 June 2025 · 6 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