Managing Docker Containers

Search for a command to run...

Thanks Rajiv
API (Application Programming Interface) security is critical for protecting sensitive data and maintaining the integrity of systems and applications. APIs are used to connect different systems and applications, and as a result, they can provide a gat...

Well after months of deep slumber, away from social media networks, I had to return to Pune and adapt to hybrid working. I was browsing through meetup.com and was intrigued by one event from AWS user group Pune. It is AWS Reinvent 2022 -Recap on 21st...

Episode 2

Simplify Access Control and Enhance Transparency Over Your ML Projects

Episode 1

sudo docker run -it ubuntu /bin/bash
The "docker run" command provides all launching capabilities for docker to create a container. We use docker run to create new containers.
-i: open STDIN from the container
-t: tells docker to assign a pseudo-tty to the container
-it : provides an interactive shell
ubuntu: ubuntu is an image and also called as "stock image" or "base image". This image will be downloaded from Docker hub when we run 'docker run' command first time
/bin/bash: 'shell program' that will be installed in the terminal
Let's believe that it's a separate machine by using the below commands
hostname
cat /etc/hosts
hostname -i
ps -ef
Setup SSH in the containers so that they can communicate with each other
Create two containers having IP addresses - 172.17.0.2, 172.17.0.3
Try to connect to 172.17.0.3 from 172.17.0.2 using the below command. You will get an error.
$ ssh demo@172.17.0.3 (you won't be able to connect by default)
Install ssh server
$ apt-get update
$ apt-get install openssh-server
Start the server
$ service ssh start (status/stop/restart)
Create a user and set up a password
$ useradd -m -d /home/demo -s /bin/bash demo
$ passwd demo
Connect to the container using ssh from 172.17.0.2 or any other machine.
$ ssh demo@172.17.0.3
Enable root user over ssh
Add the below line under "# Authentication:" in "/etc/ssh/sshd_config" PermitRootLogin yes
"exit" to stop the container
$docker start
$docker attach
$ docker container ls -a
$ docker ps -a
$ docker ps -a -n1
$ docker container ls
$ docker ps
$ docker container ls -f status=exited (Where Status can be exited/running)
The "docker container ls" command output shows:
- Image name from which container is created
- ID - the container can be identified using short UUID, longer UUID Or name.
- Status of the container (Up / Exited)
- Name of the container
$ docker container ls -l
$ docker run --name demo -it ubuntu /bin/bash
Note: Two containers can't have the same name.
$ docker rename container_name_1 container_name_2
$ docker rm ID/name
$ docker rm -f $(docker container ls -a -q)
$ docker rm -f $(docker ps -a -q)
$ docker rm -f $(docker container ls -q)
$ docker rm -f $(docker ps -q)
$ docker container ls -a -f status=exited
$ docker start container_name
$ docker attach container_name (OR)
$ docker attach b1b1c8dc1939
$ docker exec -it tomcat-server ps -ef
$ docker stop container_name(Gracefully stop the container)
$ docker kill container_name(Forcibly stop the container)
$ docker top container_name
$ docker ps -n4
$ docker run -it -d ubuntu /bin/bash
The 'docker inspect' command interrogates the container and returns complete information about it.
Ex: image name, IP, Memory details, hostname ..etc
Examples:
$ docker inspect container_name
$ docker inspect -f '{{.Config.Hostname}}' container_name
$ docker inspect -f '{{.NetworkSettings.Networks.bridge.IPAddress}}' container_name
Note: use --format (OR) -f
$ docker inspect --format "{{.Name}}" $(docker ps -a -q) | tr -d '/'
$ docker stats --no-stream container_name
$ docker stats --no-stream --all
$ docker stats --no-stream --format {{.MemUsage}} container_name
$ docker stats --no-stream --format {{.CPUPerc}} container_name
$ docker run -it --name container_name -m 1g ubuntu /bin/bash
$ docker run -it --name container_name -m 1024m ubuntu /bin/bash
$ docker update -m 2024m container_name
$ docker run -it --cpus="2" --name container_name ubuntu /bin/bash
$ docker update --cpus="2" conatiner_name