Deploy a two-tier application on AWS using Docker.

Deploy a two-tier application on AWS using Docker.

ยท

4 min read

Introduction

Containerization with Docker has revolutionized the way we deploy and manage applications. Amazon Web Services (AWS) offers a robust platform for hosting applications in the cloud. In this blog, we'll walk through the process of deploying a two-tier application on AWS using Docker, enabling scalability, flexibility, and ease of management.

Prerequisites:

Set up your AWS account for the Ec2 instance for deployment.

Before you begin, make sure you have the following installed:

  • Docker

      sudo apt install docker.io -y
      sudo usermod -aG $USER docker
    

    After installation, restart the instance.

  • Git (optional for cloning the repository)

Setup

git clone https://github.com/your-username/your-repo-name.git

Clone above Repositories

Write the Dockerfile:

# Use an official Python runtime as the base image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# install required packages for system
RUN apt-get update \
    && apt-get upgrade -y \
    && apt-get install -y gcc default-libmysqlclient-dev pkg-config \
    && rm -rf /var/lib/apt/lists/*

# Copy the requirements file into the container
COPY requirements.txt .

# Install app dependencies
RUN pip install mysqlclient
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code
COPY . .

# Specify the command to run your application
CMD ["python", "app.py"]

Make An image from above Dockerfile

docker build -t flaskapp .
  • Now, make sure that you have created a network using the following command
docker network create twotier
  • Attach both containers to the same network so that they can communicate with each other

i) MySQL container

docker run -d --name mysql -v mysql-data:/var/lib/mysql -v ./message.sql:/docker-entrypoint-initdb.d/message.sql --network=twotier -e MYSQL_DATABASE=mydb -e MYSQL_USER=root -e MYSQL_ROOT_PASSWORD="admin" -p 3360:3360 mysql:5.7

ii) Backend container

docker run -d --name flaskapp -v mysql-data:/var/lib/mysql -v ./message.sql:/docker-entrypoint-initdb.d/message.sql --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=admin -e MYSQL_DB=mydb -p 5000:5000 flaskapp:latest

Notes

  • Make sure to replace placeholders (e.g., your_username, your_password, your_database) with your actual MySQL configuration.

  • This is a basic setup for demonstration purposes. In a production environment, you should follow best practices for security and performance.

  • Be cautious when executing SQL queries directly. Validate and sanitize user inputs to prevent vulnerabilities like SQL injection.

  • If you encounter issues, check Docker logs and error messages for troubleshooting.

Troubleshooting

I faced an issue with MySQLdb.OperationalError: (1044, "Access denied for user 'admin'@'%' to database 'myDb'")

Hear the solution.

  1. Check Your Docker MySQL Container Configuration:

    First, make sure that your Docker MySQL container is correctly configured. Ensure that you have set up the necessary environment variables (e.g., MYSQL_ROOT_PASSWORD, MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE) when running the container.

    For example, if you're using the official MySQL Docker image, you might start your container like this:

     docker run -d --name my-mysql-container -e MYSQL_ROOT_PASSWORD=root_password -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin_password -e MYSQL_DATABASE=myDb -p 3306:3306 mysql:latest
    

    Make sure that the MYSQL_USER is set to 'admin', the MYSQL_PASSWORD is set to 'admin_password', and the MYSQL_DATABASE is set to 'myDb'.

  2. Check Database Privileges:

    Log in to your MySQL server and check if the 'admin' user has the appropriate privileges to access the 'myDb' database. You can use the MySQL command-line client to do this:

     docker exec -it my-mysql-container mysql -u root -p
    

    Once you're in the MySQL shell, run the following SQL commands to grant the necessary privileges:

     GRANT ALL PRIVILEGES ON myDb.* TO 'admin'@'%';
     FLUSH PRIVILEGES;
    

    Replace 'admin' with your actual username, and '%' with the appropriate host (or use 'localhost' if you want to restrict access to the same machine).

  3. Check Host Configuration:

    If your MySQL container is running on a remote host, ensure that the MySQL server allows remote connections. You may need to modify the MySQL configuration file to allow connections from outside the localhost. Check the bind-address and skip-networking settings in your MySQL configuration.

  4. Restart the Docker Container:

    After making these changes, restart your Docker MySQL container for the changes to take effect:

     docker restart my-mysql-container
    
  5. Test the connection:

    Finally, test your connection to the MySQL database from your application to see if the error is resolved.

๐Ÿ˜Š Thank you so much for reading my blog! ๐Ÿ˜Š I hope you found it helpful and informative. If you did, please ๐Ÿ‘ give it a like and ๐Ÿ’Œ subscribe to my newsletter for more of this type of content. ๐Ÿ’Œ

I'm always looking for ways to improve my blog, so please feel free to leave me a comment or suggestion. ๐Ÿ’ฌ

Thanks again for your support! ๐Ÿ˜Š

ย