Skip to main content

Command Palette

Search for a command to run...

Docker

Updated
3 min read
Docker

Basic Commands

sudo docker images                                        # to know the docker image in current machine
sudo docker search httpd                                # to get the list of available docker image in docker hub
sudo docker pull Imagepath                                  # latest tag will be pulled/download fron docker hub
sudo docker push Imagepath:version                         # to upload the image to dockerhub under username/appname:version 
sudo  docker run -d -it --name Myhttp -p 81:80 httpd      # to run the images to get container
sudo docker build . -t myweb:v1                            # to build image using alias tags
sudo docker exec it ubuntu1 /bin/bash                    # to login container
sudo  docker ps -a                                       # to check the image status
sudo docker inspect Imagepath                             # to check the port used in image for port forwarding 
grep -A 10 Config                                        # to filter results
sudo docker history                                        # to get history of commands
sudo docker save image Id > snapshot.tar                # backup or snapshot 
scp snapshot.tar username@remotemachine:path            # to pull image from other server


sudo docker images
sudo docker stop container_names                        # to stop containers
sudo docker rm  container_name                             # to remove container  
sudo docker rmi    image_name                                # to remove images ( container need to remove before remove images)
sudo docker commit docker Image_id Custom_name:tag        # to convert the container to image
sudo docker login                                         # login dockerhub
sudo docker login sunil.aws.com                            # Custom repository login
Sudo docker info                                        # to get information about docker
sudo docker logs                                        # to get logs

Docker file Format (image file)

FROM ubuntu:latest 
LABEL                                #( TO NAME AND alisa the environment ) 
EXPOSE 80 443                        # (define container port listen)
copy index.html /var/www/html/       # To copy index file to publish

Note : Command ADD (copy and extract tar file from source to destination), COPY (copy tar to destination location)

RUN sudo apt-get update                                     # (RUN to run any command on top of image )
RUN sudo apt-get in-y apache2
RUN sudo apt-get update && sudo apt-get install -y apache2  # run in single layer will reduce size)
CMD apachectl -D FOREGROUND                                 # systemctl commad will not work here
CMD ping google.com
ENTRYPOINT ping google.com                                  # SAME HAS CMD but it cant be override during excution

VOLUME /PATH                                                 # MOUNT VOLUME
USER USERNAME                                                # SELECT USER (DEFAULT WILL USE ROOT ) 
WORKDIR    /app                                                # WORKING DIRECTORY 
ENV                                                         # ENVIRONMENT VARIABLE COMPUTER
ARG                                                            # ENVIRONMENT VARIABLE USER DURING BUILD USED NOT IN CONTAINER

Docker Orchestration - Docker Swarn

sudo docker install swarm master
sudo docker init--advertise-addr Masternode_IP
sudo docker node ls 

docker service                                                                 # Container called here has service
sudo docker service create --replicas 2 -p 80:8043 --name web1 imagename
sudo docker service ls                                                      # to list the service 
sudo docker service ps web1                                                  # to check the service running nodes
sudo docker service create --mode global -p 81:8044 --name testweb1 imagename  # to run the container with max replicas( max node available)
sudo docker service scale web1=5                                            # scale up to  5 replicate
sudo docker service scale web1=2                                              # scale down to  2 replicate
sudo dcker network  create -d overlay newoverlay

sudo docker service update --image httpd:latest web1
sudo docker service rollback web1
sudo docker node update --availablity drain nodename                         # maintenanace mode
sudo docker node update --availablity pause nodename                          # drs will not migrate container to this node
sudo docker node update --availablity active nodename 

Docker compose                                                              # to run multi container
sudo apt-cache search docker-compose                                          # to install  docker composr version
sudo docker-compose -f myapp.yaml config                                     # to check the configuartion issue
sudo docker-compose -f myapp.yaml up -d                                     # to run the compose
sudo docker-compose -f myapp.yaml images                                     # to check images related to compose file

More from this blog

S

Sunil Joe

10 posts

AWS Certified Security - Specialty, CISCO Certified Network Professional, VMware Administrator, Server Administrator, Nutanix administrator. Sharing here my technical knowledge.