Customize Docker Image For Enabling SSH In Docker Container & Configuring Webserver Inside Docker Container Using Ansible

Laveena Jethani
4 min readMar 30, 2021

Hello Everyone,

I want to show I have created my own customize docker image for enabling ssh inside a docker container and I have configured the webserver inside the docker container using an ansible playbook.

Ansible & Docker Integration

As we know that by default if we create a container using centos image so inside the docker container we can not do remote login using SSH but we can enable this feature.

I have created Dockerfile and from this Dockerfile, I will create my own customize docker image so that we can do SSH login inside the docker container.

Dockerfile

  • Taking centos image as a base docker image
  • Installing net-tools,openssh-server,passwd software.
  • Setting a root password for the docker container.
  • Generating key
  • Running sshd service in the background.
Dockerfile

Building The Docker Image

I have created the docker image and from this docker image I will launch the docker container and in this docker container, I can do the remote login using ssh. Now ssh is enabled in the Docker container now I can run any task using ansible.

Configuring Docker Host

I have created a playbook docker-host.yml, this playbook will do the following task for me:

  • Install docker
  • Start docker service
  • Pull Image & launch container & expose the docker container
  • Retrieve docker container IP
  • Inside inventory mentioning docker container IP for further web server configuration.

Now let’s run the playbook docker-host.yml

After running docker-host.yml this playbook has been configured Ansible inventory file for the mention docker container IP.

Ansible Inventory File

In this inventory docker container IP, Username, and password are written using this username and password ansible controller login inside a docker container and do the tasks.

Ansible Inventory

Now Let’s configure the webserver inside the docker container using an ansible playbook. For this, I have created a docker-container.yml playbook this playbook will configure a web server inside a docker container.

Configure Web Server Inside Docker Container

docker-container.yml file

docker-container.yml

Now let’s run the playbook docker-container.yml

As previously while launching the container I have mentioned in the docker-host.yml file container will expose at port 8880. Here we are exposing to docker container because web clients don’t have direct connectivity with docker container because it is running privately with docker bridge. So give connectivity to docker container using docker host IP. The client will communicate with the docker host and the docker host will transfer the client request to the docker container. so let’s run the website on the browser and see the output.

Run Website with port 8880

website

Successfully Setup Webserver Inside Docker Container

I have uploaded code at the GitHub Please have look at the code.

Github Link:

--

--

Laveena Jethani

Technical Blog Writer | Research & Review different technologies | ARTH learner