Dockers and Containers

Docker is a containerization platform,
Containers are just a processes, limited to what resources they can access, exit when process stops.

Image vs Container:
Image is the binaries, libraries and source code that all make up the application.
Container is the running instance of that image.

Note: We can run many containers with the same image.

In this example we are using open source webserver "nginx".

From where we will get this "nginx" image?
If it is not available locally then it will approach Docekr's default image "registry" is called "Docker Hub" (hub.docker.com), it contains the container images.

Running/Stop/Remove containers, checking container logs and processes:

// Creating "nginx" container:
PS C:\Users\MOHI\code> docker container run --publish 80:80 nginx   
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
8d691f585fa8: Pull complete
5b07f4e08ad0: Pull complete
abc291867bca: Pull complete
Digest: sha256:922c815aa4df050d4df476e92daed4231f466acc8ee90e0e774951b0fd7195a4
Status: Downloaded newer image for nginx:latest
172.17.0.1 - - [07/Nov/2019:06:27:11 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"

// Creating container in the background (detach option):
PS C:\Users\MOHI\code> docker container run --publish 80:80 --detach nginx   
9289009b1e8f5c7545d72a4a2dba3bcc9834d97420ceba716c7d171c6c4b5c00
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: driver failed programming external connectivity on endpoint mystifying_proskuriakova (cd4f71e37af9ae6bdf70a1e9474b6b51850779b21fbca2142acedd494333729b): Bind for 0.0.0.0:80 failed: port is already allocated.

// List only the running containers:
PS C:\Users\MOHI\code> docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
909529f2842f        nginx               "nginx -g 'daemon of…"   7 minutes ago       Up 7 minutes        0.0.0.0:80->80/tcp   crazy_cartwright

// Stopping the container 909:
PS C:\Users\MOHI\code> docker container stop 909
909

// List only the running containers:
PS C:\Users\MOHI\code> docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

// List all the containers:
PS C:\Users\MOHI\code> docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                      PORTS               NAMES
9289009b1e8f        nginx               "nginx -g 'daemon of…"   About a minute ago   Created                                         mystifying_proskuriakova
909529f2842f        nginx               "nginx -g 'daemon of…"   9 minutes ago        Exited (0) 13 seconds ago                       crazy_cartwright
8a9ac6864e14        hello-world         "/hello"                 3 days ago           Exited (0) 3 days ago                           gifted_noether

 // Creating container with all the options:
PS C:\Users\MOHI\code> docker container run --publish 80:80 --detach --name webhost nginx  
ef5cca8b15bba525304979aec0f76aa2792e971d947f87f22b578128529676b7

// List all the containers:
PS C:\Users\MOHI\code> docker container ls -a  
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                NAMES
ef5cca8b15bb        nginx               "nginx -g 'daemon of…"   7 seconds ago       Up 2 seconds               0.0.0.0:80->80/tcp   webhost
9289009b1e8f        nginx               "nginx -g 'daemon of…"   6 minutes ago       Created                                         mystifying_proskuriakova
909529f2842f        nginx               "nginx -g 'daemon of…"   13 minutes ago      Exited (0) 4 minutes ago                        crazy_cartwright
8a9ac6864e14        hello-world         "/hello"                 3 days ago          Exited (0) 3 days ago                           gifted_noether

// List only the running containers:
PS C:\Users\MOHI\code> docker container ls  
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
ef5cca8b15bb        nginx               "nginx -g 'daemon of…"   12 seconds ago      Up 8 seconds        0.0.0.0:80->80/tcp   webhost

// Checking the log:
PS C:\Users\MOHI\code> docker container logs webhost   
172.17.0.1 - - [07/Nov/2019:06:40:41 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:42 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:42 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:44 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:44 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:44 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"

// Older command to check the log:
PS C:\Users\MOHI\code> docker logs webhost   
172.17.0.1 - - [07/Nov/2019:06:40:41 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:42 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:42 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:44 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:44 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"
172.17.0.1 - - [07/Nov/2019:06:40:44 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" "-"

// Running processes inside container:
PS C:\Users\MOHI\code> docker container top webhost   
PID                 USER                TIME                COMMAND
2821                root                0:00                nginx: master process nginx -g daemon off;
2869                101                 0:00                nginx: worker process

// List all the containers:
PS C:\Users\MOHI\code> docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
ef5cca8b15bb        nginx               "nginx -g 'daemon of…"   8 minutes ago       Up 8 minutes                0.0.0.0:80->80/tcp   webhost
9289009b1e8f        nginx               "nginx -g 'daemon of…"   14 minutes ago      Created                                          mystifying_proskuriakova
909529f2842f        nginx               "nginx -g 'daemon of…"   22 minutes ago      Exited (0) 12 minutes ago                        crazy_cartwright
8a9ac6864e14        hello-world         "/hello"                 3 days ago          Exited (0) 3 days ago                            gifted_noether

// Deleting the containers, Note: It can not delete the running container start with "ef5":
PS C:\Users\MOHI\code> docker container rm ef5 928 909 8a9 
928
909
8a9
Error response from daemon: You cannot remove a running container ef5cca8b15bba525304979aec0f76aa2792e971d947f87f22b578128529676b7. Stop the container before attempting removal or force remove

// "ef5" container is still running:
PS C:\Users\MOHI\code> docker container ls -a   
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
ef5cca8b15bb        nginx               "nginx -g 'daemon of…"   10 minutes ago      Up 10 minutes       0.0.0.0:80->80/tcp   webhost

We need to stop the container and delete or can delete it forcefully

// Forcefully deleting the container:
PS C:\Users\MOHI\code> docker container rm -f ef5  
ef5

// List all the containers:
PS C:\Users\MOHI\code> docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


No comments:

Post a Comment