Docker basic Tasks: Step by Step Procedure

Task 1: Search & Pull images from Dockerhub

  • docker can search and pull images from dockerhub
    • docker search hello-world # search for hello-world image
    • docker search jenkins # search for jenkins image in dockerhub
    • docker search httpd # search for apache httpd image
    • docker search tomcat # search for tomcat image
    • docker pull jenkins/jenkins # pull jenkins:lts image from dockerhub
    • docker pull httpd # pull httpd image
    • docker pull tomcat # pull tomcat image
  • by default docker pulls the image with :latest tag. This is not a good practice. Instead pull a specific version of docker image
  • go to https://hub.docker.com/r/jenkins/jenkins/tags?page=1&ordering=last_updated and pull the image with tag 2.263.3-lts-slim docker pull jenkins/jenkins:2.263.3-lts-slim

Tsk 2: list all images available on the local server

  • docker image ls -a # list all the images on local server

Task 3: delete images

  • docker image ls -a # to list images
  • docker rmi httpd # remove httpd image from local server

    Task 4: launch containers with port and volumes

    • containers can be launched with available images or images that are not currently available on local system. Incase , the image is not available locally docker will try to pull it from repository
    • launch apache httpd container with 80 port from container mapped to 8888 port on host
    • also , the htdocs directory where deployment files should be copied is mapped to /root/my-httpd-files directory on host.
      • docker run -d -p 8888:80 -v /root/my-httpd-files:/usr/local/apache2/htdocs –name ncd-apache1 httpd
      • netstat -nap|grep 8888 # check for port 8888 on host. Remember https is running on 80 inside container
    • launch tomcat container with 8080 port from container mapped to 7777 port onn host
    • webapps directory of tomcat is mapped to /root/my-tomcat-webapps on host
      • docker run -d -p 7777:8080 -v /root/my-tomcat-webapps:/usr/local/tomcat/webapps –name ncd-tomcat1 tomcat
      • netstat -nap|grep 7777 # check for port 7777 on host. Remember its running on 8080 inside container
      • docker logs ncd-tomcat1 –follow # check the logs of container ncd-tomcat1 . Follow the logs. Press ctrl+c to exit
      • docker inspect ncd-tomcat1 # inspect the meta data of containers. eg: ports , volumes etc
      • deploy a sample application to both apps by copying the file to host mapped directory
      • cp /tmp/apache.html /root/my-httpd-files # copy apache.html for apache server
      • cp /tmp/ncodeit.war /root/my-tomcat-webapps # copy ncodeit.war file for tomcat server
      • docker logs ncd-tomcat1 –follow # check if ncodeit.war file is deployed
      • once launched , apache and tomcat servers can be accessed on host’s exposed ports
      • curl -I :8888/apache.html # httpd server is accessible on 8888 port of host server
      • curl -I :7777/ncodeit/ncodeit.html # tomcat server is accessible on 7777 port of host server
      • you can use any port available on host to map to any port inside the container

    Task 5: login to containers & execute commands inside containers

    • docker exec -it ncd-tomcat1 /bin/bash # start a bash shell inside a container
    • docker exec -it ncd-tomcat1 hostname # run hostname command inside a container
    • docker exec -it ncd-tomcat1 top # run top command inside a contaienr. press q to exit

    Task 6: stop/start/restart/kill containers

    • docker stop ncd-tomcat1 # stop a running container
    • docker ps -a # check status of all containers
    • docker start ncd-tomcat1 # start a container
    • docker ps -a # check status of all containers
    • docker restart ncd-tomcat1 # restart a container
    • docker ps -a # check status of all containers
    • docker kill ncd-tomcat1 # kill the container if its not responding to stop command
    • docker ps -a # check status of all containers

    Task 7: commit & save existing containers

    • sometimes we want to save an existing container as image. To take the image to another system
    • _this is not recommended practice. images should not be created from containers. Instead use Dockerfile to create images
      • docker pull ubuntu # pull ubuntu image. Yes, ubuntu also can be run inside a container
      • docker run -it ubuntu /bin/bash # login to ubuntu image
      • apt-get update -y && apt-get install htop -y # inside the container. install htop
      • htop # run htop command inside the container . press q to exit out of htop
      • exit
      • docker ps -a # check all the containers on the host
      • docker commit ncd-ubuntu-image #this container will be in Exited state
      • docker image ls -a # check the images. newly created image should be available now
      • docker run -it ncd-ubuntu-image /bin/bash # launch a new container from image
      • htop # this command is availble without any installation
      • exit
    • same thing can be done for any container

    Task 8: delete containers

    • remove all containers that have “exited” or “created” –
    • “created” is a state of the container when a wrong command is executed inside
      • docker ps -a # check status of all containers
      • docker rm
      • docker rm $(docker ps -a -f status=exited -f status=created -q) # remove all containers in exited or created state

    Task 9: create dockerhub account

    • go to www.docker.com and create a free account for yourself
    • login to the account -> click “Create Repository” -> create a new repository
    • you will push some images into this repository of your account in the next steps

    Task 10: tag images & push images to dockerhub

    • tagging in simple words is to give a version to an existing image
    • syntax docker tag <existing-image-with-tag> <dockerhub-user>/<remote-image-name-with-tag>
    • repository name on dockerhub.com should be same as the image name thats being tagged
    • once the tagging is done, login to docker registry with docker login
    • then push the image to docker registry using docker push
    • lets tag our image and push to the newly created dockerhub account
      • docker pull tomcat:8.5 # pull tomcat 8.5 image from dockerhub official apache repo
      • docker image list -a # list all the images available locally
      • docker tag tomcat:8.5 /tomcat:8.5.ncd # tag can be anything. ncodeitdocker is the username on dockerhub.com
      • docker image ls -a # you will see the newly created image also
    • now login to dockerhub.com and create a repository with name “tomcat” . Other repo names will not work
    • name of the image and name of repo should be same
      • docker login # provide your dockerhub username/password
      • docker push /tomcat:8.5.ncd # pushes image to repo in your account
    • login to dockerhub and make sure the new image is avaialble there
      • docker logout #logout of dockerhub account
      • rm /root/.docker/config.json # remove the stored credentials

    Leave a Comment

    MFH IT Solutions (Regd No -LIN : AP-03-46-003-03147775)

    Consultation & project support organization.

    Contact

    MFH IT Solutions (Regd)
    NAD Kotha Road, Opp Bashyam School, Butchurajupalem, Jaya Prakash Nagar Visakhapatnam, Andhra Pradesh – 530027