skip to Main Content

Now I want to make a docker command run in frontend so that I could see the log output. Now I am using this command to run my docker container:

docker run -p 11110:11110 -p 11111:11111 -p 11112:11112 --name canal-server dolphinjiang/canal-server:v1.1.5

this is the Dockerfile of my project:

FROM centos:7

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo ZONE="Asia/Shanghai" > /etc/sysconfig/clock


RUN rm -rf /etc/yum.repos.d/*.repo 
COPY CentOS6-Base-163.repo /etc/yum.repos.d/
RUN yum clean all



RUN groupadd -g 2500 canal; useradd -u 2501 -g canal -d /home/canal -m canal
RUN echo canal:De@2018er | chpasswd; echo root:dockerroot | chpasswd


RUN yum -y update && yum -y install wget vi openssl.x86_64 glibc.x86_64 tar tar.x86_64 inetutils-ping net-tools telnet which file
RUN yum clean all


COPY jdk-8u291-linux-x64.tar.gz /opt
RUN tar -zvxf /opt/jdk-8u291-linux-x64.tar.gz -C /opt && 
        rm -rf /opt/jdk-8u291-linux-x64.tar.gz && 
                        chmod -R 755 /opt/jdk1.8.0_291 && 
                                        chown -R root:root /opt/jdk1.8.0_291
RUN echo 'export JAVA_HOME=/opt/jdk1.8.0_291' >> /etc/profile                       
RUN echo 'export JRE_HOME=$JAVA_HOME/jre' >> /etc/profile
RUN echo 'export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH' >> /etc/profile
RUN echo 'export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH' >> /etc/profile
RUN source /etc/profile


RUN yum install kde-l10n-Chinese -y
RUN yum install glibc-common -y
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
ENV JAVA_HOME /opt/jdk1.8.0_291
ENV PATH $PATH:$JAVA_HOME/bin
ENV LANG zh_CN.UTF-8
ENV LC_ALL zh_CN.UTF-8


ADD canal-server /home/canal/
RUN chmod 755 /home/canal/bin


WORKDIR /home/canal/bin
RUN chmod 777 /home/canal/bin/restart.sh
RUN chmod 777 /home/canal/bin/startup.sh
RUN chmod 777 /home/canal/bin/stop.sh
RUN chmod 777 /home/canal/bin/config.sh
CMD  /home/canal/bin/config.sh

this is the config.sh:

cat > /home/canal/conf/canal.properties <<- EOF
# register ip
canal.register.ip = ${HOSTNAME}.canal-server-discovery-svc-stable.testcanal.svc.cluster.local

# canal admin config
canal.admin.manager = canal-admin-stable:8089
canal.admin.port = 11110
canal.admin.user = admin
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
# admin auto register
canal.admin.register.auto = true
canal.admin.register.cluster = 
EOF
sh /home/canal/bin/restart.sh

and this is the restart.sh:

#!/bin/bash

args=$@

case $(uname) in
Linux)
  bin_abs_path=$(readlink -f $(dirname $0))
  ;;
*)
  bin_abs_path=$(cd $(dirname $0) ||exit ; pwd)
  ;;
esac

sh "$bin_abs_path"/stop.sh $args
sh "$bin_abs_path"/startup.sh $args

and this is the start.sh:

#!/bin/bash 

current_path=`pwd`
case "`uname`" in
    Linux)
        bin_abs_path=$(readlink -f $(dirname $0))
        ;;
    *)
        bin_abs_path=`cd $(dirname $0); pwd`
        ;;
esac
base=${bin_abs_path}/..
canal_conf=$base/conf/canal.properties
canal_local_conf=$base/conf/canal_local.properties
logback_configurationFile=$base/conf/logback.xml
export LANG=en_US.UTF-8
export BASE=$base

if [ -f $base/bin/canal.pid ] ; then
    echo "found canal.pid , Please run stop.sh first ,then startup.sh" 2>&2
    exit 1
fi

if [ ! -d $base/logs/canal ] ; then 
    mkdir -p $base/logs/canal
fi

## set java path
if [ -z "$JAVA" ] ; then
  JAVA=$(which java)
fi

ALIBABA_JAVA="/usr/alibaba/java/bin/java"
TAOBAO_JAVA="/opt/taobao/java/bin/java"
if [ -z "$JAVA" ]; then
  if [ -f $ALIBABA_JAVA ] ; then
    JAVA=$ALIBABA_JAVA
  elif [ -f $TAOBAO_JAVA ] ; then
    JAVA=$TAOBAO_JAVA
  else
    echo "Cannot find a Java JDK. Please set either set JAVA or put java (>=1.5) in your PATH." 2>&2
    exit 1
  fi
fi

case "$#" 
in
0 ) 
    ;;
1 ) 
    var=$*
    if [ "$var" = "local" ]; then
        canal_conf=$canal_local_conf
    else
        if [ -f $var ] ; then 
            canal_conf=$var
        else
            echo "THE PARAMETER IS NOT CORRECT.PLEASE CHECK AGAIN."
            exit
        fi
    fi;;
2 ) 
    var=$1
    if [ "$var" = "local" ]; then
        canal_conf=$canal_local_conf
    else
        if [ -f $var ] ; then
            canal_conf=$var
        else 
            if [ "$1" = "debug" ]; then
                DEBUG_PORT=$2
                DEBUG_SUSPEND="n"
                JAVA_DEBUG_OPT="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=$DEBUG_PORT,server=y,suspend=$DEBUG_SUSPEND"
            fi
        fi
     fi;;
* )
    echo "THE PARAMETERS MUST BE TWO OR LESS.PLEASE CHECK AGAIN."
    exit;;
esac

str=`file -L $JAVA | grep 64-bit`
if [ -n "$str" ]; then
    JAVA_OPTS="-server -Xms2048m -Xmx3072m -Xmn1024m -XX:SurvivorRatio=2 -XX:PermSize=96m -XX:MaxPermSize=256m -Xss256k -XX:-UseAdaptiveSizePolicy -XX:MaxTenuringThreshold=15 -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError"
else
    JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:MaxPermSize=128m "
fi

JAVA_OPTS=" $JAVA_OPTS -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8"
CANAL_OPTS="-DappName=otter-canal -Dlogback.configurationFile=$logback_configurationFile -Dcanal.conf=$canal_conf"

if [ -e $canal_conf -a -e $logback_configurationFile ]
then 
    
    for i in $base/lib/*;
        do CLASSPATH=$i:"$CLASSPATH";
    done
    CLASSPATH="$base/conf:$CLASSPATH";
    
    echo "cd to $bin_abs_path for workaround relative path"
    cd $bin_abs_path
    
    echo LOG CONFIGURATION : $logback_configurationFile
    echo canal conf : $canal_conf 
    echo CLASSPATH :$CLASSPATH
    $JAVA $JAVA_OPTS $JAVA_DEBUG_OPT $CANAL_OPTS -classpath .:$CLASSPATH com.alibaba.otter.canal.deployer.CanalLauncher 2>&1 
    echo $! > $base/bin/canal.pid 
    
    echo "cd to $current_path for continue"
    cd $current_path
else 
    echo "canal conf("$canal_conf") OR log configration file($logback_configurationFile) is not exist,please create then first!"
fi

after I start the docker, it exit automaticlly, and the docker not startup, no log output. what should I do to make it run in frontend. after successs, switch to the backend. I also tried to run in deamon like this(make the container run background and not exit):

docker run -it -d -p 11110:11110 -p 11111:11111 -p 11112:11112 --name canal-server canal/canal-server:v1.1.5

the process still exit automaticlly. and docker container did not startup.

2

Answers


  1. Basically, you should get the point (based on your latest comment).
    Docker is based on some command, when it’s done – it stops the container.

    So to make it continuously running you should have command and run infinitely.

    Also check this answer as well, there are more explanation
    Why docker exiting with code 0

    One of the easiest solution is to tail some logs.
    Like,

    tail -f /dev/null
    

    Taken from here

    Login or Signup to reply.
  2. you can use tail -f /dev/null to keep the container from stopping, try this

    docker run -it -d -p 11110:11110 -p 11111:11111 -p 11112:11112 --name canal-server canal/canal-server:v1.1.5 tail -f /dev/null
    

    see also this post

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search