Docker Commands to start/stop containers

Getting docker images

To search for available images in the repository..

[code language=”bash”]
$ docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 4585 [OK]
ansible/centos7-ansible Ansible on Centos7 115 [OK]
jdeathe/centos-ssh CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86… 99 [OK]
consol/centos-xfce-vnc Centos container with “headless” VNC session… 60 [OK]
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 44 [OK]
tutum/centos Simple CentOS docker image with SSH access 43
centos/mysql-57-centos7 MySQL 5.7 SQL database server 38
gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glust… 32 [OK]
openshift/base-centos7 A Centos7 derived base image for Source-To-I… 31
centos/python-35-centos7 Platform for building and running Python 3.5… 28
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 26
kinogmt/centos-ssh CentOS with SSH 22 [OK]
openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use w… 14
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 7
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
openshift/jenkins-1-centos7 DEPRECATED: A Centos7 based Jenkins v1.x ima… 4
openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for use … 4
darksheer/centos Base Centos Image — Updated hourly 3 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 2
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 1
jameseckersall/sonarr-centos Sonarr on CentOS 7 0 [OK]
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 0
smartentry/centos centos with smartentry 0 [OK]
pivotaldata/centos7-build CentosOS 7 image for GPDB compilation 0
[/code]

To download the image from the search results..

[code language=”bash”]
$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
256b176beaff: Pull complete
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for centos:latest
[/code]
After downloading the image, we may run the instance.

[code language=”bash”]
$ docker run centos
[/code]

Now, we have downloaded many images. To see the list of images..

[code language=”bash”]
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5182e96772bf 10 days ago 200MB
ubuntu latest 735f80812f90 2 weeks ago 83.5MB
hello-world latest 2cb0d9787c4d 5 weeks ago 1.85kB
[/code]
To run an interactive shell of centos, lets run it.

[code language=”bash”]
$ docker run -it centos
[root@53771652af4a /]# hostname
53771652af4a
[root@53771652af4a /]# id
uid=0(root) gid=0(root) groups=0(root)
[/code]

Customizing the containers

Let’s modify this container a bit. For example. following is how we install net-tools package

[code language=”bash”]
[root@53771652af4a /]# ifconfig
bash: ifconfig: command not found
[root@53771652af4a /]# yum install net-tools
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirror.0x.sg
* extras: mirror.0x.sg
* updates: mirror.0x.sg
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/4): base/7/x86_64/group_gz | 166 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 174 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 5.9 MB 00:00:00
(4/4): updates/7/x86_64/primary_db | 5.0 MB 00:00:01
Resolving Dependencies
–> Running transaction check
—> Package net-tools.x86_64 0:2.0-0.22.20131004git.el7 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================================
Installing:
net-tools x86_64 2.0-0.22.20131004git.el7 base 305 k

Transaction Summary
======================================================================================================================================================
Install 1 Package

Total download size: 305 k
Installed size: 917 k
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.22.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for net-tools-2.0-0.22.20131004git.el7.x86_64.rpm is not installed
net-tools-2.0-0.22.20131004git.el7.x86_64.rpm | 305 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : “CentOS-7 Key (CentOS 7 Official Signing Key) ”
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-5.1804.1.el7.centos.x86_64 (@Updates)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : net-tools-2.0-0.22.20131004git.el7.x86_64 1/1
Verifying : net-tools-2.0-0.22.20131004git.el7.x86_64 1/1

Installed:
net-tools.x86_64 0:2.0-0.22.20131004git.el7

Complete!
[root@53771652af4a /]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8448 bytes 12601933 (12.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4833 bytes 321266 (313.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[/code]

Similarly I have installed openssh server as well. As we customized our container now, we need to commit it to retain the updated copy. To commit it, lets exit the container shell and execute the following command from the docker machine.

[code language=”bash”]
$ docker commit -m “added net-tools and ssh server” -a “pandian” 53771652af4a pandian/centos_ssh
sha256:7f019be4424c070511efb90e357b720333df5d80f845de6a7d569b94ac829fd7
[/code]

To get the container id, you may use docker ps command as below.

[code language=”bash”]
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53771652af4a centos “/bin/bash” 27 minutes ago Exited (0) 7 minutes ago relaxed_vaughan
f6ed55b0451c centos “/bin/bash” 33 minutes ago Exited (0) 33 minutes ago relaxed_rosalind
462658ae9e5a ubuntu “bash” 15 hours ago Up 15 hours inspiring_mahavira
1aebd1e64faa ubuntu “bash” 15 hours ago Exited (127) 15 hours ago gracious_banach
ff56bc579e04 hello-world “/hello” 15 hours ago Exited (0) 15 hours ago distracted_lamarr
e209861d337d hello-world “/hello” 15 hours ago Exited (0) 15 hours ago laughing_ramanujan
[/code]

As we commited our image, lets see the image list now. the repository has the updated image.

[code language=”bash”]
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
pandian/centos_ssh latest 7f019be4424c 25 minutes ago 294MB
centos latest 5182e96772bf 10 days ago 200MB
ubuntu latest 735f80812f90 3 weeks ago 83.5MB
hello-world latest 2cb0d9787c4d 5 weeks ago 1.85kB

[/code]

Starting and stopping instances

Here is the way we stop the instances.

[code language=”bash”]
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
462658ae9e5a ubuntu “bash” 15 hours ago Up 15 hours inspiring_mahavira
$ docker stop 462658ae9e5a
462658ae9e5a
[/code]

To start the instance the instance customized by us..

[code language=”bash”]
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
pandian/centos_ssh latest 7f019be4424c 43 minutes ago 294MB
centos latest 5182e96772bf 10 days ago 200MB
ubuntu latest 735f80812f90 3 weeks ago 83.5MB
hello-world latest 2cb0d9787c4d 5 weeks ago 1.85kB
$ docker run -it pandian/centos_ssh
[root@d66d18a06066 /]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 25 bytes 3024 (2.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[/code]

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top