We have a typical multi layered web application: a balancer, 2 web server 2 db server.

I feel we strongly need to start using a kind of Infrastructure As Code in order to replicate easily environments, testing infrastructure updates, control production servers.

I've never done that and I'm struggling to understand the workflow. I'm thinking something like this:

  • create locally my machines with docker and Dockerfiles.

  • use terraform to setup locally the infrastructure provided by docker (or docker-compose?).

  • reuse in some way the Dockerfiles to provide the production or staging machine (we don't use Docker in production, but rather AWS with EC2/AMI and RDS).

  • use ansible to deploy the application.

I'm not sure this is the way. Or maybe it's too complex

3 Answers 3


Ok so I understand that you are in AWS and Docker already and Terraform is something you want to bring into picture for IaaC. and Yes, docker for production. Now, let's start with environment: Assume you have three environments, Dev, Test and Prod. So you create three folders, Dev, Test and Prod which contain infra code for three environments. Now, I don't understand what you mean by infrastructure provided by docker but docker will contain your web servers and web application. Number of containers will depend on how large or small your application is and are you in micro services architecture. So what Terraform does for you is creating almost everything: Right from network structure(VPC, subnets, NACLs, gateways etc), autoscaling groups(I prefer this as you may want to use docker orchestration), security groups, IAM roles, policies to RDS and ECS etc.

So you can tag them in your code as per their environment and you create each of these components using Terraform modules.

So now you have Terraform code which creates infrastructure for you. Store your state file in S3 because you want consistency in your infrastructure state. Keep your database state file separate from other state files. You may want to have separate state files for your network, security groups, RDS, IAM, Cloudwatch etc.

Now comes the deployment part: Since you are in Docker, why don't you use AWS ECS? It will be easy deployment (Almost zero downtime) and orchestration will be managed by ECS itself. So now even deployment you can do using Terraform. Maintain a separate state file for this too.

You can create separate docker images for dev, test and production during your CI. If you do not have any docker tagging strategy, you may use $BUILD_NUMBER:$ENV for now. One of the purpose of using docker is that you don't need to care about where you want to run the container so identical docker images for Dev, Test and Production is almost always ok with some configuration changes.

You do not create anything locally. Use CI/CD tools such as combo of Jenkins and Git which triggers everything.

  • "provided by docker" I mean using terraform docker provider. Dockerfiles has all the scripts needed to setup an environment. But I don't want use Docker in production, as we have EC2. Furthermore, we are poor, I cannot afford AWS for dev, testing and prod, so we need something locally
    – Glasnhost
    Feb 6, 2019 at 17:33
  • But why don't you run docker on EC2? Why do you want overhead of more defects when app actually runs on AWS? Docker still needs a machine to run on: the host machine. So suppose you have one t2.medium ec2 instance. You can have 2 t2.small in lieu of that and use docker orchestration and cost will be same with better reliability and availability. @Glasnhost
    – deosha
    Feb 7, 2019 at 3:55
  • let's say we have few human resources and I don't "trust" docker for production straight away. We have EC2 machines that are currently fine. I'm being very conservative here, but not having much devops skills nor docker knowledge I'd rather go on like this and switch gradually. Docker is however very handy for testing new configurations locally
    – Glasnhost
    Feb 7, 2019 at 16:41
  • Even then, I would recommend using CI rather that creating docker containers locally. So you can setup a CI Jenkins machine and team can push code to say Git and CI automation can do that(You can use multibranch pipeline). You are good with deployment with Ansible. However, the EC2 machines for production, network, load balancers etc you should create using Terraform. And yes, you can use Ansible provider of Terraform for deployment. If you use docker provider to create containers, it's ok.
    – deosha
    Feb 8, 2019 at 2:36
  • 1
    It's quite difficult to do the development inside docker. Generally teams develop in their local machines, push to git, CI process ensures that everything is fine and CI takes place on uniform system. Docker is certainly not for development from scratch but for packaging the already developed code
    – deosha
    Feb 12, 2019 at 2:52

Here two parts: use IaC tools like Terraform to create environments and use Docker to deploy applications.

1) You can use Terraform with workspaces to make production and development. Maybe use Packer to prepare AMI images to reduce time for building.

2) Decide what CI/CD pipeline you will use to deploy apps. And first of all here you should know what orchestration tool you will use for Docker containers - compose, swarm, nomad, kubernetes.


If I understand your need:

  • Need Infrastructure as Code.
  • Configuration management.

Terraform is a great tool for creating your infrastructure. It has API support for AWS, GCP and almost all the cloud providers. Now that your infrastructure is ready, you need to configure the same using tools like (CHEF, Ansible or Puppet). I have personally bootstrapped the nodes created by Terraform to CHEF and have run cookbooks.

  • Ok I understand that I provide the infrastructure with terraform and "provide" the machines with something like Ansible. But I'm think already about using Docker for local development, to test machine upgrades and environments. So I guess I must repeat the Dockerfile instructions almost the same for Ansible when I deploy?
    – Glasnhost
    Feb 11, 2019 at 22:10
  • What about trying with Vagrant? Docker files is also an option, but how do we network between the container , Docker-compose?
    – Sunil
    Feb 12, 2019 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.