Apr 3, 2019

Docker for Beginner


Docker Vs Virtual Machine
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

Docker commands:
docker info   
docker --version
docker-compose —version
docker-machine —version

#Execute Docker image
docker run hello-world

#list of running running containers
docker ps

#list docker images
docker image ls 

#list Docker containers
docker container ls
docker container ls --all

docker images
docker ps -l


You create a Dockerfle
###################
From this Dockerfile, you will create/build an image
docker build  --tag=friendlyhello  .

When you run a docker image, it will create a container for you (when u kill the running image, the running container is gone)

#Mapping container port to host/local_machine port
docker run -p 4000:80 friendlyhello
#4000 is the host port (locally) - http://localhost:4000/   (thats why we use 4000 port locally)
#80 is the container port

docker run -d -p 4000:80 friendlyhello   #run as a background daemon
docker run -d -p 4000:80 prabhathkota/test-docker:tag1

docker container ls
docker container stop 1fa4ab2cf395

docker stats <container_id>
docker logs <container_id>
docker cp <container_id>:/path/to/useful/file /local-path

Get inside the container:
####################
docker exec -it <containerid> bash


Docker hub - is like GitHub repository (to share)
####################################
Share your image
docker login
docker tag friendlyhello prabhathkota/test-docker:tag1
docker image ls

Remove image
#############
docker rmi -f 8b810fbdcf2d   #(forcefully remove image)

Publish the image:
##############
docker push prabhathkota/test-docker:tag1

Run image from remote repository
##########################
docker run -p 4000:80 prabhathkota/test-docker:tag1

docker build -t friendlyhello .  # Create image using this directory's Dockerfile

Run local repository
################
docker run -p 4000:80 friendlyhello  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode

Docker commands:
################
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry