How To Manage Docker Container Services From Docker Host – Part 8

Well in our last tutorial we manage to setup apache server using a Docker file and we learn the possibility and scope of using Docker file in automating build and configuration a Docker images.

But, it is a bit difficult to manage Docker container services by attaching Docker every time when we need to manage Docker container services like stopping or starting apache. Also, we cannot remember ContainerID every time for access Docker services.

For Ex. we Use docker stop command followed by the container ID or name to stop a container
# docker stop <name or ContainerID>

So we came up with creating system-wide configuration files to access and manage Docker containers with ease.

Just in case, if you did Miss Previous tutorials regarding Docker, You can read here:

Part 1: What Is Docker And Concept Of Containers With Virtualization?
Part 2: How To Install Docker On CentOS/RHEL 6/7? And Learn Docker HUB Registration
Part 3A: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 A
Part 3B: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 B
Part 4: Learn To Install, Run And Delete Applications Inside Docker Containers – Part 4
Part 5: Learn To Push Custom Build Docker Image On Docker HUB (Repository) 
Part 6: What Are Docker File Learns To Use Them? | Simple Demo

Prerequisite:

Apache ContainerPart 7

Scenario :

Host OS: CentOS 7 x86_64
Host IP: 192.168.1.188
RAM: 4GB memory,
Network Port: 1GB/s
HDD: 1000GB of disk space.


Let’s start

Step 1: Name Your Docker Container (something rememberable)

As described above To do several tasks it is little tidy to use remember container id every time so to fix this problem you may name you Docker container with a unique name(descriptive name) and use that name to hit any command.

[root@localhost ~]# docker run --name webserver -it -p 81:80 centos-apache /bin/bash 
[root@33dc37ff953c /]# httpd &
[1] 15
[root@33dc37ff953c /]# AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[1]+ Done httpd
[1] 15 ----------- Press CTRL+p then CTRL+q 
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33dc37ff953c centos-apache "/bin/bash" 28 seconds ago Up 21 seconds 0.0.0.0:81->80/tcp webserver

Now you can use this descriptive name replacing containerID to run above-mentioned commands (start, stop, top, stats etc.).

[root@localhost ~]# docker stats webserver
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
webserver 0.00% 12.3 MiB / 3.618 GiB 0.33% 648 B / 648 B 10.59 MB / 0 B 7

Step 2: Creating File for Docker Container

On CentOS/RHEL 7 you can create a systemd configuration file and manage the container services normally as you do for local system services. For our example, we are creating systemd file for our Apache container created in Part 7. Let’s say, the name is docker-httpd.service Follow following commands

[root@localhost ~]# vim /etc/systemd/system/docker-httpd.service

Now append the following lines to file docker-httpd.service:

[Unit]
Description=Apache Container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a webserver
ExecStop=/usr/bin/docker stop -t 2 webserver
[Install]
WantedBy=local.target

Save and close using :wq!

Step 3: Check Your Work

After you finish editing the file we need to reload the service/daemon of systemd to reflect the changes we have done.

[root@localhost ~]# systemctl daemon-reload

Now we can do our task on Docker Container using below commands

[root@localhost ~]# systemctl start docker-httpd.service
[root@localhost ~]# systemctl status docker-httpd.service
 docker-httpd.service - Apache Container
Loaded: loaded (/etc/systemd/system/docker-httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2017-07-10 16:01:22 IST; 1s ago
Main PID: 11594 (docker-current)
Memory: 5.7M
CGroup: /system.slice/docker-httpd.service
└─11594 /usr/bin/docker-current start -a webserver

This was just a simple example on what you can do with a simple Docker

You May Like:

Part 1: What Is Docker And Concept Of Containers With Virtualization?
Part 2: How To Install Docker On CentOS/RHEL 6/7? And Learn Docker HUB Registration
Part 3A: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 A
Part 3B: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 B
Part 4: Learn To Install, Run And Delete Applications Inside Docker Containers – Part 4
Part 5: Learn To Push Custom Build Docker Image On Docker HUB (Repository) 
Part 6: What Are Docker File Learns To Use Them? | Simple Demo