In this docker tutorial, we’ll explain Docker commands tutorial for Beginners with example and also know how Container works.
Docker Commands Tutorial for Beginners with Example
Docker Base Image
If we don’t have any images, which means we can’t run any containers
[root@Docker-sysadmin ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest bb776ce48575 4 days ago 109MB tomcat latest f1332ae3f570 2 weeks ago 463MB amazonlinux latest 01da4f8f9748 6 weeks ago 162MB [root@Docker-sysadmin ec2-user]#
How to search any images on Docker hub using CLI
$ docker search centos
[root@Docker-sysadmin ec2-user]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 5299 [OK] ansible/centos7-ansible Ansible on Centos7 121 [OK] jdeathe/centos-ssh CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86… 107 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC session… 85 [OK] imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 53 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 51 tutum/centos Simple CentOS docker image with SSH access 44 openshift/base-centos7 A Centos7 derived base image for Source-To-I… 41 gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glust… 40 [OK] centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 37 centos/python-35-centos7 Platform for building and running Python 3.5… 34 kinogmt/centos-ssh CentOS with SSH 26 [OK] centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 22 centos/php-56-centos7 Platform for building and running PHP 5.6 ap… 20 openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use w… 20 pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 10 openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for use … 6 openshift/jenkins-1-centos7 DEPRECATED: A Centos7 based Jenkins v1.x ima… 4 darksheer/centos Base Centos Image -- Updated hourly 3 [OK] pivotaldata/centos Base centos, freshened up a little with a Do… 3 pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 2 pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 1 blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] openshift/wildfly-81-centos7 A Centos7 based WildFly v8.1 image for use w… 1 smartentry/centos centos with smartentry 0 [OK] [root@Docker-sysadmin ec2-user]#
You can search images in the docker hub as per your requirement.
Eg: $ docker search apache
How to download or pull docker images locally
[root@Docker-sysadmin ec2-user]# docker pull centos latest: Pulling from library/centos 8ba884070f61: Pull complete Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861 Status: Downloaded newer image for centos:latest [root@Docker-sysadmin ec2-user]#
You will get default latest version of centos or you can pull specific docker images as mention.
Example:
# docker pull centos:latest
If you want centos with an older version, then you can use the tag.
Example:
# docker pull centos:centos6
How Container works
Whenever you run docker pull command followed by the name of the image as mention below:
Eg: docker pull nginx
- It will pull all of the base image filesystem layers that are represented how that particular image build.
- You can run docker images directly (remotely) without downloading (pull) to your base machine in one single command
- If you directly run docker run command it will download the images and then it will run the image in docker.
How to Check Docker Images
[root@Docker-sysadmin ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest bb776ce48575 4 days ago 109MB tomcat latest f1332ae3f570 2 weeks ago 463MB centos latest 9f38484d220f 4 weeks ago 202MB amazonlinux latest 01da4f8f9748 6 weeks ago 162MB [root@Docker-sysadmin ec2-user]#
You can run docker images with Image Name or Image ID.
How to get information about Docker Object
[root@Docker-sysadmin ec2-user]# docker inspect nginx
It will give output in JSON format which tells what goes into nginx Image and also detail idea about how nginx images are created.
Command to run the container in docker-engine
[root@Docker-sysadmin ec2-user]# docker run centos
Command to know which all containers are currently running
[root@Docker-sysadmin ec2-user]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8f6da1019e14 nginx "nginx -g 'daemon of…" 10 seconds ago Up 9 seconds 80/tcp brave_goldstine [root@Docker-sysadmin ec2-user]#
How to check container history which has run in the past
[root@Docker-sysadmin ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8f6da1019e14 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 80/tcp brave_goldstine 9d68fd1cde24 nginx "-d" 2 minutes ago Created 80/tcp nervous_ramanujan c7d4e2878ac1 centos "/bin/bash" 9 minutes ago Exited (0) 9 minutes ago condescending_bartik 3567a989e75a amazonlinux "./bin/sh" 2 days ago Exited (127) 2 days ago hopeful_brattain 40061df52bb2 nginx "nginx -g 'daemon of…" 3 days ago Exited (0) 3 days ago youthful_mahavira ba1a3200f2de tomcat "catalina.sh run" 3 days ago Exited (143) 3 days ago vigorous_tesla [root@Docker-sysadmin ec2-user]#
This list all the containers which have run in the past/history of run container’s
How to check IP address or other details of containers which are running
[root@Docker-sysadmin ec2-user]# docker inspect 4abb5bb3abfa | grep -i ipaddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.3", "IPAddress": "172.17.0.3", [root@Docker-sysadmin ec2-user]#
You can grep and check specific context as mention in the above command.
- IP Address is not shown when the container is not running
- IP Address is based upon the automatically installed “docker0” (zero) interface.
- Eg: $ ifconfig | grep docker
Run Docker in interactive mode
[root@Docker-sysadmin ec2-user]# docker run -i -t amazonlinux ./bin/bash bash-4.2# bash-4.2# See you got the terminal TTY of container amazonlinux
- -i = interactive
- -t = terminal
- -d = detach
You want to install some of the packages or install any application in a container by using the above command take access and run the installation command.
Run container in detached or disconnected mode:
[root@Docker-sysadmin ec2-user]# docker run -d nginx 8247428b3feee4fb6c3f4e1c49fffe81f2384d097c50e76499c2568f1ca157b8 [root@Docker-sysadmin ec2-user]# [root@Docker-sysadmin ec2-user]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8247428b3fee nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 80/tcp mystifying_darwin [root@Docker-sysadmin ec2-user]# [root@Docker-sysadmin ec2-user]#
Check your application which is running in the container using Elinks or Curl command
Use Elinks or curl command for checking/verify your application status, there are many alternatives from which you can check your application is working properly or not.
Elinks is a text-based web browser
How to install Elinks command in amazonlinux or any Linux variant OS
# yum install elinks
This will install elinks command with dependency.
- nss_compat_ossl-0.9.6-8.6.amzn1.x86_64.rpm
- elinks-0.12-0.36.pre6.14.amzn1.x86_64.rpm
[root@Docker-sysadmin ec2-user]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8247428b3fee nginx "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 80/tcp mystifying_darwin [root@Docker-sysadmin ec2-user]# [root@Docker-sysadmin ec2-user]# docker inspect 8247428b3fee | grep -i "ipaddress" "SecondaryIPAddresses": null, "IPAddress": "172.17.0.3", "IPAddress": "172.17.0.3", [root@Docker-sysadmin ec2-user]# elinks http://172.17.0.3
After entering elink command you will get text web browser as shown below:
Nginx is working on a container, not on localhost
- Check out below output I am trying to telnet to nginx 80 port on localhost it won’t work and similar I am doing telnet to container IP address and it works
- Telnet will not work on localhost with 80 port because we are not running any application on localhost port 80.
[root@Docker-sysadmin ec2-user]# telnet localhost 80 Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused [root@Docker-sysadmin ec2-user]# [root@Docker-sysadmin ec2-user]# [root@Docker-sysadmin ec2-user]# telnet 172.17.0.3 80 #(this is container IP) Trying 172.17.0.3... Connected to 172.17.0.3. Escape character is '^]'.
Command to start and stop the container
Stop the container
$ Docker stop <container-name/id>
[root@Docker-sysadmin ec2-user]# [root@Docker-sysadmin ec2-user]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8247428b3fee nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 80/tcp mystifying_darwin [root@Docker-sysadmin ec2-user]# docker stop 8247428b3fee 8247428b3fee [root@Docker-sysadmin ec2-user]# docker ps [root@Docker-sysadmin ec2-user]#
Start the container:
$ Docker start <container-name/id>
[root@Docker-sysadmin ec2-user]# docker start 8247428b3fee 8247428b3fee [root@Docker-sysadmin ec2-user]# [root@Docker-sysadmin ec2-user]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8247428b3fee nginx "nginx -g 'daemon of…" About an hour ago Up 7 seconds 80/tcp mystifying_darwin
This is the end of Docker Commands Tutorial for Beginners with example. and also you can check Docker doc’s