Setup and run website in Docker Container with Terraform
In this post, I will share on how to create the simple Hello World Nodejs Docker container and deploy it with Terraform in your local machine. To test on this demo, you need to make sure that you have docker and terraform installed, if not, you may refer to their official website on how to install docker and terraform.
To complete the demo, it’s only take few simple steps:
- Create the app.js file to write our simple Nodejs code and listen on port 8080
- Create the Docker file to instruct the docker to create our docker image in local machine
- Create main.tf terraform file to automate our deployment using terraform
- Destroy the docker container
1. Create app.js
Let’s start with the app.js file, just copy the following sample Hello World code in your project directory
const express = require('express')
const app = express()
const port = 8080 app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
On terminal, go to your project directory, create the package.json file using the following command:
$ npm init -y
$ npm install express
2. Create Dockerfile
Next we create the docker file with the following content
FROM node:10
RUN mkdir /myapp
# Create app directory
WORKDIR /myapp
COPY ./package.json /myapp/package.json
COPY ./package-lock.json /myapp/package-lock.json
RUN npm install
RUN npm install -g nodemon
# Bundle app source
COPY . /myapp
EXPOSE 8080
CMD [ "nodemon", "app.js" ]
The docker file above basically instruct the docker to use node:10 as our base image, then create the /myapp directory, copy the package.json and package-lock.json into the container.
Run the npm install and expose port 8080 to the host, later on the host we may need to map the external port into the container port 8080.
$ docker build -t hello_world_app .
Build the docker image using the command above.
3. Create terraform file and deploy the resource
Next we will start to write our terraform code in the file call main.tf, on this file, we will instruct terraform to run a container for us by input the image name, volumes as well as the port mapping from the host to the container.
You need to make sure that you already have the terraform install in your machine, refer to the terraform official portal if you need assist on how to install.
resource "docker_container" "hello_world_app" {
image = "hello_world_app:latest"
name = "hello_world_app"
restart = "always"
volumes {
container_path = "/myapp"
# replace the host_path with full path for your project directory starting from root directory /
host_path = "/path/to/your/project/directory"
read_only = false
}
ports {
internal = 8080
external = 8080
}
}
Until now, everything is ready and good to go.
In order to use terraform to deploy our infrastructure, we first need to initialize the project by using following command:
$ terraform init
Terraform will download all the dependency base on your .tf file.
Terraform plan allow you to have a clear picture on what are the resources that going to deploy by the code, at the same time, terraform also refresh the state of your code to find out the difference between your code with the deployed resource on the target location.
$ terraform plan
If you are happy with all the thing, you can deploy the code using the following command.
$ terraform apply --auto-approve
To double check is the docker container already run in our local machine, you can use the command
If everything working fine, you should see 1 container running. To test on our container is that ready to receiving any request, we can issue the curl to the container to get the response.
$ curl localhost:8080 Hello World!
If you get the response of “Hello World!” it’s mean that the node.js server is response to your request.
4. Terraform destroy to clean up the environment
Once you done the testing, it’s always a best idea that you should clean up your environment using terraform command
$ terraform destroy
This is a very simple demo on how you can use terraform with docker to deploy something in your local machine, there are unlimited of possibility of doing thing using docker and terraform, to learn more about what can terraform, you can visit the official terraform docker provider at: https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs
Hope my sharing help you to know what’s docker and terraform.
Originally published at https://tech.david-cheong.com on January 10, 2021.