# ansible / lab / docker
Build a Local Ansible Lab with Docker & SSH
Learning Ansible requires target nodes. Instead of paying for cloud VMs, this guide shows you how to spin up Ubuntu Docker containers and secure them with PEM authentication so they act exactly like real servers.
Host Preparation
First, we need to ensure the host machine has the required software (Docker and Ansible) and generate the SSH key pair that Ansible will use to authenticate with the containers.
Why a PEM key?
We use a 4096-bit RSA key to mimic enterprise environments where password authentication is strictly disabled. The .pem file is your private key, keep its permissions locked down (400).
Spawn the Nodes
> --hostname server1 \
> --network ansible-lab ubuntu:22.04
> --hostname server2 \
> --network ansible-lab ubuntu:22.04
A Private Network
By creating a custom Docker network (ansible-lab), we ensure both containers can communicate with each other securely, and the host machine can route traffic to them.
Configure SSH Inside Containers
Base Ubuntu containers do not come with SSH installed. We need to exec into the containers, install the OpenSSH server, create our devops user, and inject the public key we generated earlier.
The Internal Config Flow
- Enter
Execute bash inside the running container.
- Install
Download and install
openssh-server. - User
Create the
devopsuser with a home directory and bash shell. - Keys
Create the
.sshdirectory and authorize the public key. - Daemon
Harden
sshd_configand start the SSH service.
Verify Connection
You must repeat the internal SSH configuration steps for ubuntu-server-2. Once both are configured, you can add their IPs to your Ansible i