Usage of CLI-suite for container
Terminology
Container: a docker container
Container type: a term describing a set of containers that have the same role, e.g. api, core, booking-engine. It is defined by a corresponding Task Definition.
ECS Service: manages the containers of a certain container type.
Image: a docker image
Docker Cluster: A logical entity consisting of a number of docker hosts
Task Definition: an extended, ECS-specific docker compose file (JSON)
ECS Task: Instance of a Task Definition, usually represents a single container
Desired Tasks: A number of containers based on the same image (horizontal scale)
Revision: Version of an ECS Task, that has a set of properties, such as an image-URL pointing to an image in a registry
Registry: a system for storing and managing images
Repository: a logical space seperator within the registry to separate images e.g. by type or purpose
The deployment process
Docker Container Deployment describes a process of updating a fleet of running docker containers that are operated via Amazon AWS EC2 Container Service (ECS).
Deployment strategies
Per default a rolling update is used, starting new containers based on a new docker image. The method is explained below in the context of a web application. Knowledge of typical ECS terms are useful. Besides rolling updates it is also possible to have replacing updates. This means running containers of a container type are terminated and then new ones are launched based on the submitted image.
The main difference between replacing and rolling updates is that deployments run with 0 downtime only in the later case. However, additional capacity has to be provided permanently. Thus increasing cost of permanently running docker hosts in the cluster.
Process example
Initiation of deployment from the bastion host via CLI for a Container Type providing a new image-URL
Creation of a new ECS Task Definition with the new image-URL, which has its own version number (revision)
Update of the respective ECS Service managing the Containers of the Container Type of consideration
Creation of new Containers based on the new image-URL in the Docker Cluster
All new Containers are taken into Service from the Load Balancer
Connections to all old containers are drained by the Load Balancer, meaning established ones are allowed to finish, bu no new ones are established to them
Shutdown of all old containers
Finish
Safeguards
Several mechanism are in place to recognize success and handle failure and rollback:
Container state verification
A deployment is successful if all new containers are recognized as running and the Load Balancer has taken them into service successfully
Image-URL verification
Checking to given Image-URL to be a valid String
If the docker registry images should be pulled from is part of your environment (within the same AWS account) we try to locate the given image
Native docker commands
The following commands can be called via "sudo" by default, meaning in every environment:
docker ps
,docker ps -a
docker images
,docker images -a
docker logs [OPTION] CONTAINERID
docker exec -it CONTAINERID bash
,docker exec -it CONTAINERID sh
docker stats
,docker stats CONTAINERID
docker inspect CONTAINERID
For non-productive environments root360 can also enable access to the following commands e.g. to support debugging and analysis:
docker run
docker pull
docker start
docker create
You can find further details about the individual commands in docker cli reference.
Example usage scenarios
A great example is the docker start
command. If you deployed a new container image to your test environment and it wouldn't come up you are in a dilemma.
On the one hand you want to look into the failing container, but on the other hand AWS ECS keeps trying to launch and terminate containers. Thus it will constantly kill the container you are actually want to keep a little bit longer for analysis.
By using docker start
to run a container image AWS ECS has created on a docker host before, you can do so without AWS ECS interfering anymore.
CLI-suite container commands
All commands operating on docker hosts and containers and the tasks to manipulate them are available in the r3 container
subcommand. A general description how the r3 CLI Suite works can be found in CLI-Suite (root360)
In the following actual usage examples are presented.
What are the general command line options?
r3 container overview
USER@BASTIONHOST:~$ r3 container --help
usage: r3 container [-h] [-d] [-q] {deploy,list,templates,instances,status,image,events,logs,show} ...
options:
-h, --help show this help message and exit
-d, --debug Run in debug mode. (default: 0)
-q, --quiet Run in quiet mode (only errors are shown). (default: False)
command overview:
{deploy,list,templates,instances,status,image,events,logs,show}
deploy Deploy containers to hosts.
list List running containers.
templates List available container templates.
instances List container instances.
status Show container status.
image Show container image.
events Show container event log.
logs Show container logs.
show Show detailed information about containers.
Show which container is running on which host including resources overview as table
r3 container list
USER@BASTIONHOST:~$ r3 container list
+---------------------------------------+----------------------+--------------+-----------------+---------+-----------------+------------+----------+---------------------+
| Container Name | Image | Reserved CPU | Reserved Memory | Status | Running/Desired | LaunchType | Host | LaunchTime |
+---------------------------------------+----------------------+--------------+-----------------+---------+-----------------+------------+----------+---------------------+
| company-project-setting-hello-ec2 | hello-ec2:1.0 | 500 | 1000 | running | 2 / 2 | EC2 | 10.x.y.z | 2021-05-04_00:59:30 |
+---------------------------------------+----------------------+--------------+-----------------+---------+-----------------+------------+----------+---------------------+
| company-project-setting-hello-ec2 | hello-ec2:1.0 | 500 | 1000 | running | 2 / 2 | EC2 | 10.x.y.z | 2021-05-04_00:59:41 |
+---------------------------------------+----------------------+--------------+-----------------+---------+-----------------+------------+----------+---------------------+
| company-project-setting-hello-fargate | hello-fargate:latest | 512 | 1024 | running | 1 / 1 | FARGATE | 10.x.y.z | 2021-05-04_00:21:30 |
+---------------------------------------+----------------------+--------------+-----------------+---------+-----------------+------------+----------+---------------------+
| company-project-setting-hello-multi | hello-fargate:latest | 512 | 1024 | running | 1 / 1 | FARGATE | 10.x.y.z | 2021-05-04_00:21:30 |
| | hello-ec2:1.0 | | | | | | | |
+---------------------------------------+----------------------+--------------+-----------------+---------+-----------------+------------+----------+---------------------+
Show which container is running on which host including resources overview as JSON
Using --format json
you get JSON output which can be parsed using e.g. jq
command line tool (Manual).
r3 container list --format json
USER@BASTIONHOST:~$ r3 container list --format json | jq
{
"instances": [
{
"instance_ip": "10.x.y.z",
"mem_registered": 7624,
"mem_remaining": 4712,
"cpu_registered": 4096,
"cpu_remaining": 2096,
"tasks": [
{
"name": "company-project-setting-hello-ec2",
"images": [
"hello-ec2:1.0"
]
"cpu": "500",
"memory": "1000",
"status": "running"
},
{
"name": "company-project-setting-hello-ec2",
"images": [
"hello-ec2:1.0"
]
"cpu": "500",
"memory": "1000",
"status": "running"
},
{
"name": "company-project-setting-multi",
"images": [
"hello-fargate:latest",
"hello-ec2:1.0",
]
"cpu": "500",
"memory": "1000",
"status": "running"
}
]
}
]
}
Show all containers running on FARGATE using JSON and JQ
Show all containers including EC2 host ip address using JSON and JQ formated as CSV
Show all broken (not running) containers using JSON and JQ
Show all container instances and their services as table
Show all container instances and their services as JSON (more details)
Find instance where specific service is running using JSON and JQ
Show container logs for all containers
r3 container logs
Show container logs for specific container
r3 container logs --service ServiceName
Which container services can be deployed?
r3 container list --templates
How to start a new deployment for container-type x?
You can start deployments in background using --detach
. To keep track of a detached deployment, you can use the r3 deployments --service ServiceName --deployment <id> --watch
Single container tasks
r3 container deploy --service Service-Name --image-url Registry/Repository:Tag
Multi container tasks
r3 container deploy --service Service-Name --image-url container1=Registry/Repository:Tag container2=Registry/Repository:Tag
How to list deployments?
r3 deployments --service ServiceName
On which image do the currently running containers of type X base?
r3 container image --service Service-Name
Show the latest events for a given container type
r3 container events --service ServiceName
Show the deployment status for a container-type
Show detailed information about all containers of container-type X (see following example for explanation)
r3 container status --service Service-Name
The important points here are:
"lastStatus" describes the current status
"desiredStatus" describes the target state
"stoppedReason": is only to be found with stopped containers and represents in detail the reason for the state.
Related tutorials
Related components
intermediate
- 1 Terminology
- 2 The deployment process
- 2.1 Deployment strategies
- 2.2 Process example
- 2.3 Safeguards
- 3 Native docker commands
- 4 Example usage scenarios
- 5 CLI-suite container commands
- 5.1 What are the general command line options?
- 5.2 Show which container is running on which host including resources overview as table
- 5.3 Show which container is running on which host including resources overview as JSON
- 5.4 Show all containers running on FARGATE using JSON and JQ
- 5.5 Show all containers including EC2 host ip address using JSON and JQ formated as CSV
- 5.6 Show all broken (not running) containers using JSON and JQ
- 5.7 Show all container instances and their services as table
- 5.8 Show all container instances and their services as JSON (more details)
- 5.9 Find instance where specific service is running using JSON and JQ
- 5.10 Show container logs for all containers
- 5.11 Show container logs for specific container
- 5.12 Which container services can be deployed?
- 5.13 How to start a new deployment for container-type x?
- 5.13.1 Single container tasks
- 5.13.2 Multi container tasks
- 5.14 How to list deployments?
- 5.15 On which image do the currently running containers of type X base?
- 5.16 Show the latest events for a given container type
- 5.17 Show the deployment status for a container-type
- 5.18 Show detailed information about all containers of container-type X (see following example for explanation)
root360 Knowledge Base - This portal is hosted by Atlassian (atlassian.com | Privacy Policy)