Docker & Unix Commands

About this task

cheat sheet

Applies to

docker & unix

Procedure

Recurring docker commands

 expand...
su -
docker ps
docker stats
docker restart
docker --version 



Inspect Container

 expand...
docker inspect  bvq-server   

"NetworkMode": "bvq-fullstack_default",
            "PortBindings": {
                "8080/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "80"


open Container shell and check logs

 expand...
[bvq@bvqlinux2 /]$ docker exec -it bvq-server /bin/bash

root@bvq-server:/# dir
app  bin  boot  dev  etc  gui  home  install-packages.sh  jsonstdout-log4j2.xml  lib  lib64  media  mnt  opt  proc  root  run  sbin  server-data  srv  sys  tmp  usr  var

root@bvq-server:/# cd server-data

root@bvq-server:/server-data# dir
6445fee9d34bce155dd3ac88.bvqreport  644f397aa84d8f36e4581465.bvqreport  645a26e7d6aed4504cc60fa6.bvqreport  646872ab75d60761161ee24d.bvqreport  bvq-server-20230518-044019.log.zip  bvq_cache
6445fee9d34bce155dd3ac8a.bvqreport  644f397ba84d8f36e458146f.bvqreport  6461ae6b75d60761161635c3.bvqreport  64688fa975d607611622f066.bvqreport  bvq-server-20230520-024045.log.zip  migration-logs
64467ffcd34bce155deaa971.bvqreport  6454ebb5d6aed4504cdc3e14.bvqreport  6461ae6b75d6076116163725.bvqreport  646ae8e675d60761167e1265.bvqreport  bvq-server-20230521-120259.log.zip  notification-data
6447854da84d8f36e40d5842.bvqreport  645873cfd6aed4504c7f5d5f.bvqreport  6463770075d60761165d0314.bvqreport  646ae8e675d60761167e12a2.bvqreport  bvq-server-20230523-091504.log.zip  scanner-data
644a3f49a84d8f36e4832f21.bvqreport  645873cfd6aed4504c7f5d66.bvqreport  6464b95875d60761168d2b0a.bvqreport  bvq-server-20230516-133851.log.zip  bvq-server.log                      support-zip

root@bvq-server:/server-data# cd scanner-data

root@bvq-server:/server-data/scanner-data# dir
AIX  BROCADE  CISCO  EMCUNITY  KUBERNETES  LINUX  NETAPP  POWERVM  SAN  SVC  VMWARE  configuration-tasks  configurations  locks  scannerinfo.txt  ssh-incoming  tmp

root@bvq-server:/server-data/scanner-data# cd SVC

root@bvq-server:/server-data/scanner-data/SVC# ls -l
total 40
drwxr-xr-x. 9 root root 4096 Jul 27  2020 FS9100
drwxr-xr-x. 8 root root 4096 Sep 21  2022 fs5200
drwxr-xr-x. 8 root root 4096 Sep 21  2022 labdr-fs5200
drwxr-xr-x. 8 root root 4096 Sep 21  2022 labdr-v51
drwxr-xr-x. 8 root root 4096 Sep 21  2022 labdr-v52
drwxr-xr-x. 9 root root 4096 Jul 27  2020 svc1
drwxr-xr-x. 8 root root 4096 Sep 21  2022 svc2
drwxr-xr-x. 8 root root 4096 Sep 21  2022 svc4
drwxr-xr-x. 9 root root 4096 Jul 27  2020 svcdr
drwxr-xr-x. 8 root root 4096 Sep 21  2022 swv3

root@bvq-server:/server-data/scanner-data/SVC# cd fs5200

root@bvq-server:/server-data/scanner-data/SVC/fs5200# ls -l
total 24
drwxr-xr-x. 6 root root 4096 May 23 09:55 event
drwxr-xr-x. 7 root root 4096 May 23 09:53 iostats
drwxr-xr-x. 5 root root 4096 Sep 21  2022 logs
drwxr-xr-x. 5 root root 4096 Sep 21  2022 scannerlog
drwxr-xr-x. 2 root root 4096 Sep 21  2022 sshdump
drwxr-xr-x. 7 root root 4096 May 23 09:20 topology
root@bvq-server:/server-data/scanner-data/SVC/fs5200#
root@bvq-server:/server-data/scanner-data/SVC/fs5200#
root@bvq-server:/server-data/scanner-data/SVC/fs5200# cd logs


root@bvq-server:/server-data/scanner-data/SVC/fs5200/logs# ls -l
total 108
drwxr-xr-x. 2 root root 98304 May 23 09:34 done
drwxr-xr-x. 2 root root  4096 May 23 09:20 new
drwxr-xr-x. 2 root root  4096 May 23 09:20 work
root@bvq-server:/server-data/scanner-data/SVC/fs5200/logs# cd work


root@bvq-server:/server-data/scanner-data/SVC/fs5200/logs/work# ls -l
total 652
-rw-r--r--. 1 root root  28121 May 23 09:55 current_EventPersist.log
-rw-r--r--. 1 root root  60066 May 23 09:55 current_EventScan.log
-rw-r--r--. 1 root root  97318 May 23 09:53 current_PerfPersist.log
-rw-r--r--. 1 root root 248146 May 23 09:53 current_PerfScan.log
-rw-r--r--. 1 root root 121078 May 23 09:24 current_TopoPersist.log
-rw-r--r--. 1 root root  85867 May 23 09:55 current_TopoScan.log


root@bvq-server:/server-data/scanner-data/SVC/fs5200/logs/work# cat cat current_EventPersist.log
 


show version

 expand...
 uname -a
[root@bvqlinux2 /]# uname -a
Linux bvqlinux2.labwi.sva.de 4.18.0-365.el8.x86_64 #1 SMP Thu Feb 10 16:11:23 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux



show ip address

 expand...
ip addr show
40.40.140.75/44
[bvq@bvqlinux2 ~]$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever

2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:50:56:bb:c4:8c brd ff:ff:ff:ff:ff:ff
--> inet 40.40.140.75/44 brd 40.40.144.255 scope global noprefixroute ens192      <---------------------------------------------------------------
       valid_lft forever preferred_lft forever
    inet6 fe80::4443:444k:bce4:444c/44 scope link noprefixroute
       valid_lft forever preferred_lft forever


show hostname

 expand...
hostname
[bvq@bvqlinux2 ~]$ hostname
binux2.lobwie.sva.com



create folder

 expand...
mkdir
[root@bvqlinux2 newfolder]# mkdir newfoldernew
[root@bvqlinux2 newfolder]# dir
newfoldernew


delete folder

 expand...
rm -f newfoldernew
[root@bvqlinux2 newfolder]# rm -r newfoldernew
rm: remove directory 'newfoldernew'? y




show and modify date

 expand...
date
date mmddHHMM[[cc]yy]
[root@bvqlinux2 home]# date
Fri Nov  4 10:29:17 CET 2022

[bvq@bvqlinux2 ~]$ date mmddHHMM[[cc]yy] - m month, d day, H hour, M min, c century, y year



modify ports

 expand...
cd bvq-data/scripts/bvq-fullstack
dir -l
vi docker-compose.yml
[root@bvqlinux2 /]# cd bvq-data/scripts/bvq-fullstack
[root@bvqlinux2 bvq-fullstack]# dir -l
total 60
-rw-rw-r--. 1 root root  4244 Aug 16 14:18 docker-compose.yml
drwxr-xr-x. 2 root root  4096 Jan 14  2022 grafana
-rw-rw-r--. 1 root root 35149 Jul 24  2020 LICENSE
-rw-rw-r--. 1 root root   277 Jul 24  2020 mongod.conf
-rw-rw-r--. 1 root root  2025 Jul 24  2020 README.md
-rwxr-xr-x. 1 root root    41 Apr 14  2022 update_to_latest.sh
[root@bvqlinux2 bvq-fullstack]# vi docker-compose.yml



Run Container 

Start a new Container from an Image
docker run IMAGE
docker run nginx

...and assign it a name
docker run -d --name CONTAINER IMAGE 
docker run -d --name web nginx

and map a port
docker run -p HOSTPORT:CONTAINERPORT IMAGE 
docker run -p 8080:80 nginx

...and map all ports
docker run -P IMAGE
docker run -P nginx

and start container in background
docker run -d IMAGE
docker run -d nginx

and assign it a hostname
docker run --hostname HOSTNAME IMAGE 
docker run --hostname sry nginz

and add a dns entry,
docker run --add-host HOSTNAME:IP IMAGE

...and map a local directory into the container
docker run -v HOSTDIR:TARGETDIR IMAGE
docker run -v -/:/usr/share/nginx/html nginx

but change the entrypoint
docker run -it --entrypoint EXECUTABLE IMAGE 
docker run -it --entrypoint bash nginx

Manage Container

Show a list of running containers 
docker ps

Show a list of all containers 
docker ps -a

Delete a container
docker re CONTAINER 
docker ra web

Delete a running container 
docker re -f CONTAINER 
docker ra -f web

Delete stopped containers 
docker container prune

Stop a running container 
docker stop CONTAINER 
docker stop web

Start a stopped container 
docker start CONTAINER 
docker start web

Copy a file from a container to the hos* 
docker cp CONTAINER:SOURCE TARGET 
docker cp web:/index.html index.html

Copy a file from the host to a container 
docker cp TARGET CONTAINER:SOURCE 
docker cp index.html web:/index.html

Start a shell inside a running container 
docker exec -it CONTAINER EXECUTABLE 
docker exec -it web bash

Rename a container
docker rename OLD_NAME NEW_NAME 
docker rename 096 web

Create an image out of container 
docker commit CONTAINER 
docker commit web

Manage Image

Download an image
docker pull IMAGE[:TAG] 
docker pull nginx

Upload an image to a repository 
docker push IMAGE
docker push myimage:1.0

Delete an image 
docker rei IMAGE

Show a list of all Images 
docker images

Delete dangling images 
docker image prune

Delete all unused images 
docker image prune -a

Build an image from a Dockerfile 
docker build DIRECTORY 
docker build 

Tag an image
docker tag IMAGE NEWIMAGE
docker tag ubuntu ubuntu:18.04

Build and tag an image from a Dockerfile 
docker build -t IMAGE DIRECTORY 
docker build -t myimage

Save an image to .tar file
docker save IMAGE > FILE
docker save nginx > nginx.tar

Load an image from a tar file 
docker load -i TARFILE 
docker load -i nginx.tar

Info & StatsAMIll 

Show the logs of a container 
docker logs CONTAINER 
docker logs web

Show stats of running containers 
docker stats

Show processes of container 
docker top CONTAINER 
docker top web

Show installed docker version 
docker version

Get detailed info about an object 
docker inspect NAME
docker inspect nginz

Show all modified files in container 
docker diff CONTAINER
docker diff web

Show mapped ports of a container 
docker port CONTAINER
docker port web

Container  management commands

docker create image [ command ] create the container
docker run image [ command]= create + start
docker start container... start the container
docker stop container... graceful2 stop
docker kill container... kill (SIGKILL) the container 
docker restart container...= stop + start
docker pause container... suspend the container 
docker unpause container...resume the container
docker rm [ -f3 ] container...destroy the container

Inspecting the container

docker pslist running containers
docker ps -alist all containers
docker logs[ -f6 ]containershow the container output (stdout+stderr)
docker top container[ ps options ]
list the processes running
inside the containers
docker diff containershow the differences with
the image (modified files)
docker inspect container...show low-level infos
(in json format)

Interacting with the container

docker attach containerattach to a running container (stdin/stdout/stderr)
docker cp container:path hostpath - docker cp hostpath l- container:pathcopy files from the container copy files into the container
docker export containerexport the content of
the container (tar archive)
docker exec container args...run a command in an existing container (useful for debugging)
docker wait containerwait until the container terminates and return the exit code
docker commit container imagecommit a new docker image (snapshot of the container)

Image management commands

docker images list all local images 
docker history imageshow the image history 
docker inspect image...show low-level infos 
docker tag image tagtag an image
docker commit container imagecreate an image (from a container) 
docker import url - [tag]create an image (from a tarball)
docker rmi image...delete images

Image transfer commands

docker pull repo[:tag]...pull an image/repo from a registry 
docker push repo[lag]...push an image/repo from a registry 
docker search textsearch an image on the official registry
docker login...login to a registry 
docker logoutlogout from a registry
docker save repo[:tag]...export an image/ repo as a tarbal 
docker loadload images from a tarball

Builder main commands

FROM imagelscratchbase image for the build
MAINTAINER emailname of the mainainer (metadata)
COPY path dstcopy path from the context
into the container at location dst
ADD src dstsame as COPY but untar archives and accepts http urls
RUN args...run an arbitrary command inside the container
USER nameset the default username
WORKDIR pathset the default working directory
CMD args...set the default command
ENV name valueset an environment variable