Site icon UnixArena

Docker Basic Operations – Images and Containers

Docker - Basic Operations - Images and Containers

Docker - Basic Operations - Images and Containers

This article will help you to understand the basic operations of dockers images and containers. Images and containers are two basic components of Docker’s architecture.  A container is launched using an image. An image is a fully packed stack which has everything that needed by an application i.e runtime libraries, environment variable, and configuration files. In the last article, we have demonstrated docker functions by pulling image (hello-world) and ran it.

 

Docker Basic Operations – Images and Containers

Login to the server where docker is installed.

1. List the available docker images which are residing on the host node.

[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              113a43faa138        10 days ago         81.2MB
hello-world         latest              e38bc07ac18e        2 months ago        1.85kB
[root@node1 ~]#

 

2. List the running Docker container.

[root@node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@node1 ~]#

There is no container currently running on this host.

 

3. Let’s start the new container using ubuntu image. (image id – 113a43faa138. Refer step:1)

Syntax: docker run -it -d image_id command
[root@node1 ~]# docker run -it -d 113a43faa138 /bin/bash
5e5f9aaebbae36934f5e347f074a113fe264ab413f53dbc8a0142dbc16a15f32
[root@node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5e5f9aaebbae        113a43faa138        "/bin/bash"         5 seconds ago       Up 4 seconds                            priceless_chatterjee
[root@node1 ~]#

Here, docker will deploy an image on the new container with random container id and name.

 

4. How to interact with active docker container? In other words, how to login to ubuntu container instance? Let’s login to the above mentioned running container (5e5f9aaebbae).

Syntax: docker attach container_id
[root@node1 ~]# docker attach 5e5f9aaebbae
root@5e5f9aaebbae:/# df -h /
Filesystem      Size  Used Avail Use% Mounted on
overlay          18G  1.4G   17G   8% /
root@5e5f9aaebbae:/# uname -a
Linux 5e5f9aaebbae 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
root@5e5f9aaebbae:/#

 

5. If you exit from the prompt, container instance will be terminated immediately.

root@5e5f9aaebbae:/# exit
exit
[root@node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@node1 ~]#

 

6. To list the terminated containers and running containers, use “-a” option.

[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
5e5f9aaebbae        113a43faa138        "/bin/bash"         About an hour ago   Exited (0) About a minute ago                       priceless_chatterjee
7f3d9faa58ad        113a43faa138        "/bin/bash"         2 hours ago         Exited (0) About an hour ago                        jovial_brahmagupta
00aac448e81b        ubuntu              "bash"              2 hours ago         Exited (0) 2 hours ago                              sad_lalande
d87574c80674        ubuntu              "bash"              2 hours ago         Exited (0) 2 hours ago                              affectionate_kapitsa
0c737587f1a8        ubuntu              "bash"              3 hours ago         Exited (0) 2 hours ago                              reverent_hopper
356c7a9f9438        hello-world         "/hello"            21 hours ago        Exited (0) 21 hours ago                             lucid_cray
[root@node1 ~]#

 

You could also use the following command to list the all the available containers

[root@node1 ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMES
404b151ed97e        113a43faa138        "/bin/bash"         5 minutes ago       Up 5 minutes                                          flamboyant_wilson
5e5f9aaebbae        113a43faa138        "/bin/bash"         About an hour ago   Exited (127) About a minute ago                       priceless_chatterjee
7f3d9faa58ad        113a43faa138        "/bin/bash"         2 hours ago         Exited (127) 37 seconds ago                           jovial_brahmagupta
00aac448e81b        ubuntu              "bash"              3 hours ago         Exited (0) 3 hours ago                                sad_lalande
d87574c80674        ubuntu              "bash"              3 hours ago         Exited (0) 3 hours ago                                affectionate_kapitsa
0c737587f1a8        ubuntu              "bash"              3 hours ago         Exited (0) 3 hours ago                                reverent_hopper
356c7a9f9438        hello-world         "/hello"            21 hours ago        Exited (0) 21 hours ago                               lucid_cray
[root@node1 ~]#

Here only one container is active and others are in the “Exited” state.

 

7. How to exit from a container without terminating it? Press Ctrl+p then Ctrl+q .

[root@node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
404b151ed97e        113a43faa138        "/bin/bash"         4 seconds ago       Up 2 seconds                            flamboyant_wilson
[root@node1 ~]# docker attach 404b151ed97e
root@404b151ed97e:/#
root@404b151ed97e:/# read escape sequence
[root@node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
404b151ed97e        113a43faa138        "/bin/bash"         35 seconds ago      Up 33 seconds                           flamboyant_wilson
[root@node1 ~]#

 

8. To know the docker version, use the following command.

[root@node1 ~]# docker --version
Docker version 18.03.1-ce, build 9ee9f40
[root@node1 ~]# docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:20:16 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:23:58 2018
  OS/Arch:      linux/amd64
  Experimental: false
[root@node1 ~]#

 

9. To know more information about docker root directory, No of containers and images, use the following command.

[root@node1 ~]# docker info
Containers: 7
 Running: 1
 Paused: 0
 Stopped: 6
Images: 2
Server Version: 18.03.1-ce
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-862.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 974.6MiB
Name: node1
ID: YTO7:KPXU:OCR5:PTAY:RLSG:GUPE:XFIY:KG4R:J6Q5:CR5J:QTR3:EYAB
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

[root@node1 ~]# 

 

Exit mobile version