Docker – Command line cheat sheet

Early post Docker for beginners ,we talked about how to start with dockers

Here are few basic commands which we need on daily base work case with docker

Useful Docker Commands helpful for operation

1) To Build Docker image from Dockerfile

      # docker build -t thegeeklinux:1.0 .
-t for Image name and tag name:tag (If you will not give tag it will tag latest)

2) To list images :
      # docker images

3) To save docker image in tar file :
      # docker save -o /tmp/thegeeklinux.tar thegeeklinux:1.0

4) To delete docker image :
      # docker rmi -f thegeeklinux:1.0
-f forcefully

5) To load docker image from tarball :
      # docker load < /tmp/thegeeklinux.tar

6) To tag docker image :
      # docker tag thegeeklinux:1.0 localhost:5000/thegeeklinux:1.0

NOTE:localhost:5000 should be your private registry ip and port

7) To push image to private or public registry server
      # docker push localhost:5000/thegeeklinux:1.0

NOTE: replace localhost with your private registry hostname or ip.

8) To pull docker image for private or public registry

a) To pull image from private registry server
         # docker pull localhost:5000/thegeeklinux:1.0

NOTE :replace localhost with your private registry hostname or ip.

b) To pull image from public registry
         # docker pull ubuntu

Docker Container Operation

1) To RUN Docker container :
      # docker run -it thegeeklinux:1.0
-i interactive mode
-t attract terminal

      # docker run -d --name thegeeklinux -p 80:80 -h tgl -l "com.thegeeklinux=1.0" -e WORKDIR=/opt/thegeeklinux thegeeklinux:1.0

-d Detach it will run container in backgraund.
-e set environment for container
-h Hostname you can give hostname to container also
-l we can give label also to container for indentification.
-p we can open port to host from container
–name Name of the container

2) To Run command inside docker container
      # docker exec thegeeklinux ls

3) Login to container without ssh :
      # docker exec -it thegeeklinux bash

4) To check container list :
      # docker ps

5) To check container memory and cpu.
      # docker stats thegeeklinux

6) To copy contant from container to host machine or host machine to container
      # docker cp ./thegeeklinux.war tgl:/opt/tomcat/current/webapps/

7) To check container related logs :
      # docker logs -f thegeeklinux
-f It will follow log output

8) To commit container :
      # docker commit -p -a "" -m "commit message" tgl tgl:2.0
-a Author of the container
-m commit message
-p pause container during commit

9) To export container in compressed file
      # docker export -o /tmp/dockerexport.tar thegeeklinux

NOTE : export command will not export attached volume.

10) To get container information
      # docker inspect thegeeklinux

NOTE: it will give info in json format

To get ip addres of container
       # docker inspect -f {{.NetworkSettings.IPAddress}} thegeeklinux

To get mapped port to host :
       # docker inspect -f '{{(index (index .NetworkSettings.Ports "22/tcp") 0).HostPort}}' thegeeklinux

11) To Stop container:
      # docker stop thegeeklinux

12) To start container :
      # docker start thegeeklinux

13) To get container processes
      # docker top thegeeklinux

14) To rename the container :
      # docker rename thegeeklinux tgl

15) To restart container :
      # docker restart docker-scripts

16) To delete container :
      # docker rm -vf thegeeklinux
-v delete volume also
-f delete container forcely