Sling Academy
Home/DevOps/How to open an SSH port on Jenkins container

How to open an SSH port on Jenkins container

Last updated: February 03, 2024

Introduction

To effectively manage Jenkins and potentially automate many administrative tasks, it is typically necessary to open an SSH port on the Jenkins container. This tutorial guides you through several methods, starting from basic to more advanced techniques, ensuring that you can securely and successfully access your Jenkins container over SSH.

Prerequisite

Before proceeding, make sure you have the following:

  • Docker installed on your machine.
  • A running Jenkins container.
  • Knowledge of basic Docker commands.

Basic: Using Docker’s ‘-p’ option

Let’s start with the simplest method of opening an SSH port by using Docker’s port-mapping feature.

$ docker run -d -p 2222:22 --name my-jenkins jenkins/jenkins:lts

This command runs a Jenkins container named ‘my-jenkins’ and maps the host’s port 2222 to the container’s port 22, the default SSH port. After execution, you should be able to SSH into your Jenkins container using:

$ ssh -p 2222 jenkins@localhost

The output should prompt you for a password. Ensure you’ve configured SSH access in the Jenkins container beforehand.

Intermediate: Custom Dockerfile

For more control, consider creating a custom Dockerfile that installs and configures both Jenkins and the SSH server within the container.

FROM jenkins/jenkins:lts
RUN apt-get update && apt-get install -y openssh-server
RUN echo 'root:password' | chpasswd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

In this Dockerfile, we base our image on the Jenkins LTS version, install the OpenSSH server, set a password for the root user, expose port 22, and start the SSH server. Build this image with:

$ docker build -t my-custom-jenkins .

Then run:

$ docker run -d -p 2222:22 my-custom-jenkins

Now, SSH into the container is the same as before, but you use root access.

Advanced: Docker Compose and SSH Keys

For an advanced setup, especially useful in production environments, Docker Compose can manage both port mappings and SSH keys.

version: '3'
services:
  jenkins:
    image: jenkins/jenkins:lts
    ports:
      - "2222:22"
    volumes:
      - "/path/to/.ssh:/root/.ssh"
    command: "/usr/sbin/sshd -D"

This compose file pulls the Jenkins LTS image, maps port 2222 to 22, and mounts a volume from the host’s .ssh directory to the container’s root .ssh directory. It ensures that your container uses the same SSH keys as your host, enhancing security. Start your service with:

$ docker-compose up -d

Note: Ensure the SSH server is installed and configured within your Jenkins container, as mentioned in the ‘Intermediate’ section.

Conclusion

Opening an SSH port on a Jenkins container aligns with various degrees of complexity, from simple port mapping in Docker to configuring custom Dockerfiles and managing security through Docker Compose. Integrating these methods into your development workflow can enhance control, security, and efficiency in managing Jenkins containers.

Next Article: Jenkins Web UI: What is the default port and how to change it

Previous Article: Managing Environment Variables in Jenkins: A Practical Guide

Series: Jenkins Tutorials

DevOps

You May Also Like

  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide
  • Terraform: 3 Ways to Remove Duplicates from a List
  • Terraform: How to convert a number to a string and vice versa
  • Using bcrypt() and md5() functions in Terraform