Showing posts with label container. Show all posts
Showing posts with label container. Show all posts

Saturday, 9 May 2020

Docker: Run MySQL in a container

Step 1: Open a terminal and execute the below command.

docker run -d -e MYSQL_ROOT_PASSWORD=tiger mysql:latest

-d: Run the container in background, so we can't see all the log messages of docker in terminal.
-e: Used to set Environment Variables.
$docker run -d -e MYSQL_ROOT_PASSWORD=tiger mysql:latest
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
54fec2fa59d0: Pull complete 
bcc6c6145912: Pull complete 
951c3d959c9d: Pull complete 
05de4d0e206e: Pull complete 
319f0394ef42: Pull complete 
d9185034607b: Pull complete 
013a9c64dadc: Pull complete 
42f3f7d10903: Pull complete 
c4a3851d9207: Pull complete 
82a1cc65c182: Pull complete 
a0a6b01efa55: Pull complete 
bca5ce71f9ea: Pull complete 
Digest: sha256:61a2a33f4b8b4bc93b7b6b9e65e64044aaec594809f818aeffbff69a893d1944
Status: Downloaded newer image for mysql:latest
122d27c2a3467700df7fbf20f0b919da2cade9eadb30c745b35b42fbec1dff6f

Step 2: Run MySQL command in the running container using 'docker exec'

Syntax
docker exec -it {container_id/container_name} {command_to_execute}

Get all the running containers
$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
122d27c2a346        mysql:latest        "docker-entrypoint.s…"   54 seconds ago      Up 53 seconds       3306/tcp, 33060/tcp   elastic_tesla
As you see ‘elastic_tesla’ is the container name. Now execute below command.

docker exec -it elastic_tesla mysql -h localhost -u root -p

Enter the password as tiger.

$docker exec -it elastic_tesla mysql -h localhost -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Note
a. You can specify container name using –name option.

docker run -d --name krishna_sql -e MYSQL_ROOT_PASSWORD=tiger mysql:latest


Previous                                                    Next                                                    Home

Docker: inspect: Get more details about container

‘docker inspect {containerId/containerName}’ command is used to get more details about the container in json format.

Example
$docker inspect bda7390cac5b
[
    {
        "Id": "bda7390cac5baa788bcc2292c6376855940449b044d4275037c8d8f8daa02582",
        "Created": "2019-08-30T18:29:55.3309368Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 5409,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-08-30T18:29:55.9240131Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:62a9f311b99c24c0fde0a772abc6030bc48e5acc7d7416b8eeb72d3da1b4eb6c",
        "ResolvConfPath": "/var/lib/docker/containers/bda7390cac5baa788bcc2292c6376855940449b044d4275037c8d8f8daa02582/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/bda7390cac5baa788bcc2292c6376855940449b044d4275037c8d8f8daa02582/hostname",
        "HostsPath": "/var/lib/docker/containers/bda7390cac5baa788bcc2292c6376855940449b044d4275037c8d8f8daa02582/hosts",
        "LogPath": "/var/lib/docker/containers/bda7390cac5baa788bcc2292c6376855940449b044d4275037c8d8f8daa02582/bda7390cac5baa788bcc2292c6376855940449b044d4275037c8d8f8daa02582-json.log",
        "Name": "/mySqlServer_1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/Users/Shared/datadir:/var/lib/mysql"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "3306/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "3306"
                    }
                ]
            },
            "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/c8e2aaad4e3ae3f4c1458a776ad261164bc232267e99e83e2aeb365c271ad651-init/diff:/var/lib/docker/overlay2/9f307d7e99b69d1e8e5e00ced86fb93b17c48e89696f8df57b461c4aa582a914/diff:/var/lib/docker/overlay2/bd785e427f010228ec509c23030fcf88a26c9774ac19c5e884fa669a0b2b3195/diff:/var/lib/docker/overlay2/43f80e620ec70664547f329048b7b3f5bcd42e4b6ba11205f12bf1275c4801fc/diff:/var/lib/docker/overlay2/260170b54bf2dcbc6d9c9d5e65c61d3bd99ffe3831413713f6d8a632212b766d/diff:/var/lib/docker/overlay2/3b4895b5045f26846b03276733b022856dacc3dd9d3fc095b7c54baf7e275ad2/diff:/var/lib/docker/overlay2/8c1f6a89e6e62a5f448448ea9a35e6ef8a934afefb97805aa57bde56dda8bef9/diff:/var/lib/docker/overlay2/d2d710a9ff0da3cd2cc2e1deb8a97b9cae5cc558aafffd52cb4f31b604f15de0/diff:/var/lib/docker/overlay2/a0fcd3b88ea0a0c8256f1b73bd2016656389982ecb6a9d146cc6babcd7c9771d/diff:/var/lib/docker/overlay2/da3ee694f9ad03eeb54e0a5067a70438e0390fb3077ec242bb508453c9afb312/diff:/var/lib/docker/overlay2/d6e41e69f6d968b964785f8ac1033d97ef025328f7af5f51c8071e3f503204da/diff:/var/lib/docker/overlay2/a001fcf09a5d4c10b3f425ebb09f48f806441d4b64236aa46b11f7673d1dc2d9/diff:/var/lib/docker/overlay2/cc4c7a749fdcbfb2bc797c20b39e3a579705314cc634506011152960a1a11b17/diff",
                "MergedDir": "/var/lib/docker/overlay2/c8e2aaad4e3ae3f4c1458a776ad261164bc232267e99e83e2aeb365c271ad651/merged",
                "UpperDir": "/var/lib/docker/overlay2/c8e2aaad4e3ae3f4c1458a776ad261164bc232267e99e83e2aeb365c271ad651/diff",
                "WorkDir": "/var/lib/docker/overlay2/c8e2aaad4e3ae3f4c1458a776ad261164bc232267e99e83e2aeb365c271ad651/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/Users/Shared/datadir",
                "Destination": "/var/lib/mysql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "bda7390cac5b",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "MYSQL_ROOT_PASSWORD=tiger",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.7",
                "MYSQL_MAJOR=8.0",
                "MYSQL_VERSION=8.0.17-1debian9"
            ],
            "Cmd": [
                "mysqld"
            ],
            "ArgsEscaped": true,
            "Image": "mysql",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "15d54385d52fb95d6aecefb47a0fb23d7d43fbed1329980bca36f546a71887e5",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "3306"
                    }
                ],
                "33060/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/15d54385d52f",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "2b00bb1d496d12fcb2b64f3657ea7cdb7ac69c742f53452987eaa345807db257",
            "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": "c6ce40fd584448ab73a31e24c94da9edc37e0f5b4fb2078b19eae2e60fb1d771",
                    "EndpointID": "2b00bb1d496d12fcb2b64f3657ea7cdb7ac69c742f53452987eaa345807db257",
                    "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
                }
            }
        }
    }
]


Previous                                                    Next                                                    Home

Docker: -it: Connect to container stdin while running

You can use -it option to instructs Docker to allocate a pseudo-TTY connected to the container’s stdin.

$docker run -it ubuntu bash
root@723d542001e9:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@723d542001e9:/# 
root@723d542001e9:/# exit
exit
$



Previous                                                    Next                                                    Home

Docker Exec: Execute a command on a running container

‘docker exec’ is used to execute a command on running container.

For example,
For example, execute the command 'docker run ubuntu sleep 1000', it starts a container with ubuntu image and execute the sleep command.

Go to other terminal and execute the command ‘docker ps’ to see all active containers.
$docker run ubuntu echo "Hello World"
Hello World
$
$docker run ubuntu sleep 5
$
$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
e73d81bec53a        ubuntu              "sleep 5"                15 seconds ago      Exited (0) 9 seconds ago                        nervous_bhaskara
12bf7981e2c5        ubuntu              "echo 'Hello World'"     31 seconds ago      Exited (0) 29 seconds ago                       competent_pascal

Now use below syntax to execute a command on running container.

docker exec {containerId/containerName} {command}

$docker exec 26d4846180b3 cat /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi



Previous                                                    Next                                                    Home

docker restart: Restart the container

'docker restart' command is used to restart one or more containers.

Syntax
docker restart <container_id>

$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
a523924b741a        jboss/wildfly       "/opt/jboss/wildfly/…"   28 minutes ago      Exited (0) About a minute ago                        silly_cori

As you see the output of above command, container 'a523924b741a' stopped a minute ago. You can restart the container by executing below statement.

docker restart a523924b741a

$docker restart a523924b741a
a523924b741a
$
$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS                    NAMES
a523924b741a        jboss/wildfly       "/opt/jboss/wildfly/…"   34 minutes ago      Up 8 seconds                     0.0.0.0:1234->8080/tcp   silly_cori


Previous                                                    Next                                                    Home

docker kill vs docker stop

Both the commands 'docker kill' and 'docker stop' are used to stop the container.

'docker kill' command sends 'SIGKILL' signal to the container, whereas 'docker stop' sends 'SIGTERM' signal, after a grace period it sends SIGKILL signal.

SIGTERM vs SIGKILL
a. 'SIGTERM' command tries the kill the process in a polite way, whereas SIGKILL command kill the process in a harsh way.

b. 'SIGTERM' command internally executes the command 'kill', whereas 'SIGKILL' command internally executes 'kill -9'

c. SIGTERM command can be handled, but SIGKILL can't be handled.


Previous                                                    Next                                                    Home

docker: create, start: Create and start the container

'docker create' command is used to create a container, once the container is created, you can start the container using 'docker start' command.

Step 1: Execute the below command to create a container
docker create -p1234:8080 jboss/wildfly

$docker create -p1234:8080 jboss/wildfly
a523924b741accfb04a72d6322c6197c50b4be0b175b8a2d9e087d22d20a1786
$
$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
a523924b741a        jboss/wildfly       "/opt/jboss/wildfly/…"   3 seconds ago       Created                                              silly_cori

From the output, you can observe that container is created with id 'a523924b741a'.

Step 2: Start the container using 'docker start' command.
$docker start a523924b741a
a523924b741a

open the url 'http://localhost:1234/', you can able to see WildFly server home page.


You can even execute the command 'docker ps -a' to check whether container started or not.
$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                           PORTS                    NAMES
a523924b741a        jboss/wildfly       "/opt/jboss/wildfly/…"   About a minute ago   Up 50 seconds                    0.0.0.0:1234->8080/tcp   silly_cori



Previous                                                    Next                                                    Home

docker exec: Connect to the container

Step 1: Execute below command, that starts wildfly server and you can able to access the server at localhost port 1234.
docker run -d -p1234:8080 jboss/wildfly
$docker run -d -p1234:8080 jboss/wildfly
Unable to find image 'jboss/wildfly:latest' locally
latest: Pulling from jboss/wildfly
a02a4930cb5d: Pull complete 
b5ffff9dbcda: Pull complete 
36845d6d0218: Pull complete 
08619c8c88e2: Pull complete 
1779d505ac3a: Pull complete 
Digest: sha256:ae4545bc428d36fa77746f48c23f43f11158ea37723a6ec2b55d999b930bf0e4
Status: Downloaded newer image for jboss/wildfly:latest
bdc55593d441a1103c38658bab2133c1f50626eee86b944cf3a57f015e56e3d5

once the server started, open the URL 'http://localhost:1234/', you can able to see WildFly server home page.

Step 2: Get into the container using 'docker exec' command.

Execute the command 'docker ps -a' to see list of all (running, stopped) container.

$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                    NAMES
bdc55593d441        jboss/wildfly       "/opt/jboss/wildfly/…"   2 minutes ago       Up 2 minutes                   0.0.0.0:1234->8080/tcp   vigilant_mclaren

Execute the below command to get into the container.
docker exec -ti bdc55593d441 /bin/bash

'bdc55593d441' is container id.
$docker exec -ti bdc55593d441 /bin/bash
[jboss@bdc55593d441 ~]$ pwd
/opt/jboss
[jboss@bdc55593d441 ~]$ 
[jboss@bdc55593d441 ~]$ ls
wildfly
[jboss@bdc55593d441 ~]$ 
[jboss@bdc55593d441 ~]$ ls -l wildfly/
total 544
-rw-rw-r-- 1 jboss root  26530 Jun  7 17:39 LICENSE.txt
-rw-rw-r-- 1 jboss root   2221 Jun  7 17:39 README.txt
drwxrwxr-x 3 jboss root   4096 Jun  7 17:39 appclient
drwxrwxr-x 3 jboss root   4096 Jun 10 16:19 bin
-rw-rw-r-- 1 jboss root   2451 Jun  7 17:39 copyright.txt
drwxrwxr-x 6 jboss root   4096 Jun  7 17:39 docs
drwxrwxr-x 4 jboss root   4096 Jun  7 17:39 domain
-rw-rw-r-- 1 jboss root 489207 Jun  7 17:39 jboss-modules.jar
drwxrwxr-x 3 jboss root   4096 Jun  7 17:39 modules
drwxrwxr-x 1 jboss root   4096 Jun 19 05:34 standalone
drwxrwxr-x 2 jboss root   4096 Jun 10 16:19 welcome-content

Execute the command 'exit' to come out of the bash shell.



Previous                                                    Next                                                    Home

Tuesday, 4 June 2019

Docker: Getting into the container


Containers are good at running exactly one command. To get into the container, add 'bash' at the end of the command.

Example
docker run -it jboss/wildfly bash

Instead of running jboss/wildfly container, this command launch bash shell, which is pointing to wildfly container image.

$docker run -it jboss/wildfly bash
[jboss@ed446ef19d1c ~]$ ls
wildfly
[jboss@ed446ef19d1c ~]$ cd  wildfly/
[jboss@ed446ef19d1c wildfly]$ 
[jboss@ed446ef19d1c wildfly]$ ls
LICENSE.txt  README.txt  appclient  bin  copyright.txt  docs  domain  jboss-modules.jar  modules  standalone  welcome-content


Open other terminal and execute the command 'docker container ls -a' to see list of all the containers.

$docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ed446ef19d1c        jboss/wildfly       "bash"              47 seconds ago      Up 46 seconds       8080/tcp            sleepy_antonelli

As you see the output 'jboss/wildfly' container is started and running the command 'bash'.

How to exit from interactive shell?

Execute the command ‘exit’ to come out of interactive shell.

[jboss@ed446ef19d1c wildfly]$ pwd
/opt/jboss/wildfly
[jboss@ed446ef19d1c wildfly]$ 
[jboss@ed446ef19d1c wildfly]$ exit
exit
$




Previous                                                 Next                                                 Home