LESSION 02 : DOCKER BASICS - WHERE AM I START LEARN DOCKER ?

In this tutorial I’m going to teach you the basics you should have known before dive into depth.
Now, I’m going to cover up fundamentals you must know.

UNDERSTAND DOCKER IMAGES:

A. List Docker Images
$docker images
Repository - Image where it came from TAG - Version Number Image ID - Internal representation of Docker Image
B. Running an Image
$docker run -ti ubuntu:latest bash
Docker image tuns into a running container. Docker run takes images into container.
-ti Terminal Interactive - stands for interactive shell (Which help for TAB Completion & work correctly)
bash - Run command on a bash shell
Now I’m in a Ubuntu environment
Now check
#cat /etc/lsb-release
To exit
#exit
D. List the Running Images
$docker ps
ID : Running Container’s ID IMAGE : Image which made from COMMAND : Command it running on STATUS
PORT
E. List the Recently Stopped Last Containers
$docker ps -l
F. List All Stopped Containers
$docker ps -al
G. Docker Commit Docker Commit takes containers back to new images.
$docker run -ti ubuntu:latest bash
#touch test.file
#exit
$docker ps -l
ID : XXXXXXXX IMAGE : ubuntu COMMAND : bash CREATED : STATUS : PORTS : NAMES : ubuntu_happy
$docker commit
OR
$docker commit
Which returns Image ID sha256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Now, I have a newly created Docker Images.
H. Give Images a Name Using TAG
$docker tag  my-new-image
Now List Docker Images
$docker images
I. Now, Run Docker Image using The TAG
$docker run -ti my-new-image bash
#ls -la /test.file
Now, It has my test.file in it.
###################################
How To Run Processes In Docker Containers
$docker run –rm -ti ubuntu sleep 5
$docker run -ti ubuntu bash -c “sleep 3; echo all done”
–rm Delete this container when it exits
-ti Interactive Terminal
Keep Things Running on Container
$docker run -d -ti ubuntu
-d detach things running and leaving running in the background
Now, Jump into the task again that I had started over there.
$docker attach
By Pressing Ctrl+P, Ctrl+Q these key combinations you can exit from terminal by detaching from it. But, leaves it running. So you can attach back again.
again you can find running docker container using “docker ps” and you can run it again using “docker attach "
Docker Logs: By looking at the container output. Even though, you start the container, but it didn’t work. Now “docker logs” command come to an action.
$docker run –name example -d ubuntu bash -c “lose /etc/passwd” docker logs use look at what the output was.
docker logs
$docker logs exmaple
Stopping & Removing Containers:
docker kill Its stop the container.
docker rm Its remove the container.
$docker kill docker rm $docker ps -l
Docker Resource Limit Enforce Management: Docker has a feature to limit & enforce limit the resources. You can limit it to a fixed amount of memory as you need.

DOCKER COMMANDS

DOCKER MANAGEMENT COMMANDS
  1. Docker “help” Command:
Using “help” command you can see what are the other available commands.
docker -h | more
  1. To all the management command which associated with “docker image” command.
[root@svr1 ~]# docker image --help

Usage: docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.
  1. List Docker Images:
docker image ls
<2.png>
  1. Pull Docker Image:
Pull an image or a repository from a registry (Docker Hub).
docker image pull nginx
<3.png>
  1. View Image Information:
docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f949e7d76d63        5 days ago          126MB
httpd               latest              19459a872194        2 weeks ago         154MB
hello-world         latest              fce289e99eb9        9 months ago        1.84kB

docker image inspect fce289e99eb9
<4.png>
  1. To list the all all command related to “docker container” command.
[root@svr1 ~]# docker container --help

Usage: docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.
  1. List Running Containers:
docker container ls
<5.png>
  1. Run Command in a new container:
docker container  run nginx:latest
  1. List All Containers:
docker container ls -a
<6.png>
  1. Run Docker Container:
docker container run  -P -d nginx
-P Port
-d Run program in the background
<7.png>
[admin@svr1 ~]$ curl http://172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  1. List all the running Containers
docker container ps
<8.png>
  1. Display detailed information on the specific container:
docker container inspect 4d625f4a7921
docker container inspect <CONTAINER_ID>
<9.png>
  1. Display the running process of specific container:
docker container top 4d625f4a7921
docker container top <CONTAINER_ID>
<10.png >
  1. Attach Docker Containers:
docker container run -P -d nginx:latest 

docker container ls -a
<12.png>
<11.png>
[root@svr1 ~]# docker container attach 7e9428fa3cbc
172.17.0.1 - - [01/Oct/2019:09:07:57 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
  1. View Stats for Specific Docker Container
docker container stats 7e9428fa3cbc
  1. Run Command in a Specific Container: Which can use for execute commands on the container.
[root@svr1 ~]# docker container exec -it 7e9428fa3cbc /bin/bash
root@7e9428fa3cbc:/# ls
bin  boot  dev etc  home  lib lib64  media  mnt  opt proc  root  run  sbin  srv  sys  tmp  usr  var
  1. Remove Docker Container:
docker container rm -f 7e9428fa3cbc

docker container rm -f <CONTAINER_ID>
  1. Remove all the Stopped Containers:
docker container prune
###############################################################################
CREATING DOCKER CONTAINERS
-d Detach from the container run the container in the background.
[root@svr1 ~]docker container run -d nginx
8317663311f0d2d987ccae41d8c85802ef5ed93512f5f79479901ed27857bad9
[root@svr1 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8317663311f0        nginx               "nginx -g 'daemon of…"   8 seconds ago       Up 7 seconds        80/tcp              hardcore_leakey
Here it can see running in the background.
-i Keep “STDIN” open
[root@svr1 ~]# docker container run -it ubuntu:latest 
root@fdc372caedf7:/# ls 
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@fdc372caedf7:/# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.3 LTS"
-rm Automatically remove the container when its program exits.
docker container run --rm ubuntu:latest
–name string - Assign a name to a docker container
docker container run --name my_webserver nginx
EXPOSING & PUBLISHING CONTAINER PORTS
-d run as a background process
[root@svr1 ~]# docker container run -d nginx
aac2d187d875c571ab7461614cad5b64a6453c6083fd4761c3e9e564e5517fc6
[root@svr1 ~]# curl http://172.25.10.50
curl: (7) Failed connect to 172.25.10.50:80; Connection refused
In here you can see, we can’t access nginx welcome page even though nginx running. The reason is, we didn’t setup port mapping for nginx container. just only we did is expose port 3000.
–expose open up port 3000
[root@svr1 ~]# docker container run -d --expose 3000 nginx
1999e3a8a32c3f73e0a03663372141093ca096c712d1de76da609702474a242d
[root@svr1 ~]# docker container ls 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1999e3a8a32c        nginx               "nginx -g 'daemon of…"   10 seconds ago      Up 9 seconds        80/tcp, 3000/tcp    nice_lewin
8317663311f0        nginx               "nginx -g 'daemon of…"   46 minutes ago      Up 46 minutes       80/tcp              hardcore_leakey
Now, I’m going to expose & map the port same time.
[root@svr1 ~]# docker container run -d --expose 3000 -p 8080:80 nginx
14a093de7fe4474bad8bdbf9a6480c4932b1e10aa8cded51528ee4d0a191e5c4
[root@svr1 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
14a093de7fe4        nginx               "nginx -g 'daemon of…"   5 seconds ago       Up 4 seconds        3000/tcp, 0.0.0.0:8080->80/tcp   infallible_ellis
[root@svr1 ~]# curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
As same as above, Now I’ forwarding nginx container default port 80 into host port 8018.
docker container run -d -p <HOST_PORT>:<CONTAINER_PORT> <IMAGE_ID>
root@svr1 ~]# docker container run -d -p 8081:80 nginx:latest 
df99bbd4a46acb5acb08a70f1102296c836f0db28c745f6ba23bc0b4b5a43d56
[root@svr1 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
df99bbd4a46a        nginx:latest        "nginx -g 'daemon of…"   6 seconds ago       Up 5 seconds        0.0.0.0:8081->80/tcp   goofy_swanson
[root@svr1 ~]# curl localhost:8081
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
List All Port Mappings:
To see all the port mapping for a specific docker container.
[root@svr1 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
df99bbd4a46a        nginx:latest        "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes        0.0.0.0:8081->80/tcp   goofy_swanson
[root@svr1 ~]# docker port df99bbd4a46a
80/tcp -> 0.0.0.0:8081
############# EXECUTE COMMANDS ON A CONTAINER ##################
There Three Ways…
  1. Dockerfile
At the end of the Dockerfile for the nginx has mentioned “CMD”
  1. During a Docker run
[root@svr1 ~]# docker container run -it nginx /bin/bash
root@509e7ea33288:/# pwd
/
root@509e7ea33288:/# ls /etc/nginx/conf.d/
default.conf
root@509e7ea33288:/# ls /etc/nginx/                    
conf.d/         koi-utf         mime.types      nginx.conf      uwsgi_params    
fastcgi_params  koi-win         modules/        scgi_params     win-utf         
root@509e7ea33288:/# ls /etc/nginx/
conf.d fastcgi_params koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params uwsgi_params  win-utf
  1. Using the “exec” command

Comments