Part 3 – Docker Images

Containers 
————-

# To show running containers:
docker ps
docker ps – a  ( list all On and of containers) 
docker container ls -l
docker container ls -aq
docker ps –format “table {{.Names}}\t{{.Status}}\t{{.Ports}}”

 

# To “start”  a  container:
docker start -a containerID
– a will show output

# To “stop” a running  container:
docker stop -a containerID
– a will show output

# Stop all containers 
alias dsa=”docker ps -q | awk ‘{print $1}’ | xargs -o docker stop”

# Renaming containers
sudo docker rename old_name new_name

# checking logs on a container
sudo docker logs registry

# To “kill” a  container: ( ie: destroy ) 
docker stop -a containerID
docker kill containerID
– a will show output

# To “remove” a  container: ( ie: remove container ) 
docker rm  containerID
docker container rm $(docker container ls -aq)

# To create a container in detached mode
docker container run -d –name ash <imagename>
# To create/start a container with an interactive shell:
docker run -dt –name ashwebserver <image-name> /bin/bash

# To create/start a container with an interactive shell for a onetime run use –rm flag
docker run –rm -dt –name checks <image-name> ping -c www.google.com

# To create container with a port in bridge network

docker run -dt –name ashwebserver -p 8080:80 <image-nam> sh
– d = stands for detached mode 

ie : communicate to the port 80 via port 8080 on the host 

 

# Create container with a random port in bridge network
-P will pick a random port from the host
docker run -dt –name ashwebserver -P <image-name> sh

#Create a container with control groups – memory , ram 
docker run -dti –name memorydemo -m 256m busybox sh

# Create a container with CPU constraints
docker container run -dt –name constraint01 –cpus=1.5 busybox sh
docker container run -dt –name constraint02 –cpuset-cpus=0,1 busybox sh

# setting reservations and limits 
docker container -dt –name container2 –memory-reservation 250m busybox sh
Hard limit
docker container -dt –name container2 -m 500m –memory-reservation 250m busybox sh

# To create a container with autorestart 
sudo docker run -dt –restart unless-stopped –name web0123 busybox sh

# To create a container in a particular network in bridge mode 
docker run –dt –name web01 –restart unless-stopped -p 8081:80 –network vlan23 nginx

# to create a container in a particular network in host mode ( there is no need for port in host)
docker run –dt –name web01 –restart unless-stopped –network host  nginx

# To create container with a persistent volumes ( Volume Mount – note there is no /myvolume – / refers to Unix dir)
docker run -dt –name ashwebserver -v <volname>:<mapto> <image-name> sh
docker run -dt –name ashwebserver -v myvolume:/etc <image-name> sh
or
docker run -dt –name webserver01 –mount type=volume,source=myvolume,target=/mypath nginx

# To create container with a persistent volumes ( Bind Mount aka – / )
docker run -dt –name webserver01 –mount type=bind,source=/root/index,target=/mypath nginx
or
docker run -dt –name ashwebserver -v /myvolume:/etc <image-name> sh
/basically determines if this is a volume or bind
docker container run -dt –name mynginx –mount type=bind,source=/root/index,target=/usr/share/nginx/html nginx

 

# To create container with a persistent volumes in readonly ( Bind Mount aka – / )
docker run -dt –name webserver01 –mount type=bind,source=/root/index,target=/mypath,readonly nginx

# To “shell” into a running container:
docker exec -ti <container-name> bash
– interactive , -t tty
docker exec -it test ps aux

# To install a package inside a container to make it executable
docker exec -it <containerid> bash
apt install net-tools
exit
docker exec -ti <containerid> netstat -tupln
note : – only the packages loaded in /bin is executed by default so if we wish to run something outside to querry something we need to install the package

# To inspect a running container or check the properties:
docker inspect <container-name> (or <container-id>) | grep -i IPaddress 

# To inspect an image 
docker image inspect <ImageName>

 

# To clone an image using docker commit:
# this is used to take a backup of an imaage in case we need to modify stuffs
docker commit <existing image> <newimagename>

# To get the process ID for a container:
docker inspect –format {{.State.Pid}} <container-name-or-id>

# To list (and pretty-print) the current mounted volumes for a container:
docker inspect –format='{{json .Volumes}}’ <container-id> | python -mjson.tool

# To copy files/folders between a container and your host:
docker cp foo.txt mycontainer:/foo.txt

# To push an image to dockerhub
we first need to tag it 
docker tag xwiki:latest ashintoms/myhomewiki:v1
docker tag xwiki:latest example.com/admin/ashintoms:v1
Ashintoms is username here
docker push ashintoms/myhomewiki

# Save docker image so it can be exported 
docker save imageiD > demo.tar

# Restoring a container
docker load < demo.tar

(Visited 45 times, 1 visits today)

By C A Thomas

Chinchu A. Thomas is an Infrastructure Analyst specializing in Microsoft Azure, the Microsoft 365 suite, AWS, and Windows infrastructure management products.

Leave a Reply