无法使用ENTRYPOINT Docker运行script.sh
我有以下脚本:
echo "** Creating data volume." docker volume create --name sData echo "** Build the java image that will be used to compile a Java class." docker build -t bh/java:1.0 ./Java echo "** Run the Java container to compile a Java class." docker run --name "java-compile" -v sData:/usr/src/data bh/java:1.0
在Java文件夹里面,我有下面的Dockerfile:
FROM openjdk:7 AS build-env # Copy java file to compile and run COPY ./Main.java /usr/src/myapp/Main.java WORKDIR /usr/src/myapp RUN ls -al # Copy script.sh to root folder COPY ./script.sh /usr/src/data/script.sh RUN chmod 777 /usr/src/data/script.sh ENTRYPOINT ["sh", "/usr/src/data/script.sh"]
和script.sh文件:
#!bin/sh # Go to myapp cd /usr/src/myapp #echo "Listing all files in directory - myapp" ls -al #echo "Compiling the java class" javac Main.java # Move the .class file echo "Moving Main.class to /usr/data/Main.class" mv /usr/src/myapp/Main.class /usr/src/data/Main.class cd /usr/src/data #echo "Listing all files in directory - data" ls -al
当我如上所述运行容器时,出现以下错误:
: not foundta/script.sh: 2: /usr/src/data/script.sh: 's: invalid option -- ' Try 'ls --help' for more information. : not foundta/script.sh: 4: /usr/src/data/script.sh: : not foundta/script.sh: 5: /usr/src/data/script.sh: : not foundta/script.sh: 8: /usr/src/data/script.sh: : not foundta/script.sh: 11: /usr/src/data/script.sh: : not foundta/script.sh: 14: /usr/src/data/script.sh: : not foundta/script.sh: 18: /usr/src/data/script.sh: : not foundta/script.sh: 20: /usr/src/data/script.sh:
另一个问题可能是在COPY命令之前放置“WORKDIR”。 因为当前目录改为/ usr / src / myapp并复制search文件./如果你的dockerfile和脚本在同一个文件夹中,你可以简单的称之为“COPY script.sh / usr / src / data”
我能够使脚本运行如下:
/script.unix.sh
#!/bin/bash echo "** Creating data volume." docker volume create --name sharkData echo "** Build the java image that will be used to compile a Java class." docker build -t bhaidar/java:1.0 ./Java echo "** Run the Java container to compile a Java class." docker run --name "java-compile" -v sharkData:/usr/src/data bhaidar/java:1.0 echo "** Build a docker-compose." docker-compose -f "docker-compose.yml" build echo "** Run a docker-compose." docker-compose -f "docker-compose.yml" up -d
/Java/Dockerfile
FROM openjdk:7 AS build-env WORKDIR /usr/src/myapp # Copy java file to compile and run COPY Main.java Main.java RUN ls -al RUN javac Main.java # I am creating the directory which is the same as the -v mapped with docker run because at this time the volume didn't create a directory inside Container RUN mkdir /usr/src/data RUN mv /usr/src/myapp/Main.class /usr/src/data/Main.class
/Tomcat7/Dockerfile
FROM tomcat:7.0.82-jre7 # List the files in shark-common WORKDIR /usr/local/tomcat/sharkCommon RUN ls -al # Run the Main.class file RUN java Main.class RUN apt-get update && apt-get install -y vim ADD tomcat-users.xml /usr/local/tomcat/conf EXPOSE 8080 RUN chmod +x /usr/local/tomcat/bin/catalina.sh ENTRYPOINT ["catalina.sh", "run"]
/docker-compose.yml
version: '3' services: tomcat: container_name: tomcat7 build: context: ./Tomcat7 dockerfile: Dockerfile image: bhaidar/tomcat:1.0 volumes: - sharkData:/usr/local/tomcat/sharkCommon - ./Tomcat7/tomcat-webapp/index.html:/usr/local/tomcat/webapps/index.html ports: - "8888:8080" tty: true volumes: sharkData: external: true
除了/ Tomcat7 / Docerfile中的以下行之外,它们都运行良好:
# List the files in shark-common WORKDIR /usr/local/tomcat/sharkCommon RUN ls -al
运行Dockerfile时, Main.class
文件不会显示在sharkCommon目录中(RUN ls -al inside / Tomcat7 / Dockerfile)。 我相信这个文件是在上面的Java
容器中生成的,因为一旦容器tomcat7被创build,如果我检查sharkCommon文件夹,我可以看到Main.class文件。
任何想法? 看来外部卷sharkData不被共享。