Docker compose

Docker compose

Docker Compose is a tool for defining and running Docker programs that use multi-containers. With Docker Compose, we use a YAML file to set up the necessary services for the program. Finally, with one command, we will create and start all services from those settings.

Using Compose typically involves the following three steps:

  • Declare the program’s environments in Dockerfile.

  • Declare the necessary services for the program in the docker-compose.yml file so that the services can run together in an environment.

  • Run the docker-compose up command to start Compose and run the program.

Compose has commands that allow you to manage your program’s lifecycle:

  • Start, Stop and Rebuild the service.

  • View the status of running services.

  • View the log output of the running service.

  • Run a one-off command in a service.

Benefits of using Compose

  • Create multiple isolated environments in one host: Compose isolates the environments of projects to ensure they don’t conflict with each other, and makes it easy to make copies of an environment.

  • Recreate only changed containers: Compose will recognize the services that have not changed and reuse the containers corresponding to that service.

  • Adjust variables used for environments: Compose uses variables in the Compose file for environments. So with different environments or users, it is possible to adjust variables when using Compose to set up services.

Practice

  1. Access the connected EC2 interface.
  • Use the command to clone the code with git (you clone an additional repo on your computer for easy editing for the next steps)
git clone https://github.com/AWS-First-Cloud-Journey/AWS-FCJ-Management.git

Image Node

  1. Check if you have cloned the repo
ls

Image Node

  1. Then create a file .env to configure the database.
touch .env
en .env

Then execute the configuration and save it with the command !wq

Image Node

  1. Review the configuration file.
cat .env

Image Node

  1. Then we see the contents of the file docker-compose.yaml
cat docker-compose.yaml

Image Node

  1. Install Docker Engine
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user

Image Node

  1. Automatically enable docker
sudo chkconfig docker on

Then install the latest docker-compose version

sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker- compose

We will give permission to docker-compose

sudo chmod +x /usr/local/bin/docker-compose

Check the installation is successful

docker-compose version

Image Node

  1. After successfully installing docker-compose. We execute the command to run docker compose
docker-compose up --build

Image Node

  1. We will check Task Definitions in ECS interface

Image Node

  1. Run command and get parameters for configuration
aws ecs describe-task-definition --task-definition aws-fcj-task-def
  • Make configuration file taskdef.json and file appspec.yaml

Image Node

  1. We also clone a repo on the computer to facilitate editing and configuring the template. Perform configuration based on Task Definition information.

Image Node

  1. To avoid the limit rate docker error at build time, we will replace FROM node:16-alpine with the image we built in the previous step.

Pipeline

  1. Configuration file buildspec.yaml

Pipeline

  1. File appspec.yaml once configured.

Pipeline

  1. File taskdef.json after configuration.

Pipeline