Set Up Docker on AWS EC2 and Connect via VS Code Remote Development

VarunrajVarunraj
2 min read

In this blog, we'll install Docker on an EC2 instance, connect to it using VS Code, install a sample Docker image, and access it from our local machine. This setup will help you improve your development workflow.

Advantages:

  • No Local Installation Required

    You don’t need to install any packages or Docker on your local machine.

  • Only VS Code is Required

    The only tool you need on your machine is VS Code, with the Remote - SSH extension.

  • Minimal Specifications Required

    Since everything runs on the EC2 instance, your machine can have minimal specifications.

Below is the architecture we are going to build.

We'll start by setting up an EC2 instance and making sure it meets all the requirements for running Docker.

Step 1: Create an EC2 instance with at least a t2.medium size. Since we will be installing Docker packages, a t2.micro might be too slow.

While creating the instance, enable key pair configuration and create a security group that allows SSH access from anywhere. Add the following code to the user data section. This code updates and upgrades the packages, installs Docker, and creates a proj folder in the home directory.

#!/bin/bash
apt-get update -y
apt-get upgrade -y
apt-get install -y ca-certificates curl
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-compose
systemctl enable docker
systemctl start docker
mkdir -p /proj

Below is a summary of my EC2 instance:

Step 2: Open VS Code and install the Remote - SSH extension if you haven't already. Press the F1 key, choose Remote SSH - Add New Host, and enter the command in this format:

ssh -i <complete-path-of-pem-file> ubuntu@ec2-public-ip

Choose the path where you want to add the configuration. You can select C:\Users\user\.ssh\config. A pop-up will appear in VS Code, click on “Connect“.

There you go! You have successfully connected to the EC2 instance using VS Code.

Step 3: Create a docker-compose.yaml file in the /proj folder and paste the following code:

services:
  qdrant:
    image: qdrant/qdrant
    ports:
      - 6333:6333

Run the following command:

docker compose up -d

On your local machine, access http://your-ec2-address:6333 and you will receive a response

Congratulations, you have successfully launched your first Docker image, and can access it from your local machine.

0
Subscribe to my newsletter

Read articles from Varunraj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Varunraj
Varunraj