Container Concepts: Steps for Automation
Hey! guys this post is about the containers specific commands examining each and the best outputs are collected. Hope this could help you in preparing automation in docker pipeline CI/CD scripts, and you feel enjoy this exploring post helpful!Docker is a platform for developers and sysadmin to develop, deploy and run applications with containers. The use of Linux containers to deploy applications is called containerization.
Docker Container Concepts |
Containerization is increasingly popular because containers are :
- Flexible: Even the most complex applications can be containerized.
- Lightweight: Containers leverage and share the host kernel.
- Interchangeable: You can deploy updates and upgrades on-the-fly.
- Portable: You can build locally, deploy to the cloud, and run anywhere.
- Scalable: You can increase and automatically distribute container replicas.
- Stackable: You can stack services vertically and on-the-fly.
How Images and containers are connected?
An image is an executable package that includes everything needed to run an application--the code, a runtime, libraries, environment variables, and configuration files.A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps
Differences in Containers and virtual machines
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.
Docker Container Deep Dive
Now let's start exploring the commands about the container specific options from the creation to listing to controlling them to saving to removing from the docker host.Docker Container related command Help
To learn any CLI tool best way is to check the manual or help option. So lets see more options realted to container.docker container --help
docker container command help |
How can I see the docker Container Size?
Get the Container Size with two options -s or --size sample following:Docker Container Size |
How to create containers?
We can create a container using 'container create' command, which internally calls 'docker pull' if the image is in not available on that docker engine.docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]
Here note that 'create' command will not run the any container, it's status would be 'Created' state.
docker container create --name test-nginx --interactive --tty nginx
docker create examples |
Now let's check the Containers status:
Here we can select the columns which are required to fetch the container details.
docker ps --all --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}"
docker container list with custom format |
You can create the container and attach it to STDIN, STDOUT or STDERR in the following example showing that a tomcat container created and then attach to STDIN
Container Create command execution |
How to start the Docker Container?
Any container which is just created state that could be started using container start command.To start one or more stopped containers we can use the following :docker start [OPTIONS] CONTAINER [CONTAINER...]
docker start modest_shannon
How to Stop the Container?
Similar to the start subcommand we have stop The following command to stop a running containerdocker container stop test-nginx
If there is no issues, this is will returns the name of the container or container id whatever you pass as argument.
How to Restart the Docker container?
This will stop the running container and start the container then the status of the container will be in 'Up'.
docker container restart test-nginx
docker container create, start, stop, restart |
How to remove/Delete a docker Container?
The docker container rm command will be used to remove the container from the docker engine. If the container is running status then it will refuse to remove it. So before you remove the container ensure that is stopped means Status should be Exited. We can delete one or more containers at a time as :docker container rm my_container
Kill Containers
As situation demands that if the docker container crashed due to internal resource problems such as insufficient memory or load for execution is in uncontrolled then container might fail to serve. In such cases we can use docker container kill command will cleanup those jambe container, we can also remove forcefully using kill subcommand all running containers.docker container kill $(docker ps -q)
One command to sweep/Delete all containers that are not running on the docker engine.
docker container rm $(docker ps -a -q)
How to Inspect a docker container?
To look for the storage driver, volume details or network ip address and lots of information.docker container inspect C1_web
Docker container list
List of containers can be output with the aliases: ls, ps, list That means all the three commands will give the same output.docker container ls docker container ps docker container list
Container list command output |
Docker attach and detach containers
Allows one terminal to attach a running container. It allows you to connect to the process' STDIO in another terminal.From the Docker docs following topper Example: Let's run in detach mode OracleLinux container, where inside the container by running top command with -b that is infinite.
$ ID=$(sudo docker run -d oraclelinux /usr/bin/top -b) Unable to find image 'oraclelinux:latest' locally latest: Pulling from library/oraclelinux 04e172e76262: Pulling fs layer 04e172e76262: Verifying Checksum 04e172e76262: Download complete 04e172e76262: Pull complete Digest: sha256:27c0c3e5ca8ce6e9ef8121c106de2d8ad28067074d7f5f5ce4e5cb78a0f07b3d Status: Downloaded newer image for oraclelinux:latest [node1] (local) root@192.168.0.23 ~ $ docker attach $ID top - 05:31:27 up 15 days, 2:52, 0 users, load average: 5.67, 6.12, 4.91 Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie %Cpu(s): 12.0 us, 22.3 sy, 0.0 ni, 64.6 id, 0.1 wa, 0.0 hi, 1.1 si, 0.0 st KiB Mem : 32929708 total, 7670004 free, 9502344 used, 15757360 buff/cache KiB Swap: 0 total, 0 free, 0 used. 21260512 avail Mem [node1] (local) root@192.168.0.23 ~ $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e29921306526 oraclelinux "/usr/bin/top -b" 7 seconds ago Up 6 seconds eloquent_wescoff
Well, Ctrl+C (or Ctrl+\) should detach you from the container but it will kill the container because your main process is a bash or bach command.
docker run -ti -d --name test python:3.6 /bin/bash -c 'while [ 1 ]; do sleep 30; done;' docker container lsLet's attach to the test container
docker attach testType Ctrl+p, Ctrl+q will help you to turn interactive mode to daemon mode.
Typical detach keys sequence examples here.
docker attach --detach-keys="ctrl-a" testPlease note here that escape sequence ^P^Q does work, BUT only when -t and -i is used to launching the container.
For example:
applications have no system dependencies
updates can be pushed to any part of a distributed application
resource density can be optimized.
With Docker, scaling your application is a matter of spinning up new executables
further links you could refer
1 comment:
Thanks for sharing this wonderful and fantastic and great information.
AngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
Post a Comment