DOCKER-3 (Basic Commands)
What we will learn in this post,
·
Docker
basic commands
·
Run
& Pull Docker Images
·
Create
container from image
·
See
the running or exited containers
·
Remove
container & images
·
Running
a container
Let’s start…
# docker run ubuntu
What does it mean?
Docker will search the image locally if found then it will create a ubuntu container from that image, otherwise it will go to docker hub to pull the image and then loads up the container and then runs a command in that container. Though we did not provide any command or argument here, hence the container will booted, ran an empty command and then exited.
From below command we can see the images locally (we need to pull)
[root@centos7-i2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
SIZE
REPOSITORY: name of
image
TAG: version of
Image
IMAGE ID: Unique Random
no for identification
CREATED: when it
was created by actual owners/developers
SIZE: size of image
# docker ps = to show running containers
# docker ps -a = to show all containers
[root@centos7-i2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
I do not have any image or container.
[root@centos7-i2 ~]# docker pull ubuntu
Using
default tag: latest
latest:
Pulling from library/ubuntu
7b1a6ab2e44d:
Pull complete
Digest:
sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status:
Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
I used the command docker pull ubuntu, which fetched the latest image of ubuntu and saved locally.
[root@centos7-i2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 35 hours ago 72.8MB
[root@centos7-i2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Now I have one image and no container, let’s create one.
[root@centos7-i2 ~]# docker run ubuntu
[root@centos7-i2 ~]# docker ps -a
CONTAINER
ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
1426a0b641e3 ubuntu "bash" 10 seconds ago Exited (0) 7 seconds ago modest_goldstine
How the container created? It’s created from the image we pulled before sometime.
Is this the only way? Let’s check.
First remove everything we pulled/created.
[root@centos7-i2 ~]# docker rm 1426a0b641e3
1426a0b641e3
[root@centos7-i2 ~]# docker rmi ubuntu
Untagged: ubuntu:latest
Untagged:
ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Deleted: sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1
Deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b
***IMP*** You cannot remove running containers
Now we are cleaned,
[root@centos7-i2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@centos7-i2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Now do the “docker run” directly without pulling.
[root@centos7-i2 ~]# docker run ubuntu
Unable to find image 'ubuntu:latest' locally <= see, local is
first preference
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest:
sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
[root@centos7-i2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 35 hours ago 72.8MB
[root@centos7-i2 ~]# docker ps -a
CONTAINER
ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
9b3bd4030a50 ubuntu "bash" 18 seconds ago Exited (0) 15 seconds ago gracious_euclid
Both purpose solved, it pulled an image and created container from it also.
Now we understand difference between pull & run.
What is ps -a (list all containers) whereas ps only lists running containers
Let’s see how many images we have
[root@centos7-i2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 8 days ago
72.8MB
centos latest 5d0da3dc9764 5 weeks ago 231MB
Let’s check how many running containers we have,
[root@centos7-i2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
No running container, let’s see the status of all containers.
[root@centos7-i2 ~]# docker ps -a
CONTAINER
ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
8ccf10715e1b ubuntu
"bash" 3 days
ago Exited (127) 3 days ago vigilant_swirles
0f761dc69b18 ubuntu
"bash" 3 days
ago Exited (0) 3 days ago xenodochial_morse
9b3bd4030a50 ubuntu "bash" 6 days ago Exited (0) 6 days ago gracious_euclid
All exited, they are of no use. Let’s remove all.
Removal one by one,
[root@centos7-i2 ~]# docker rm 8ccf10715e1b
8ccf10715e1b
Above command will remove selected container.
Below command will remove all containers in one go.
[root@centos7-i2 ~]# docker ps -a
CONTAINER
ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
0f761dc69b18 ubuntu
"bash" 3 days
ago Exited (0) 3 days ago xenodochial_morse
9b3bd4030a50 ubuntu "bash" 6 days ago Exited (0) 6 days ago gracious_euclid
Removal in one go,
[root@centos7-i2 ~]# docker container prune
WARNING!
This will remove all stopped containers.
Are
you sure you want to continue? [y/N] y
Deleted
Containers:
0f761dc69b181ee572fab853c3dd949ae0c223006202fcf24d6f0f12da12f543
9b3bd4030a504dc693f28e4b7566dc0c08fe0b7a1f64559c65b95a0769edb3b0
Total reclaimed space: 0B
All gone,
[root@centos7-i2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Let’s create containers, (there are 3 ways to run)
run
run with detach mode
run with interactive mode to get immediate login
Modes, -d & -it
-d for detach mode
-i for interactive mode
-t to assign terminal
By –it we can immediately login to container after creation
[root@centos7-i2 ~]# docker run centos
[root@centos7-i2 ~]# docker run -d centos
6319643e9bba5a080bdf294c9632e8bb68775bcb490e4e615c0affc2c1148a7d
[root@centos7-i2 ~]# docker run -it centos bash
[root@3b51b523cf0f /]# hostname
3b51b523cf0f
[root@3b51b523cf0f /]# ip a
1:
lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group
default qlen 1000
link/loopback 00:00:00:00:00:00 brd
00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
30:
eth0@if31: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state
UP group default
link/ether 02:42:ac:11:00:02 brd
ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope
global eth0
valid_lft forever preferred_lft forever
[root@3b51b523cf0f /]# exit
Exit
[root@centos7-i2 ~]# docker ps
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES
Strange!!! No running containers
[root@centos7-i2 ~]# docker ps -a
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b51b523cf0f centos
"bash" 23 seconds
ago Exited (0) 10 seconds ago funny_gould
6319643e9bba centos
"/bin/bash" 44 seconds
ago Exited (0) 41 seconds ago suspicious_sinoussi
0b4e93161d51 centos
"/bin/bash" About a
minute ago Exited (0) 55 seconds ago competent_murdock
Let’s try to start containers,
[root@centos7-i2 ~]# docker start funny_gould
funny_gould
[root@centos7-i2 ~]# docker ps
CONTAINER
ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
3b51b523cf0f centos
"bash" 7 minutes
ago Up 2 seconds funny_gould
[root@centos7-i2 ~]# docker start suspicious_sinoussi
suspicious_sinoussi
[root@centos7-i2 ~]# docker ps
CONTAINER
ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
3b51b523cf0f centos
"bash" 7 minutes
ago Up 24 seconds funny_gould
[root@centos7-i2 ~]# docker start competent_murdock
competent_murdock
[root@centos7-i2 ~]# docker ps
CONTAINER
ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
3b51b523cf0f centos
"bash" 8 minutes
ago Up 58 seconds funny_gould
[root@centos7-i2 ~]#
Only one started, which one is this? Its container which run with –it
option
You can identify via looking at COMMAND option in ps –a output.
Thanks to below authors for writing excellent explanation for
Why container exit after docker run
https://webkul.com/blog/docker-container-will-automatically-stop-run/
http://coddingbuddy.com/article/50776509/why-docker-container-exits-immediately
Great content. We are also looking for differences in windows environment, if any major.
ReplyDeleteDear Alok, Thanks for reading and appreciating my work. I will try to point out the differences very soon.
Delete