This repo provides an example of how to use packer to build a generic nginx server from either a CentOS or Ubuntu container image. The basic workflow is as follows:
- Packer pulls the desired container image
- It uses a shell provisioner for the Ubuntu image to ensure that
pythonis installed. CentOS bundles python since there are a few dependencies on python built into the distribution. - Runs an Ansible playbook against the container to configure the system. The
ansible-remotemethod is utilized to prevent any remnants of Ansible from being installed on the container. - Tags and pushes the container to a Docker Hub account
There are a few prerequisites needed to make it all go:
- Packer - https://packer.io/docs/install/index.html
- Ansible 2.7+ - https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
- Docker - https://docs.docker.com/install/
- A Docker Hub account - https://hub.docker.com/signup
The packer template supports a few user supplied variables:
container_image(required) - should be eithercentosorubuntuto pull the corresponding image from Docker Hubcontainer_tag(optional) - defaults tolatest, a more fun value likegit rev-parse --short HEADcould be used to facilitate promotionsdocker_hub_user(required) - the Docker Hub user account to push containers to
The image can be built and shipped via the following command:
packer build -var container_image=<centos|ubuntu> -var docker_hub_user=<docker_hub_username> template.jsonThe build process assumes that the docker daemon is already logged in to the Docker Hub account. This process is separated from the build step so that:
- Credentials can be isolated from the stack repository
- Credentials could be passed in to the build process in a pipeline and handled in a more secure fashion
Once the container is built, it could be run as shown below. This repositry includes a sample html folder containing an index.html file for demonstration purposes.
docker run --name nginx-test -v <path_to_repo_checkout>/html:/html -p 8888:80 -d <docker_hub_username>/<centos|ubuntu>-nginx:latestOnce the container is running it can be viewed in a web browser at http://127.0.0.1:8888/.
The nginx daemon writes its logs to its stderr stream so that they can be accessed via the docker daemon or the container platform's designated logging system.