Install Ssh In Docker Container

/ Comments off

  1. Install Ssh In Docker Container Design
  2. Install Ssh In A Docker Container

Hey guys hope you all are doing in today’s article we are going to see how can we directly run ansible-playbook directly into the docker container. Isn’t it interesting just think one playbook launching container for us updating inventory and when we run another playbook it will configure the container to run an apache web server.

Install Ssh In Docker Container Design

To continue with this practical we will be needed a docker image that has ssh enabled in it you can use any but if don’t know how you can read this article

Once you have an image with ssh enabled we are ready to write the playbook we are going to write two playbook one for launching container and updating inventory and one for configuring container

To enable SSH inside Docker Container, you have to create one configuration file that contains permissions and host keys as given below. Write down these below lines inside sshdconfig file. Create one Dockerfile that will download all the required software. Build this Dockerfile, so that it will create one customize image with SSH access. Installing the SSH Server in a Docker Container. Most popular Docker base images are kept intentionally streamlined. You’ll need to add the OpenSSH server yourself, even on images derived from popular operating system distriubtions. Here’s an example Dockerfile for a Debian-based image. And in the docker container i am running a web service which must validate the user's password with the docker host's /etc/password. My view is to ssh into docker host from the docker container. So when i run command ssh in the docker container its saying ssh not found. So,basically ssh is not installed in the container. How can i install ssh.

Playbook 1 => To launch container

– hosts: localhost
— name: Installing Docker-py For Ansible
— command: pip3 show docker-pyrescue:
— pip:
name: docker-py
— name: Starting docker
— service:
name: docker
state: started
enabled: yes
— debug:
msg: ‘Docker service start’- name: Stoping SELINUX
policy: targeted
state: permissive
— name: Launching A Docker Container
name: “test”
image: adity12/ssh:latest
state: started
interactive: yes
detach: yes
tty: yes
— “8091:80”

— name: Retrieving Docker Container IP
name: test
register: x- debug:
var: x.container.NetworkSettings.IPAddress- name: Updating Inventory File By Docker Container IP
dest: “/ip.txt”
{{ x[‘container’][‘NetworkSettings’][‘IPAddress’] }} ansible_user=test ansible_ssh_pass=aditya ansible_connection=ssh

In above playbook test can be changed to any name as you like want to launch the container. An image I have used my image you can either use this or you can create your own and update in the playbook accordingly.

Image after running playbook 1

Playbook 2 => To configure container

– hosts: docker
become: yes
become_user: root
become_method: sudo
— name: html_file
private: no
prompt: “enter file name”
— name: cont
private: no
prompt: “enter content”tasks:
— name: Installing webserver
name: apache2
state: present
— name: Start serivce
name: apache2
state: started
— name: create html file
content: “{{cont}}”
dest: “/var/www/html/{{html_file}}.html”

The above playbook is used to configure the container here we are configuring an apache web server in a container from ansible master node when we run this playbook it will ask the name of the file and content that we want to put in the file.

NOTE:- Here our ansible controller node is the same as on which docker-engine in running

when we run playbook2

Once you are done with all these things goto your system IP/filename.html one thing to be remembered if you are checking from the system in which docker-engine running locally then ip will be docker ip but if you are using it in some other system then ip will be one of your system ip in which docker engine is running.


here I am using a virtual box inside that one OS on which docker-engine is running so my ip will be os ip on which docker running and we also have to give the port number you can use any here we have used 8091 so final format will for address will IP: PORT (Only when we are running outside the os where docker-engine is running)

Connected to docker and working good

In the above image you can, I am checking the webserver is configured properly or not from windows, and the docker engine is installed in different os in VM so is the ip of my os in which docker engine is running and port is that we used during patting that 8091.

Guys, here we come to the end of this blog I hope you all like it and found it informative. If have any query feel free to reach me 🙂

Install Ssh In A Docker Container

Github link:-

Guys follow me for such amazing blogs and if have any review then please let me know I will keep those points in my mind next time while writing blogs. If want to read more such blog to know more about me here is my website link Please do not hesitate to keep for it (An Open Secret: You can clap up to 50 times for a post, and the best part is, it wouldn’t cost you anything), also feel free to share it across. This really means a lot to me.