Deploy Angular using Docker , Ansible and Packer
By default to create a Docker image we can start by creating a Dockerfile, after with docker build we can create an image. On this tutorial I'll explain how we can create a docker image using Packer.
What's Packer
Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration. Packer is lightweight, runs on every major operating system, and is highly performant, creating machine images for multiple platforms in parallel. Packer does not replace configuration management like Ansible, Chef or Puppet. In fact, when building images, Packer is able to use tools like Ansible , Chef or Puppet to install software onto the image.
what is Ansible
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration and provisioning.
Ansible advantages
- Free. Ansible is an open-source tool.
- Very simple to set up and use. No special coding skills are necessary to use Ansible’s playbooks (more on playbooks later).
- Powerful. Ansible lets you model even highly complex IT workflows.
- Flexible. You can orchestrate the entire application environment no matter where it’s deployed. You can also customize it based on your needs.
- Agentless. You don’t need to install any other software or firewall ports on the client systems you want to automate. You also don’t have to set up a separate management structure.
- Efficient. Because you don’t need to install any extra software, there’s more room for application resources on your server.
Why Angular:
I'm an angular developer for 3 years, so it's very easy to test tools using an angular application on production.
Let's get started
- Create template.json
{ "variables": { "ansible_host": "default", "ansible_connection": "docker" }, "builders": [{ "type": "docker", "image": "ubuntu:16.04", "commit": "true", "run_command": [ "-d", "-i", "-t", "--name", "{{user `ansible_host`}}", "{{.Image}}", "/bin/bash" ] }], "provisioners": [{ "type": "shell", "inline": [ "apt-get update", "apt-get install python -yq" ] }, { "type": "ansible", "user": "root", "playbook_file": "./playbook.yml", "extra_arguments": [ "--extra-vars", "ansible_host={{user `ansible_host`}} ansible_connection={{user `ansible_connection`}}" ] } ], "post-processors": [ [{ "type": "docker-tag", "repository": "assakra/demoansible", "tag": "0.3" }] ] }
The docker Packer builder builds Docker images using Docker. The builder starts a Docker container, runs provisioners within this container, then exports the container for reuse or commits the image.
Packer builds Docker containers without the use of Dockerfiles. By not using Dockerfiles, Packer is able to provision containers with portable scripts or configuration management systems that are not tied to Docker in any way. It also has a simple mental model: you provision containers much the same way you provision a normal virtualized or dedicated server. For more information, read the section on Dockerfiles.[packer-documentation]
2. Create Ansible playbook.yml
--- - name: A demo to run ansible in a docker container hosts: all tasks: - name: Copy files to remote host copy: src: dist/ dest: /var/www/html owner: www-data group: www-data mode: 0755
I used Ansible copy module to copy angular dist folder content to /var/www/html.
3. Build Packer template
$ packer build template+ansible.json
$ docker run -it assakra/demoansible:0.3 /bin/bash $ cd var/www/html $ ls