Showing posts with label exec. Show all posts
Showing posts with label exec. Show all posts

Saturday, 9 May 2020

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 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