Showing posts with label docker container. Show all posts
Showing posts with label docker container. Show all posts

Monday, April 14, 2025

Docker Command Tricks & Tips

 

Docker container command Tips & Tricks


Here my idea is to use the Unix/Linux 'alias' command for most those common docker container, network, volume sub-commands to form as shorten to give you more productivity while working on developing the docker images and playing around the newly constructing containers. This trick work on bash, zsh shells.

Improve productivity by using alias for docker cli
Improve Productivity with smart work alias for Docker commands
 
First examine the docker container listing with the powerful option '--format'
docker container ps -s \
  --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Size}}"
docker ps command

To get the logs of any applications that runs in containers we can use the following:
alias dkrlogs='docker logs'
alias dkrlogsf='docker logs -f '

docker logs with alias trick

List of the images
alias dkri='docker image ls'
docker image list alias trick

The container list
alias dkrcs='docker container ls'
docker container list alias trick

Remove the 'Exited' status container
alias dkrrm='docker rm'
docker rm alias trick

Docker top list to see the container inside process ID

alias dkrtop='docker top'
docker top alias trick

All the above nice commands will be helpful for the pipelines, Containerization makes CI/CD seamless. 

We could add the following alias lines to our .bashrc or .bash_profile or .bash_aliases where you can add all simplified docker command as alias:
alias cleanall='docker container rm $(docker ps -a -q)'
alias dkrps='docker ps --all --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}"'

Improved version of Bash aliases for Docker command

After a deep search on the internet for multiple docker alias examples, prepared the following to improve productvity.
## Basic Docker Aliases
alias dps="docker ps"       # List running containers.
alias dpa="docker ps -a"    # List all containers, including stopped ones.
alias di="docker images"    # List all Docker images.
alias dip="docker container inspect  -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $1"  # Get the IP address of a container.
alias dl="docker ps -l -q"  # Get the ID of the most recently created container.
 
## Container Management
alias dstop='docker stop $(docker ps -a -q)'    # Stop all containers.
alias drm='docker rm $(docker ps -a -q)'    # Remove all containers.
alias drmf='docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)'     # Stop and remove all containers.
 
## Image Management
alias dri='docker rmi $(docker images -q)'  # Remove all Docker images.
 
# Docker Volume management
alias dvls='docker volume ls'
alias dbclean='docker volume rm $(docker volume ls -q)'
 
## Docker log management 
alias dlogs="docker logs "
alias dlogsf="docker logs -f "
 
## Docker compose commands simplified 
dcup() {
 cd /root/myproject
 docker-compose up -d
 echo "After up:"; docker-compose ps
}
 
dcdown() {
    echo "Before down:";  cd /root/myproject; docker-compose ps
    docker-compose down
}
 
dcbuidl() {
 echo "Build with docker-compose"
 cd /root/myproject
 docker-compose up --build
}

   

How to know which docker volume connected to your container?


Let's create a container named as myweb with a docker volume attached as web-volume.
docker run -d --name myweb \
 --mount source=web-volume,destination=/usr/share/nginx/html nginx
Using inspect sub-command on the container --format string with JSON forms is going to return a Json block:
  docker container inspect \
  --format '{{json .HostConfig.Mounts}}' myweb |jq

# simplified alias with parameterized 
alias knv2c="docker container inspect --format '{{json .HostConfig.Mounts}}' $1"
kn2c myweb|jq

alias k2c="docker container inspect -f '{{json .HostConfig.Mounts }}'"
docker container inspect with -f option json format

Tuesday, October 2, 2018

Docker Container Concepts

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 container


docker 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 ls
 
Let's attach to the test container

  docker attach test
  
Type 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"  test 
Please 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

Categories

Kubernetes (25) Docker (20) git (16) Jenkins (12) AWS (7) Jenkins CI (5) Vagrant (5) K8s (4) VirtualBox (4) CentOS7 (3) docker registry (3) docker-ee (3) ucp (3) Jenkins Automation (2) Jenkins Master Slave (2) Jenkins Project (2) containers (2) create deployment (2) docker EE (2) docker container (2) docker private registry (2) dockers (2) dtr (2) kubeadm (2) kubectl (2) kubelet (2) openssl (2) Alert Manager CLI (1) AlertManager (1) Apache Maven (1) Best DevOps interview questions (1) CentOS (1) Container as a Service (1) DevOps Interview Questions (1) Docker 19 CE on Ubuntu 19.04 (1) Docker Tutorial (1) Docker UCP (1) Docker installation on Ubunutu (1) Docker interview questions (1) Docker on PowerShell (1) Docker on Windows (1) Docker version (1) Docker-ee installation on CentOS (1) DockerHub (1) Features of DTR (1) Fedora (1) Freestyle Project (1) Git Install on CentOS (1) Git Install on Oracle Linux (1) Git Install on RHEL (1) Git Source based installation (1) Git line ending setup (1) Git migration (1) Grafana on Windows (1) Install DTR (1) Install Docker on Windows Server (1) Install Maven on CentOS (1) Issues (1) Jenkins CI server on AWS instance (1) Jenkins First Job (1) Jenkins Installation on CentOS7 (1) Jenkins Master (1) Jenkins automatic build (1) Jenkins installation on Ubuntu 18.04 (1) Jenkins integration with GitHub server (1) Jenkins on AWS Ubuntu (1) Kubernetes Cluster provisioning (1) Kubernetes interview questions (1) Kuberntes Installation (1) Maven (1) Maven installation on Unix (1) Operations interview Questions (1) Oracle Linux (1) Personal access tokens on GitHub (1) Problem in Docker (1) Prometheus (1) Prometheus CLI (1) RHEL (1) SCM (1) SCM Poll (1) SRE interview questions (1) Troubleshooting (1) Uninstall Git (1) Uninstall Git on CentOS7 (1) Universal Control Plane (1) Vagrantfile (1) amtool (1) aws IAM Role (1) aws policy (1) caas (1) chef installation (1) create organization on UCP (1) create team on UCP (1) docker CE (1) docker UCP console (1) docker command line (1) docker commands (1) docker community edition (1) docker editions (1) docker enterprise edition (1) docker enterprise edition deep dive (1) docker for windows (1) docker hub (1) docker installation (1) docker node (1) docker releases (1) docker secure registry (1) docker service (1) docker swarm init (1) docker swarm join (1) docker trusted registry (1) elasticBeanStalk (1) global configurations (1) helm installation issue (1) mvn (1) namespaces (1) promtool (1) service creation (1) slack (1)