Friday, November 15, 2024

How to Install Docker and LocalStack on CentOS Stream

 In this guide, we'll walk through installing Docker and LocalStack on CentOS Stream. LocalStack is a widely-used tool for simulating AWS services locally, helping developers test applications that interact with AWS without relying on live AWS resources. Below, you'll find a step-by-step process for setting up Docker, LocalStack, and configuring the AWS CLI to work with LocalStack. Let’s dive in!

Prerequisites

Before we start, ensure you have access to a CentOS Stream server and that you have root or sudo privileges.

Official Documentation

For detailed insights, refer to the official documentation:


Step 1: Removing Old Docker Installations

First, remove any older Docker packages to avoid conflicts with the new Docker installation.


sudo yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

This command ensures any older versions of Docker and related packages are removed.


Step 2: Installing Docker

  1. Install yum-utils
    yum-utils provides the yum-config-manager utility, which helps manage repositories.


    sudo yum install -y yum-utils
  2. Add Docker’s Official Repository
    Adding the Docker repository enables us to install Docker from official sources.


    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  3. Verify the Repository
    Check if Docker's repository was successfully added.


    yum repolist

    The output should list docker-ce-stable as one of the repositories.

  4. Install Docker Packages
    Install Docker’s core packages, including Docker CE (Community Edition), the CLI, and additional plugins.


    sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  5. Start Docker
    Once Docker is installed, start the Docker service.


    sudo systemctl start docker
  6. Verify Docker Installation
    Confirm that Docker was installed successfully.


    docker --version

    Expected output:


    Docker version 27.2.0, build 3ab4256

Step 3: Installing LocalStack

LocalStack can be downloaded as a standalone CLI, which we’ll place in /usr/local/bin.

  1. Download LocalStack CLI
    Navigate to the Downloads directory and download the CLI tarball.


    cd /home/<user>/Downloads wget https://github.com/localstack/localstack-cli/releases/download/v3.7.0/localstack-cli-3.7.0-linux-amd64-onefile.tar.gz
  2. Extract and Install the CLI
    Extract the downloaded tarball and move it to /usr/local/bin to make it accessible.


    sudo tar xvzf localstack-cli-3.7.0-linux-amd64-onefile.tar.gz -C /usr/local/bin
  3. Start LocalStack
    Start LocalStack in detached mode. If you have a LocalStack authentication token, set it with the LOCALSTACK_AUTH_TOKEN environment variable.


    export LOCALSTACK_AUTH_TOKEN="<localstack-auth-token>" ACTIVATE_PRO=0 localstack start -d
  4. Verify LocalStack Status
    Check that LocalStack is running.


    localstack status docker container ls -all

    You should see an output similar to this:


    Runtime version │ 3.7.2.dev15 Docker image │ tag: latest, id: bb743b345416 Runtime status │ ✔ running (name: "localstack-main", IP: 172.17.0.2)

Step 4: Configuring the AWS CLI

To interact with LocalStack’s AWS service mock, you’ll need to set up the AWS CLI.

  1. Install pip and AWS CLI
    First, install pip and then use it to install the AWS CLI.


    sudo yum install -y python3-pip pip install awscli
  2. Verify AWS CLI Installation
    Ensure that the AWS CLI is installed.


    aws --version

    Expected output:


    aws-cli/2.15.31 Python/3.9.19 Linux/5.14.0-503.el9.x86_64
  3. Configure AWS CLI
    Run aws configure to set up a profile. For LocalStack, use placeholder values for the AWS credentials.


    aws configure

    Provide the following details:

    • AWS Access Key ID: testtest
    • AWS Secret Access Key: testtest
    • Default region: ap-southeast-3
    • Default output format: json
  4. Modify AWS Configuration Files
    To integrate AWS CLI with LocalStack, update the ~/.aws/config file:


    vi ~/.aws/config

    Add the following:


    [default] region = us-east-1 output = json endpoint_url = http://localhost:4566

    Similarly, update the ~/.aws/credentials file with your credentials:


    vi ~/.aws/credentials

    Add:


    [default] aws_access_key_id = test aws_secret_access_key = test
  5. Verify Configuration
    Run the following command to confirm AWS CLI is set up correctly:


    aws configure list

Step 5: Testing the LocalStack Setup

Finally, let's confirm that LocalStack is configured correctly by creating and listing an S3 bucket.

  1. Create an S3 Bucket


    aws s3 mb s3://my-example-bucket
  2. List S3 Buckets


    aws s3 ls

    Expected output:


    2024-11-05 15:46:28 my-example-bucket
  3. Confirm Bucket Region


    aws s3api head-bucket --bucket my-example-bucket

    You should see a response similar to:


    { "BucketRegion": "us-east-1" }

Conclusion

By following these steps, you've successfully set up Docker, LocalStack, and AWS CLI on CentOS Stream, enabling you to develop and test AWS-dependent applications locally. LocalStack provides a convenient way to simulate AWS services, making it easier to work offline and avoid incurring AWS costs.

0 comments:

Post a Comment