How to open an SSH port on Jenkins container

Updated: February 3, 2024 By: Guest Contributor Post a comment

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.