Skip to main content

Command Palette

Search for a command to run...

AWS ECS

Published
4 min read
AWS ECS

Elastic Container Service

AWS ECS

  1. Elements
  2. Concepts
  3. HandsOn
  4. Service / Load Balancer / Target Group
  5. ECS Service
  6. Backup / Restore Volumes
  7. AWS ECS with Docker Hub
  8. AWS ECS with Image from AWS ECR
  9. Dockerfile docker
  10. MySQL on AWS ECS
  11. References

1. Elements

  • Cluster
  • Task Definition
  • Task
  • Service

2. Concepts

3. Hands On

4. Service / Load Balancer / Target Group

EC2 / Target Group

EC2 Target Group

public ip — target group

Target Group Created

EC2 / Load Balancer

EC2 Load Balancer

create EC2 Load Balancer

classic loader balancer

Load Balancer Created

5. ECS Service

Service Created

6. Backup / Restore Volumes Container

[How can I backup a Docker-container with its data-volumes?
if I want to revert the container I can try to commit an image, and then later delete the container, and create a new…stackoverflow.com](https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes#:~:text=To%20backup%20a%20data%20volume,data%20for%20a%20MySQL%20server. "https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes#:~:text=To%20backup%20a%20data%20volume,data%20for%20a%20MySQL%20server.")

Backup Script

a

#!/bin/bash

#

# https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes#:~:text=To%20backup%20a%20data%20volume,data%20for%20a%20MySQL%20server.

#

# $ volume_backup.sh old_container /srv/www

# $ sudo docker stop old_container && sudo docker rm old_container

# $ sudo docker run -d --name new_container myrepo/new_container

# $ volume_restore.sh new_container

#

# This script allows you to backup a single volume from a container

# Data in given volume is saved in the current directory in a tar archive.

#

# Backup Command

# ./backup.sh mysql.keycloak.foundation /var/lib/mysql

#

CONTAINER_NAME=$1

VOLUME_NAME=$2

#CONTAINER_NAME="mysql.keycloak.foundation"

#VOLUME_NAME="keycloak_mysql_data"

usage() {

echo "Usage: $0 [container name] [volume name]"

exit 1

}

echo "### step 01"

if [ -z $CONTAINER_NAME ]

then

echo "Error: missing container name parameter."

usage

fi

echo "### step 02"

if [ -z $VOLUME_NAME ]

then

echo "Error: missing volume name parameter."

usage

fi

echo "### step 03"

sudo docker run --rm --volumes-from $CONTAINER_NAME -v $(pwd):/backup busybox tar cvf /backup/backup.tar $VOLUME_NAME

Restore Script

#!/bin/bash

#

# https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes#:~:text=To%20backup%20a%20data%20volume,data%20for%20a%20MySQL%20server.

#

#Usage can be like this:

#

#$ volume_backup.sh old_container /srv/www

#$ sudo docker stop old_container && sudo docker rm old_container

#$ sudo docker run -d --name new_container myrepo/new_container

#$ volume_restore.sh new_container

#

#

## This script allows you to restore a single volume from a container

# Data in restored in volume with same backupped path

#

# Restore Command

# ./restore.sh new_container

#

#

NEW_CONTAINER_NAME=$1

usage() {

echo "Usage: $0 [container name]"

exit 1

}

if [ -z $NEW_CONTAINER_NAME ]

then

echo "Error: missing container name parameter."

usage

fi

sudo docker run --rm --volumes-from $NEW_CONTAINER_NAME -v $(pwd):/backup busybox tar xvf /backup/backup.tar

Published in

My Dev Zone

Andre Vianna

Dec 27, 2021

·

6 min read

·

Listen

6. Upload Image to Docker Hub

  • docker ps -a
  • docker commit Imagem repo
  • docker push repo
  • docker login -username=username -email=email@docker.com
  • https://hub.docker.com

docker login -u=xxxx -p=yyyydocker login -u="${DOCKER_USERNAME}" -p="${DOCKER_PASSWORD}"

Upload Image Container

  • docker ps -a
  • docker commit Imagem repo
  • docker push repo
  • docker login -username=username -email=email@docker.com
  • https://hub.docker.com

docker login -u=xxxx -p=yyyydocker login -u="${DOCKER_USERNAME}" -p="${DOCKER_PASSWORD}"

7. AWS WCS with Docker Hub

AWS ECS with Docker Hub

[Authenticating with Docker Hub for AWS Container Services | Amazon Web Services
Docker Hub has recently updated its terms of service to introduce rate limits for container image pulls. While these…aws.amazon.com](https://aws.amazon.com/pt/blogs/containers/authenticating-with-docker-hub-for-aws-container-services/ "https://aws.amazon.com/pt/blogs/containers/authenticating-with-docker-hub-for-aws-container-services/")

8.AWS ECS with Image from AWS ECR

…Copy from AWS ECR

…Paste to AWS ECS

9. Dockerfile docker build

Dockerfile

FROM ubuntu

RUN apt-get update

RUN apt-get install --yes lynx

docker build

docker build -t .

10. MySQL on AWS ECS

[How to deploy MySQL docker image on AWS ECS?
So, I found out a mistake. THE VERY FIRST THING YOU DO - is you test that docker container on localhost and see if you…stackoverflow.com](https://stackoverflow.com/questions/57795724/how-to-deploy-mysql-docker-image-on-aws-ecs "https://stackoverflow.com/questions/57795724/how-to-deploy-mysql-docker-image-on-aws-ecs")

TaskDefinition(  
            'WordpressDatabaseTaskDefinition',  
            RequiresCompatibilities=['FARGATE'],  
            Cpu='512',  
            Memory='2048',  
            NetworkMode='awsvpc',     
            ContainerDefinitions=[  
                ContainerDefinition(  
                    Name='WordpressDatabaseContainer',  
                    Image='mysql:5.7',  
                    Environment=[  
                        Environment(Name='MYSQL_ROOT_PASSWORD', Value='root'),  
                        Environment(Name='MYSQL_DATABASE', Value='wpdb'),  
                        Environment(Name='MYSQL_USER', Value='root'),  
                        Environment(Name='MYSQL_PASSWORD', Value='root'),  
                    ],  
                    PortMappings=[  
                        PortMapping(  
                            ContainerPort=3306  
                        )  
                    ]  
                )  
            ]  
        )
self.wordpress_database_task = TaskDefinition(  
            'WordpressDatabaseTaskDefinition',  
            RequiresCompatibilities=['FARGATE'],  
            Cpu='512',  
            Memory='2048',  
            NetworkMode='awsvpc',  

            # If your tasks are using the Fargate launch type, the host and sourcePath parameters are not supported.  
            Volumes=[  
                Volume(  
                    Name='MySqlVolume',  
                    DockerVolumeConfiguration=DockerVolumeConfiguration(  
                        Scope='shared',  
                        Autoprovision=True  
                    )  
                )  
            ],  

            ContainerDefinitions=[  
                ContainerDefinition(  
                    Name='WordpressDatabaseContainer',  
                    Image='mysql:5.7',  
                    Environment=[  
                        Environment(Name='MYSQL_ROOT_PASSWORD', Value='root'),  
                        Environment(Name='MYSQL_DATABASE', Value='wpdb'),  
                        Environment(Name='MYSQL_USER', Value='wordpressuser'),  
                        Environment(Name='MYSQL_PASSWORD', Value='wordpressuserpassword'),  
                    ],  
                    PortMappings=[  
                        PortMapping(  
                            ContainerPort=3306  
                        )  
                    ]  
                )  
            ]  
        )  

        self.wordpress_database_service = Service(  
            'WordpressDatabaseService',  
            Cluster=Ref(self.ecs_cluster),  
            DesiredCount=1,  
            TaskDefinition=Ref(self.wordpress_database_task),  
            LaunchType='FARGATE',  
            NetworkConfiguration=NetworkConfiguration(  
                AwsvpcConfiguration=AwsvpcConfiguration(  
                    Subnets=[Ref(sub) for sub in VpcFormation().public_subnets],  
                    AssignPublicIp='ENABLED',  
                    SecurityGroups=[Ref(self.security_group)]  
                )  
            ),  
        )

11. References

[O que é o Amazon Elastic Container Service?
O Amazon Elastic Container Service (Amazon ECS) é um serviço de gerenciamento de contêineres altamente rápido e…docs.aws.amazon.com](https://docs.aws.amazon.com/pt_br/AmazonECS/latest/developerguide/Welcome.html "https://docs.aws.amazon.com/pt_br/AmazonECS/latest/developerguide/Welcome.html")