Docker Commands - ashwin-shetty/Documents-Wiki GitHub Wiki

Content

  1. Install Docker
  2. Basic Commands
    1. Info about Containers
    2. Run, Stop and Remove Container
    3. Get IP
    4. Container Size
    5. Reclaim Space
    6. Custom Table
  3. Push Image to Repo
  4. Spring Boot
  5. Docker Compose
  6. Delete Logs

Docker Installation

Note : Create Docker account in Docker hub to access public repository

1. Install Docker

sudo apt -get install docker-ce

2. Add user to group

sudo usermod -a -G $USER

3. Login with Docker Hub Credentials

docker login

4. Test Docker 'hello world'

docker run hello-world

Basic Commands

1. Info about Containers & Images

   #  Running docker image 
    docker ps
   # Saved images
    docker images

2. Run, Stop and remove Container

   # Run 
   ## 1.Single Container 
	docker run -it --rm tomcat:8.0
   ## 2. Single Container mapped to local computer port 
	docker run -it --rm -p 8888:8080 tomcat:8.0
   ## 3. Multiple Instance and mapped to different local Port
        docker run -it --rm -p 8888:8080 --name devTomcat tomcat:8.0
        docker run -it --rm -p 8889:8080 --name qaTomcat tomcat:8.0
   ###### Note : devTomcat and qaTomcat are container name and first instance is mapped 8888 |and second one to 8889 port 

   # Stop 
    docker stop dreamy_pike

   # Remove 
    docker rm dreamy_pike 
   // dreamy_pike is a container name and can be replaced with id

3. Get IP of the Container

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' tender_joliot
Note : tender_joliot is the Container Name, We can also use container id.

4. Check Container Size

docker system df
#Sample Result
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              84                  20                  12.2GB              7.159GB (58%)
Containers          33                  31                  22GB                0B (0%)
Local Volumes       5                   5                   4.701GB             0B (0%)
Build Cache         0                   0                   0B                  0B

5. Reclaim Space

docker system prune -a
docker image prune -a

6. Custom Table

docker ps --format "table {{.Names}}\t {{.Size}}\t {{.Status}}\t {{.Image}}\t"
#Sample Result
NAMES   SIZE                      STATUS              IMAGE
app1    96kB (virtual 439MB)      Up 4 hours          tomcat7
app2    207MB (virtual 955MB)     Up 5 hours          tomcat8
app3    10.6MB (virtual 126MB)    Up 2 days           nginx
app4    39.1kB (virtual 115MB)    Up 3 weeks          nginx

Push Image to Repo

   ## 1.Login to Docker Repo 
	docker login  mylocalrepo:8081
   ## 2. To Re Tag iamge  
	docker tag ashu/myapp:1.0.0  mylocalrepo:8081/ashu/myapp:1.0.0
   ## 3. Push image to private Repo
        docker push mylocalrepo:8081/ashu/myapp:1.0.0
   ## 4 Remove Tag from local Repo
        docker rmi mylocalrepo:8081/ashu/myapp:1.0.0

   // Here we copy the image from public repo and add to our private repo

Target and Published

target: the port inside the container
published: the publicly exposed port
protocol: the port protocol (tcp or udp)
mode: host for publishing a host port on each node, or ingress for a swarm mode port to be load balanced.

SpringBoot HelloWorld

1. Clone GIT SpringBoot Repository Project in Eclipse or IntelliJ IDE

 git clone https://github.com/spring-guides/gs-spring-boot-docker.git

2. Excecute Below Command

 mvn clean
 mvn install dockerfile:build
Note : Use Java 8 for compiling application, Java 10 throws error like "Caused by: java.lang.ClassNotFoundException: javax.activation.DataSource"

Docker Compose Remove unused Containers

 docker-compose rm -svf

Delete Logs

-- Look for Containers size 
du -sh /opt/docker/containers/* | sort -h

-- Delete Logs
cat /dev/null > /opt/docker/containers/78f3d/78f353-json.log