Example of how to automatically start an application service in a Docker container

Example of how to automatically start an application service in a Docker container

If you want the application service in the Docker container to start automatically when the container is started. You only need to write the service startup script in the Dockerfile, and then use the Dockerfile to reconstruct the image:

  • Write an application service self-start script
  • Writing a Dockerfile
  • Reconstructing the image
  • Open the container

Write a service self-start script (dockerd)
(Take Tomcat as an example)

#!/bin/bash
#
# 
#
# chkconfig: 345 98 30
# description: tomcat program.
# processname: tomcat
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
#. /etc/sysconfig/network


prog="tomcat"

checkprocess()
{
  chkret=`ps -ef |grep $prog |grep -v "grep" |wc -l`
    return $chkret
}

start()
{
    cd /usr/local/tomcat/bin/
   ./startup.sh
  echo "Service $prog started!"
}

#/usr/sbin/nscd &
/usr/sbin/sshd -D &
while [ true ]
do
        DNSIP=172.18.3.179
          ping=`ping -c 3 $DNSIP|awk 'NR==7 {print $4}'`
        if [[ $ping -eq "3" ]]
        then
            break
        fi
    sleep 3
done

while [ true ]
do
    checkprocess
     if [[ $chkret -eq "0" ]]
        then
              start
     #nscd -i hosts
        else 
          echo "Service $prog is running."

        fi
    sleep 60
done
exit 0

Writing a Dockerfile

FROM tomcat

MAINTAINER liu "[email protected]"

ADD dockerd /etc/rc.d/init.d/
RUN chmod 777 /etc/rc.d/init.d/dockerd

ENV JAVA_HOME /usr/local/jdk1.6.0_30
ENV PATH $JAVA_HOME/bin:$PATH
ENV CLASSPATH .:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

CMD /etc/rc.d/init.d/dockerd

Among them, ADD adds the scripts in the same directory to the specified location of the image, and ENV sets the Java environment variable (very important, the self-starting script cannot be executed successfully without this sentence). There can only be one CMD command, which is used to execute commands when the container starts. It can also be a script.

Reconstructing the image
**pass
docker build -t new image name.
Successfully created a new image

Open the container

docker run -d -h="s" –name="s" new image name

Then, I found that tomcat was already started

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Execute the shell or program inside the Docker container on the host
  • Use Shell scripts to batch start and stop Docker services
  • How to solve the Docker container startup failure
  • Detailed explanation of the solution to the error of using systemctl to start the service in docker
  • Add port mapping after docker container starts
  • Running Flume in a Docker container and starting it without outputting running logs
  • Detailed explanation of Shell script control docker container startup order

<<:  64-bit CentOs7 source code installation mysql-5.6.35 process sharing

>>:  Vue form post request combined with Servlet to realize file upload function

Recommend

Page Refactoring Skills - Content

Enough of small talk <br />Based on the lar...

Use Navicate to connect to MySQL on Alibaba Cloud Server

1. First enter the server's mysql to modify p...

Notes on the MySQL database backup process

Today I looked at some things related to data bac...

How to create a project with WeChat Mini Program using typescript

Create a project Create a project in WeChat Devel...

Mini Program implements custom multi-level single-select and multiple-select

This article shares the specific code for impleme...

Summary of the understanding of virtual DOM in Vue

It is essentially a common js object used to desc...

Ubuntu Basic Tutorial: apt-get Command

Preface The apt-get command is a package manageme...

A brief analysis of React's understanding of state

How to define complex components (class component...

mysql replace part of the field content and mysql replace function replace()

[mysql] replace usage (replace part of the conten...

Vue close browser logout implementation example

Table of contents 1. beforeunload event 2. Unload...

Linux file systems explained: ext4 and beyond

Today I will take you through the history of ext4...

CSS3 custom scroll bar style::webkit-scrollbar sample code detailed explanation

The default scroll bar style in Windows is ugly, ...

A complete explanation of MySQL high availability architecture: MHA architecture

Table of contents 1. Introduction 2. Composition ...

Detailed explanation of sshd service and service management commands under Linux

sshd SSH is the abbreviation of Secure Shell, whi...

Web lesson plans, lesson plans for beginners

Teaching Topics Web page Applicable grade Second ...