πŸš€ Deploying a Website in Docker Container

Bhargav Teja
3 min readSep 12, 2024

--

Hello, everyone! 😊 Today, I’m going to share my experience of deploying a website using Docker on an AWS EC2 instance. This guide is beginner-friendly and will walk you through each step using easy-to-understand language. Let’s dive in! 🌊

Step 1: Setting Up an AWS EC2 Instance πŸ–₯️

  1. Create an EC2 Instance:
    Log in to your AWS account and create a new EC2 instance.
    Name it ”docker” for easy identification.
  2. To allow all incoming traffic to your Docker containers on any port, you need to update the inbound rules in your security group to all traffic any where.

3. Connect to the Instance:
β€” Use Git Bash (or any terminal of your choice) to connect to the EC2 instance.
β€” Run the command provided by AWS to connect securely via SSH.

Step 2: Installing Docker on the EC2 Instance πŸ‹

  1. Switch to the Root User:
sudo -i

2. Create a Docker Setup Script:
β€” Create a new file named `docker.sh` to automate Docker installation.
β€” Use the following commands:

# Update the package index and install necessary packages
sudo apt-get update
sudo apt-get install ca-certificates curl

# Create a directory for Docker's GPG key
sudo install -m 0755 -d /etc/apt/keyrings

# Download Docker's GPG key
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add Docker's repository to Apt sources
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

sudo service docker start
sudo docker run hello-world

3. Make the Script Executable:

chmod +x docker.sh

4. Run the Docker Setup Script:

./docker.sh

5. Verify Docker Installation:

docker --help

If you see the help commands, Docker is successfully installed! πŸŽ‰

Scenario 1: Deploying a Website Using a Template 🌐

1. Create a Dockerfile for the Website:
β€” Create a new file named `Dockerfile` and add the following content:

FROM ubuntu:latest
RUN apt update && apt install wget apache2 unzip -y
RUN wget https://www.tooplate.com/zip-templates/2133_moso_interior.zip
RUN unzip 2133_moso_interior.zip
RUN cp -r 2133_moso_interior/* /var/www/html
WORKDIR /var/www/html
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

2. Build a Docker Image:

docker build -t bhargavteja14/moss .

3. Run a Container from the Image:

 docker run --name moss -d -p 9988:80 bhargavteja14/moss

4. Access the Website:
β€” Copy the EC2 instance’s IP address.
β€” Open your browser and go to `http://<instance-ip>:9988`.
β€” Voila! πŸŽ‰ You should see your website up and running! 🌟

Scenario 2: Deploying a Website from a GitHub Repository πŸ”—

1. Create a New Dockerfile:
β€” Create a file named `Dockerfile` in another directory and add the following content:

FROM ubuntu:latest
RUN apt update && apt install apache2 git -y
RUN rm -rf /var/www/html/*
RUN git clone git@github.com:bhargavteja1999/devops88.git /var/www/html
EXPOSE 80
WORKDIR /var/www/html
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

2. Build the Docker Image:

dcker build -t bhargavteja14/cafe .

3. Run the Docker Container:

docker run --name cafe -d -p 1888:80 bhargavteja14/cafe

4. Access the GitHub Website:
β€” Copy the instance IP address and go to `http://<instance-ip>:3333` in your browser.
β€” Enjoy your GitHub repository website live! πŸš€

Conclusion 🏁

And that’s it! You’ve successfully deployed two websites using Docker on an AWS EC2 instance. πŸ’ͺ Remember, practice makes perfect, so keep experimenting and learning. If you have any questions, feel free to ask! 😊

--

--

Bhargav Teja
Bhargav Teja

Written by Bhargav Teja

πŸš€ DevOps Intern passionate about 🐧 Linux, ☁️ AWS, πŸ”§ Terraform, πŸ› οΈ Azure DevOps, 🐳 Docker, πŸ—‚οΈ Git, and πŸ’» Bash scripting.

No responses yet