Learn Basic Docker Containers Manipulation by Examples – Part 1

undefined
  • Reconnecting to Containers and Executing Commands in Containers

To reconnect/attach to the running container run ” docker attach “command by specifying container ID or name, as following:

# docker attach centos_with_name
[root@8a1b73684a02 /]# ls
anaconda-post.log dev home lib64 media opt root sbin sys tmp var
bin etc lib lost+found mnt proc run srv testttttt usr

As you see, we now inside the container, and ran ls command. Now, we’ll run the top command to see what processes are running inside this container

[root@8a1b73684a02 /]# top

Here’s the top command output, only two processes are running. The main process is bash “the one with PID 1″ which ” docker attach " is connected to.

top - 21:27:34 up 1 day, 8:53, 0 users, load average: 5.87, 5.75, 5.73
Tasks: 2 total, 1 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 71.7 us, 9.1 sy, 0.1 ni, 18.8 id, 0.2 wa, 0.0 hi, 0.2 si, 0.0 st
KiB Mem : 7895808 total, 1717136 free, 4072384 used, 2106288 buff/cache
KiB Swap: 6279156 total, 6256820 free, 22336 used. 2629224 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 
 1 root 20 0 11776 1900 1516 S 0.0 0.0 0:00.07 bash 
 19 root 20 0 51892 1880 1384 R 0.0 0.0 0:00.00 top

To quit and return to host from the running container session you must type exit command. The exit command terminates all the container processes and stops it WHY?.

# exit

As "docker attach" command is connecting to the main process “bash in our example” when we issued exit command it killed the main process which caused the container to stop.

Because we used options -it when created our container, we can keep the container in running state but exit from the interactive session by pressing Ctrl+p and Ctrl+q keys or simply Ctrl+p+q keys. By using  these keys, we can quit the console and return to host terminal WITHOUT TERMINATING THE RUNNING CONTAINER.

The following image shows that we created a container from centos image with -it option to run /bin/bash , we exited from the container and keep it running by pressing Ctrl+p+q keys, then we attached to it again and ran exit command which terminated it, the steps and check found in the below images:

undefined

You can execute command in a container without connected to it by complete reading the next point:

  • Executing Commands in Containers Without Connecting to it

Do we really need to connect to a running containers to run any command inside it? ABSOLUTELY NO.

We can run any command in a running container without connecting/attaching to it using “docker exec" command.

Here’s the syntax of “docker exec" command:

# docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

"docker exec" command comes with some options, the most important options are -it “we previously explained them", so, let’s give some examples.

Example 1: We’ll run ls command in a container:

[mohammed.semari@localhost ~]$ docker exec -it bb104b26437d ls -l
total 28
-rw-r--r-- 1 root root 18307 Nov 2 12:47 anaconda-post.log
lrwxrwxrwx 1 root root 7 Nov 2 12:45 bin -> usr/bin
drwxr-xr-x 5 root root 380 Jan 31 22:04 dev
drwxr-xr-x 48 root root 4096 Jan 31 22:04 etc
drwxr-xr-x 2 root root 6 Aug 12 2015 home
lrwxrwxrwx 1 root root 7 Nov 2 12:45 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 2 12:45 lib64 -> usr/lib64
drwx------ 2 root root 6 Nov 2 12:44 lost+found
drwxr-xr-x 2 root root 6 Aug 12 2015 media
drwxr-xr-x 2 root root 6 Aug 12 2015 mnt
drwxr-xr-x 2 root root 6 Aug 12 2015 opt
dr-xr-xr-x 448 root root 0 Jan 31 22:04 proc
dr-xr-x--- 2 root root 108 Nov 2 12:47 root
drwxr-xr-x 10 root root 121 Nov 2 12:47 run
lrwxrwxrwx 1 root root 8 Nov 2 12:45 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Aug 12 2015 srv
dr-xr-xr-x 13 root root 0 Jan 31 22:04 sys
drwxrwxrwt 7 root root 111 Nov 2 12:47 tmp
drwxr-xr-x 13 root root 143 Nov 2 12:45 usr
drwxr-xr-x 18 root root 4096 Nov 2 12:47 var

Example 2: We’ll run top command in a container:

# docker exec -it dreamy_agnesi top

From the output below, you see that the main process is bash

undefined

Example 3: We’ll use docker exec command to connect to a running container by creating a new bash process in the container, run the following command:

# docker exec -it hopeful_swartz /bin/bash

Once we run this command, we directly attached/connected to the container through a new bash process “we didn’t attach to the main container process“. Go and run top command and you’ll find that the container has two bash processes, and one top process “the one we just ran”, see the following image:

undefined

To exit this container just run exit command. The container in this case will still running as we only terminated the bash process we just created “not the main container process“.

as a prove, I’ll run another command in the container I just exited from, run the following ls command:

[mohammed.semari@localhost ~]$ docker exec -it hopeful_swartz ls
anaconda-post.log dev home lib64 media opt root sbin sys usr
bin etc lib lost+found mnt proc run srv tmp var

The Final Example: To import a database inside a container, we use docker exec command to run a full mysql database restore command, as the following

# docker exec -i mimastech mysql -uroot -ppassword mimastech_development < mysql.sql

Here’s we imported a database mimastech_development inside our database container “mimastech”. YOU MUST USE -i OPTION WITH THE ABOVE COMMAND.

Go ahead and change mysql user, password and database name to use this command.

 

 

I hope this article is good enough for you.
See you in other articles.

 

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

One Comment to Learn Basic Docker Containers Manipulation by Examples – Part 1

  1. Moustafa says:

    Great Article, thank you!

Leave a Reply

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