Skip to content

Running Containers

Tanveer Alam edited this page Jan 26, 2019 · 1 revision
[tan@localhost run]$ sudo yum install net-tools -y

Default docker network

[tan@localhost run]$ ifconfig 
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:42ff:fe00:8c4  prefixlen 64  scopeid 0x20<link>
        ether 02:42:42:00:08:c4  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1  bytes 90 (90.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Run container in interactive mode:

  • -i : interactive
  • -t : terminal

It will login with root user using the entry cmd being passed to it.

[tan@localhost run]$ docker run -it centos:latest /bin/bash
[root@774b39c9beef /]# whoami
root
[root@774b39c9beef /]# 

While this container is running open another terminal session and do $ docker ps

[tan@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
774b39c9beef        centos:latest       "/bin/bash"         3 minutes ago       Up 3 minutes                            nostalgic_albattani

Now exit from the running interactive container

[root@774b39c9beef /]# exit
exit
[tan@localhost run]$ 

Now do ( $ docker ps -a ) to see the exited containers

[tan@localhost run]$ docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                      PORTS               NAMES
774b39c9beef        centos:latest        "/bin/bash"         5 minutes ago       Exited (0) 51 seconds ago                       nostalgic_albattani

Detached mode

[tan@localhost run]$ docker run -d nginx:latest
afbbf9af9348d66d72fb1655390d0d52b433198a7a3b7f6cc00ab42b92b38740
[tan@localhost run]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
afbbf9af9348        nginx:latest        "nginx -g 'daemon of…"   5 seconds ago       Up 3 seconds        80/tcp              elegant_gauss
[tan@localhost run]$ 
[tan@localhost run]$ docker inspect afbbf9af9348
[
    {
        "Id": "afbbf9af9348d66d72fb1655390d0d52b433198a7a3b7f6cc00ab42b92b38740",
        "Created": "2019-01-26T11:24:50.722154058Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 37716,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-01-26T11:24:51.214480311Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:42b4762643dcc9bf492b08064b55fef64942f055f0da91289a8abf93c6d6b43c",
        "ResolvConfPath": "/var/lib/docker/containers/afbbf9af9348d66d72fb1655390d0d52b433198a7a3b7f6cc00ab42b92b38740/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/afbbf9af9348d66d72fb1655390d0d52b433198a7a3b7f6cc00ab42b92b38740/hostname",
        "HostsPath": "/var/lib/docker/containers/afbbf9af9348d66d72fb1655390d0d52b433198a7a3b7f6cc00ab42b92b38740/hosts",
        "LogPath": "/var/lib/docker/containers/afbbf9af9348d66d72fb1655390d0d52b433198a7a3b7f6cc00ab42b92b38740/afbbf9af9348d66d72fb1655390d0d52b433198a7a3b7f6cc00ab42b92b38740-json.log",
        "Name": "/elegant_gauss",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/bca21e4a074e29edce28503b3ceac849e93590fe4dbbd91e4eab90efd05eaa9f-init/diff:/var/lib/docker/overlay2/07d51068c22c872bf63103e3e629181cb071a72cde35eebc71184670931a66d6/diff:/var/lib/docker/overlay2/6a04731e736dada9cd10067952025933d0718aaa700b7c31b23b3f3a9e2e37ba/diff:/var/lib/docker/overlay2/13f1219f27348076dd11ac8511fec5b88d0366e16590c7b83ca1f038e10e61e1/diff",
                "MergedDir": "/var/lib/docker/overlay2/bca21e4a074e29edce28503b3ceac849e93590fe4dbbd91e4eab90efd05eaa9f/merged",
                "UpperDir": "/var/lib/docker/overlay2/bca21e4a074e29edce28503b3ceac849e93590fe4dbbd91e4eab90efd05eaa9f/diff",
                "WorkDir": "/var/lib/docker/overlay2/bca21e4a074e29edce28503b3ceac849e93590fe4dbbd91e4eab90efd05eaa9f/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "afbbf9af9348",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.15.8-1~stretch",
                "NJS_VERSION=1.15.8.0.2.7-1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "nginx:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "c4092d1eb04e39f02fcdcd374007fa8b7063d621364a0dc30690fcf01b3334bf",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/c4092d1eb04e",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "838d1ee9ba214a2dc4376b88ac5d512007eea210c563f56d5c847c74db839b73",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "1159295f500d0c1e85998d76908d8982171cc9521207c52d6d6b3e193fe8d4e6",
                    "EndpointID": "838d1ee9ba214a2dc4376b88ac5d512007eea210c563f56d5c847c74db839b73",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

Install elinks - its a text based browser

[tan@localhost run]$ sudo  yum install elinks -y
[tan@localhost run]$ elinks http://172.17.0.2

Following will not work as port 80 of container afbbf9af9348 (nginx:latest) is not mapped to any of host port

[tan@localhost run]$ elinks http://localhost
[tan@localhost run]$ elinks http://127.0.0.1

[tan@localhost run]$ telnet localhost 80
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
[tan@localhost run]$ 
[tan@localhost run]$ telnet 172.17.0.2 80
Trying 172.17.0.2...
Connected to 172.17.0.2.
Escape character is '^]'.
^CConnection closed by foreign host.

Stopping Up and running container:

[tan@localhost run]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
afbbf9af9348        nginx:latest        "nginx -g 'daemon of…"   29 minutes ago      Up 29 minutes       80/tcp              elegant_gauss
[tan@localhost run]$ docker stop elegant_gauss
elegant_gauss
[tan@localhost run]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[tan@localhost run]$ 
[tan@localhost run]$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                      PORTS               NAMES
afbbf9af9348        nginx:latest         "nginx -g 'daemon of…"   29 minutes ago      Exited (0) 4 seconds ago                        elegant_gauss

Running container with custom name to it.

[tan@localhost run]$ docker run -d --name=myweb1 nginx:latest
1804d0919acc548295424784c87cb6da49b03fbd5a8d715f1dabfc672f8aa604
[tan@localhost run]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1804d0919acc        nginx:latest        "nginx -g 'daemon of…"   9 seconds ago       Up 9 seconds        80/tcp              myweb1

Container's World



Clone this wiki locally