Section4 - Jenkins & Docker
Section4 - Jenkins & Docker
Using SSH
Introduction
• Previous Jenkins jobs were executed locally within the Jenkins
container
• Need for executing jobs on remote machines using SSH
• Two approaches to setting up remote execution:
1. Creating a separate virtual machine as remote host
2. Creating another Docker container (recommended for learning)
FROM centos:7
This section: 1. Copies the public key from the build context into the
authorized_keys file 2. Sets appropriate ownership of the .ssh directory and
its contents 3. Sets secure permissions on the authorized_keys file (600
means only the owner can read or write)
RUN /usr/sbin/sshd-keygen
This creates the server’s unique cryptographic keys, which are essential for
establishing secure SSH connections.
Service Startup Configuration
The Dockerfile concludes with the command to run when the container
starts:
CMD /usr/sbin/sshd -D
The -D flag tells sshd to run in the foreground and not detach from the
terminal. This is crucial in a Docker container as the container will stop if
the main process terminates.
ssh-keygen -f remote-key
remote_host:
container_name: remote-host
image: remote-host
build:
context: centos7
networks:
- net
Key Components:
docker-compose up -d
ssh remote_user@remote-host
# Enter password: 1234
Key-based Authentication
name="Ricardo"
echo "Hello, $name, current date and time is $(date)" > /tmp/
remote_file
Key Takeaways:
• SSH Plugin: The “Execute shell script on remote host using SSH”
build step utilizes the SSH plugin to connect to and interact with
remote hosts.
• Remote Execution: The script is executed on the remote host, not
within the Jenkins container. This is crucial for tasks that need to
interact with the remote host’s filesystem or services.
• File Location: The output file is created on the remote host in the
specified location (/tmp/ in the example).
• Troubleshooting: The console output provides valuable information
for debugging if the connection or script execution fails.