Basic Deployment Solution
Introduction
In this section we simply deployed a NodeJS application on a remote EC2 instance. The NodeJS application in it simplest form does not contain any database or multiple components.
Hosting Providers
Many hosting solution exist to deploy Docker web application. The three majors providers are:
Amazon Web services (used in this course)
Microsoft Azure
Google cloud
Bind Mounts, Volumes and COPY in Production
In Development, Bind Mounts were useful since we did not have to rebuild and restart each time the image when we make changes to the source code. However, in Production environment we want to isolate all components of the web application including the source code for an independent user to be able to run our application. In Production environments we want our container to be self-sufficient. The production container should not has to rely on any surrounding set up or source code. In others words, everything that the application needs to work should be integrated into the container itself.
In Dockerfile, the COPY instruction is used to make sure that the container based on the image will include a copy of the source code.
Spin up the EC2 instance
In the AWS Console Management, we can choose Lancer une instance
.
Create a key pair to be able to connect to the remote host via SSH. Do not loose your key as you will need to shut down the instance and spin up another one. The private key is downloadable only once and should be kept secret.
As it can be seen in the Instances tab, we have one EC2 instance up and running.
Gives the proper permission on the SSH key. In our example, the example-1.pem
file.
We can then connect to our instance using it public DNS record.
Installing Docker in the VM
The amazon-linux-extras
is a cmd line utility to install software packages from the Extras library. It facilitate the installation of many software on Linux EC2 instances. The amazon-linux-extras
allows for the download of Docker.
Bring the local image to the Cloud - Docker Hub
This step is about migrating our local image to the cloud. Two approaches are possible.
Option 1
Copy all the source code and setting files on the remote VM and build the image directly on the VM.
Option 2 (privileged option)
Deploy the built image on the VM instance. Push it to Docker Hub. Pull the Docker Hub image on the VM instance, and just execute docker run
. Avoid unnecessary remote server work.
The second option requires to push a built image to Docker Hub. For this, we need to create a new repository.
Then, we have to push the built image to this repository. For that we need to build the image on our local host machine by giving it the name of the repository. But before that, we need to create a .dockerignore
file to keep certain files out of the images.
In the example below, we want to exclude all files with the .pem
extension (likely to contain private key), the Dockerfile
and the node_modules
directory.
Pushing the built image on Docker Hub
Pull the image to the EC2 instance
We can pull the image we just pushed on Docker Hub on the EC2 instance using the following command:
As it can be observed, running the docker run command pull automatically the image from Docker Hub.
At this point, the docker container is running on a remote instance.
Security Group settings
By default, EC2 instances are unavailable from the internet. By typing the EC2 public IP within a browser, we can observe that our web application running is not available from the internet.
To make our web application available from the Internet, we need to configure the security group. In the image below are listed all security groups, and one of them is attached to our EC2 instance.
We can validate the security group attached to our instance in the Security tab.
We can configure the Inbound and Outbound rules for the security group attached to our EC2 instance. Inbound traffic is all traffic from the outside to the EC2 instance while the outbound traffic corresponds to all communication from the EC2 instance to outside world.
To allow HTTP traffic to go in that instance, we need to edit the inbound rule to allow traffic to go over port 80.
We know have our application available on the internet via the public IP address of our EC2 instance.
Managing & Updating the Container/Image
Updating the code
If we want modifications of our codes to be reflected in the web application available on the internet. We will need to go over the following steps:
Rebuild the image
RE-pushed the image on Docker Hub.
Ensure that the latest image update is running on the EC2 instance
Disadvantages of this approach
A lot of steps had to be done. We have the full control and responsibility over the EC2 instance we are deploying. Developers had to make sure that their EC2 instances are secured. With this set up, the developers have all the responsability of creating, managing, updating, monitoring and scaling the EC2 instances. This approach is this very prone to management and security errors. Therefore, we might want a more automated and managed approach.
Last updated
Was this helpful?