Wise people learn when they can; fools learn when they must - Arthur Wellesley

Sunday, 3 April 2022

DOCKER 4

 

                        DOCKER-4   

What we will learn in this post,

·         HOW TO RUN CONTAINER PROPERLY.

·         HOW TO ACCESS RUNNING CONTAINER.

·         NAME YOUR CONTAINER

·         AUTO RUN AFTER REBOOT

·         ACCESSING RUNNING CONTAINER AND EXIT WITHOUT EXITING CONTAINER


Let’s start… 

HOW TO RUN CONTAINER PROPERLY,

[root@centos7-i2 ~]# docker run -d centos

Unable to find image 'centos:latest' locally

latest: Pulling from library/centos

a1d0c7532777: Pull complete

Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177

Status: Downloaded newer image for centos:latest

658a4b6af7104de545e8a6f5eb1ec218973aadb7d1dc7a230c36e4d8a92eb668

 

[root@centos7-i2 ~]# docker ps -a

CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES

658a4b6af710   centos    "/bin/bash"   52 seconds ago   Exited (0) 47 seconds ago             intelligent_cartwright         

[root@centos7-i2 ~]# docker run -it centos bash

[root@e31a90bd220d /]# hostname

e31a90bd220d

[root@e31a90bd220d /]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default

    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

6: eth0: <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

    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0

       valid_lft forever preferred_lft forever

[root@e31a90bd220d /]# id

uid=0(root) gid=0(root) groups=0(root)

[root@e31a90bd220d /]# exit

Exit

[root@centos7-i2 ~]# docker ps -a

CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS                          PORTS     NAMES

e31a90bd220d   centos    "bash"        32 seconds ago       Exited (0) 7 seconds ago                  condescending_lamport

658a4b6af710   centos    "/bin/bash"   About a minute ago   Exited (0) About a minute ago             intelligent_cartwright

[root@centos7-i2 ~]# docker start condescending_lamport

condescending_lamport

[root@centos7-i2 ~]# docker ps -a

CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS                     PORTS     NAMES

e31a90bd220d   centos    "bash"        About a minute ago   Up 9 seconds                         condescending_lamport

658a4b6af710   centos    "/bin/bash"   2 minutes ago        Exited (0) 2 minutes ago             intelligent_cartwright

[root@centos7-i2 ~]#

HOW TO ACCESS RUNNING CONTAINER,

[root@centos7-i2 ~]# docker exec -it centos bash

Error: No such container: centos

[root@centos7-i2 ~]# docker ps -a

CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES

e31a90bd220d   centos    "bash"        4 minutes ago   Up 3 minutes                         condescending_lamport

658a4b6af710   centos    "/bin/bash"   5 minutes ago   Exited (0) 5 minutes ago             intelligent_cartwright

we should use either container name or container ID,

[root@centos7-i2 ~]# docker exec -it e31a90bd220d bash

[root@e31a90bd220d /]# pwd

/

[root@e31a90bd220d /]#

[root@e31a90bd220d /]# df -kh

Filesystem                                                                                         Size  Used Avail Use% Mounted on

/dev/mapper/docker-253:0-1260683-83aa74622d10e888a925494bcfb0532d68abd70868deac9229da88993d3644bf   10G  277M  9.8G   3% /

tmpfs                                                                                               64M     0   64M   0% /dev

tmpfs                                                                                              994M     0  994M   0% /sys/fs/cgroup

shm                                                                                                 64M     0   64M   0% /dev/shm

/dev/mapper/centos-root                                                                             18G  5.8G   12G  33% /etc/hosts

tmpfs                                                                                              994M     0  994M   0% /proc/asound

tmpfs                                                                                              994M     0  994M   0% /proc/acpi

tmpfs                                                                                              994M     0  994M   0% /proc/scsi

tmpfs                                                                                              994M     0  994M   0% /sys/firmware

[root@e31a90bd220d /]#

[root@e31a90bd220d /]# exit

exit

[root@centos7-i2 ~]#

NAME YOUR CONTAINER,

[root@centos7-i2 ~]# docker run --name MYCENTOS_1 -it centos bash

Use --name flag to name your container

AUTO RUN AFTER REBOOT,

[root@centos7-i2 ~]# docker run --name MYCENTOS_1 --restart always -it centos bash

[root@2ca5c2120675 /]# exit

exit

[root@centos7-i2 ~]# docker ps -a

CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                         PORTS     NAMES

2ca5c2120675   centos    "bash"        18 seconds ago   Restarting (0) 2 seconds ago             MYCENTOS_1

e31a90bd220d   centos    "bash"        2 months ago     Exited (255) 2 weeks ago     condescending_lamport

658a4b6af710   centos    "/bin/bash"   2 months ago     Exited (0) 2 months ago      intelligent_cartwright

[root@centos7-i2 ~]# docker start MYCENTOS_1

MYCENTOS_1

[root@centos7-i2 ~]#

[root@centos7-i2 ~]# docker ps

CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES

2ca5c2120675   centos    "bash"    3 minutes ago   Up 3 minutes             MYCENTOS_1

[root@centos7-i2 ~]# reboot

[root@centos7-i2 ~]# uptime

 15:45:45 up 1 min,  2 users,  load average: 4.60, 1.69, 0.61

[root@centos7-i2 ~]# docker ps

CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS          PORTS     NAMES

2ca5c2120675   centos    "bash"    6 minutes ago   Up 32 seconds             MYCENTOS_1

ACCESSING RUNNING CONTAINER AND EXIT WITHOUT EXITING CONTAINER,

[root@centos7-i2 ~]# docker exec -it MYCENTOS_1 bash

[root@2ca5c2120675 /]# pwd

/

[root@2ca5c2120675 /]# hostname

2ca5c2120675

[root@2ca5c2120675 /]# id

uid=0(root) gid=0(root) groups=0(root)

[root@2ca5c2120675 /]# date

Sun Apr  3 10:20:18 UTC 2022

Now press ctrl +p then ctrl +q to exit. Container will remain in running mode.

[root@2ca5c2120675 /]# read escape sequence

[root@centos7-i2 ~]# docker ps

CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS         PORTS     NAMES

2ca5c2120675   centos    "bash"    11 minutes ago   Up 5 minutes             MYCENTOS_1

[root@centos7-i2 ~]#

1 comment: