Get Started with Docker – Part 1 : Docker Hub, Pulling Images, and Basic Containers Manipulation
- Starting, Stopping, Restarting, and Deleting Containers
Once you get any container name/ID, you can start, stop, and delete it. Of course there are many other commands than those three commands, we’ll explain them later. The following examples will be against the container that has Name “centos_with_nama_newe1″ and has ID “7fc1cef7513c“
To start a stopped container either use it’s ID or it’s Name, run any of the following command:
# docker start centos_with_nama_newe1
OR
# docker start 7fc1cef7513c
If you run ” docker ps
” command , you’ll find that the container is up.
To stop a running container either use it’s ID or it’s Name, run any of the following command:
# docker stop centos_with_nama_newe1
OR
# docker stop 7fc1cef7513c
If you run ” docker ps
” command , you’ll find that the container is stopped “not found”.
To restart a running or a stopped container either use it’s ID or it’s Name, run any of the following command:
# docker restart centos_with_nama_newe1
OR
# docker restart 7fc1cef7513c
If you run ” docker ps
” command , you’ll find that the container is up since few seconds.
To stop a non-responding running container, and docker stop
command failed to stop it, use ” docker kill"
command instead, run the following command:
# docker kill ea3964d7ffea
Here’s we killed the non-responding running container.
Finally, if you do not need the container anymore, and want to delete it, run the following command if the container is stopped:
# docker rm ea3964d7ffea
But, if the container is running, and want to delete it either stop it first and then run the above command or use -f
option with the above command. Here’s I’ll force deletion for a running container, as the following:
# docker rm -f centos_with_nama_newe1
Before moving to the next point, here’s the error you’ll get if you tried to delete a running container without using -f
option:
# docker rm centos_with_name
Error response from daemon: You cannot remove a running container 8a1b73684a02f32f455881ab36180f691ff54673f816e7299cf99bcb1109c650. Stop the container before attempting removal or use -f
- 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:
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
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:
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.
summary
It’s very very important to use Docker in devops environment and in production systems, as it saves time consumed in preparing the environment for running your apps. In this article, I guided you to explore the Docker world. Now, you know some easy and important steps to get started with Docker containers, you learned how to search for and pull images from Decker hub, create your first Docker container, attach to a running containers, stop, start, delete containers, and finally execute commands in a container. This article contained important info and I tried to use simple steps.
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:
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.