Skip to content

Here I'm automating docker image creating using ansible. It will help for developer to reduce their workload.

Notifications You must be signed in to change notification settings

Jisjo/ansible_docker-build-and-push-to-dockerhub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Docker Image Build and Push the Image to DockerHub using Ansible

Build Status


Description

Here I'm automating docker image creating using ansible. we will be installing docker and building a docker image for a Flask application. Also, the image built will be pushed to DockerHub using ansible-playbook.


Architecture

Images

Step 1

Ansible will connect to the build server. All dockerization operations will run on this server.

Step 2

To build the container pushing the Docker file and flask app from the git repo.

Step 3

Created Docker image is pushing to docker hub.


Overview

In this demo, we will be provisioning the following :

  • Install packages: git, docker, and its required dependencies to run the playbook on the build server.
  • Clone the git repository to the remote build server.
  • Build and push the image-created image to DockerHub after login in to the docker hub.

Requirements

  • Install Ansible on your machine (ansible master).
  • DockerHub account.

Packages

amazon-linux-extras install -y ansbile2
yum install -y git
Ansible Modules used

How to use

git clone https://github.com/Jisjo/ansible_docker-build-and-push-to-dockerhub.git
cd ansible_docker-build-and-push-to-dockerhub
ansible-playbook -i inventory 01-docker-build-push.yml

Variables Used

    git_url: https://github.com/Jisjo/Docker_python_flask_helow-world.git     # Git repo URL
    docker_user: "jisjo"                                                      # DockerHub username
    docker_password: "****************"                                       # DockerHub password
    image_name: "jisjo/devops-flaskapp"                                       # Image name

Behind the code

Here we are installing packages to bulder server.

    - name: "Build - Package Installation"
      yum:
        name:
          - docker
          - git
          - python2-pip
        state: present
            
    - name: "Build - Sarting Docker Service"
      service:
        name: docker
        state: restarted
        enabled: true
            
    - name: "Build - Installing Docker Client For Python"
      pip:
        name: docker-py
        state: present

Here we clone the Docker file from the github repo.

   - name: "Build - Cloning Github Repository {{ git_url }}"
     git:
       repo: "{{ git_url }}"
       dest: "/var/flaskapp/"
     register: git_status

It will log in to the docker hub and build the image. After building the image will be pushed to the docker hub. The below steps only run when there are any changes in GitHub repo.

    - name: "Build - Login to remote Repository"
      when: git_status.changed == true
      docker_login:
        username: "{{ docker_user }}"
        password: "{{ docker_password }}"

    - name: "Build - Docker image from Dockerfile"
      when: git_status.changed == true
      docker_image:
        build:
          path: "/var/flaskapp/"
        name: "{{image_name}}"
        tag: "{{ item }}"
        push: yes
        source: build
      with_items:
        - "latest"
        - "{{ git_status.after }}"

Here git repo committed id used as image tag in docker hub


Result

The docker hub will look like this below after pushing the image. image

About

Here I'm automating docker image creating using ansible. It will help for developer to reduce their workload.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published