How to build Docker image and push to AWS ECR for Node.js application

·

4 min read

What is Docker?

Docker is an open-source platform for building, shipping and running containers.

Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

By doing so, they can run the containerized application on any other Linux machine regardless of any customized settings that the machine may have that could differ from the machine used for writing and testing the code.

Some key points about Docker:

- Docker allows developers to package applications into containers - standardized executable components that include everything needed to run the app.

- Containers isolate applications from one another and bundle their software, runtime, system tools, system libraries and settings.

- Docker images are the basis of containers - they contain the contents and settings that will be used to create a container.

- Docker utilizes a client-server architecture. The Docker daemon runs on a host machine and Docker clients communicate with the daemon using REST API.

- Docker provides the ability to run multiple isolated containers on a single host - these containers share the host operating system kernel and are run as isolated processes.

- Docker containers are very lightweight - they utilize resource isolation features of the Linux kernel like cgroups and namespaces. This makes them more efficient in terms of disk space and memory usage compared to virtual machines.

- Docker simplifies deployment of applications by allowing you to package up your code and dependencies into a container that can run on any Docker host.

So in short, Docker provides a simple and efficient way to create lightweight isolated environments to develop, ship and run applications.

Basic Command of Docker:

Docker provides several useful commands to manage containers and images. Here are some of the most basic and important Docker commands:

docker run

This command is used to create and run a container from an image. It has the following basic syntax:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Some of the most common options are:

  • -d: Run the container in detached mode

  • -it: Run the container interactively

  • --name: Give the container a name

For example:

docker run -d --name webserver nginx

This will run an nginx container in detached mode and name it webserver.

docker stop

Stops a running container. For example:

docker stop webserver

docker start

Starts a stopped container. For example:

docker start webserver

docker ps

Lists running containers. You can use docker ps -a to see all containers, running or stopped.

docker rm

Removes a container. For example:

docker rm webserver

docker pull

Pulls an image from a registry. For example:

docker pull nginx

docker images

Lists images on your system.

docker build

Builds an image from a Dockerfile. For example:

docker build -t myimage .

This will build an image named myimage from the current directory (.).

Write Your first Docker File:

StartFragmentFirst, you'll need to install Docker on your system and create an AWS ECR repository where you'll push the Docker image.

Then, follow these steps:

1. Create a Dockerfile in the root of your Node.js application. For example:

FROM node:12

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD [ "npm", "start" ]

2. Create a .dockerignore file and add node_modules to it.

3. Build the Docker image:

docker build -t my-node-app .

This will create a Docker image named my-node-app using the Dockerfile.

4. Authenticate Docker with your AWS account:

aws ecr get-login-password --region region | docker login --username AWS --password-stdin account_id.dkr.ecr.region.amazonaws.com

5. Tag the image with your ECR repository URI:

docker tag my-node-app:latest account_id.dkr.ecr.region.amazonaws.com/my-node-app:latest

6. Push the image to ECR:

docker push account_id.dkr.ecr.region.amazonaws.com/my-node-app:latest

7. You can then run the containerized application on ECS Fargate or EC2 instances.

That's the basic process to build and push a Docker image for your Node.js application to AWS ECR. Let me know if you have any other questions!EndFragment