I am building Scigraph database on my local machine and trying to move this entire folder to docker and run it, when I run the shell script on my local machine it runs without error when I add the same folder inside docker and try to run it fails
Am I doing this right way, here's my DOckerfile
FROM goyalzz/ubuntu-java-8-maven-docker-image
ADD ./SciGraph /usr/share/SciGraph
WORKDIR /usr/share/SciGraph/SciGraph-services
RUN pwd
EXPOSE 9000
CMD ['./run.sh']
when I try to run it I'm getting this error
docker run -p9005:9000 test
/bin/sh: 1: [./run.sh]: not found
if I run it using below command it works
docker run -p9005:9000 test -c "cd /usr/share/SciGraph/SciGraph-services && sh run.sh"
as I already marked the directory as WORKDIR and running the script inside docker using CMD it throws error
For scigraph as provided in their ReadMe, you can to run mvn install before you run their services. You can set your shell to bash and use a docker compose to run the docker image as shown below
Dockerfile
FROM goyalzz/ubuntu-java-8-maven-docker-image
ADD ./SciGraph /usr/share/SciGraph
SHELL ["/bin/bash", "-c"]
WORKDIR /usr/share/SciGraph
RUN mvn -DskipTests -DskipITs -Dlicense.skip=true install
RUN cd /usr/share/SciGraph/SciGraph-services && chmod a+x run.sh
EXPOSE 9000
build the scigraph docker image by running
docker build . -t scigraph_test
docker-compose.yml
version: '2'
services:
scigraph-server:
image: scigraph_test
working_dir: /usr/share/SciGraph/SciGraph-services
command: bash run.sh
ports:
- 9000:9000
give / after SciGraph-services and change it to "sh run.sh" ................ and look into run.sh file permissions also
It is likely that your run.sh doesn't have the #!/bin/bash header, so it cannot be executed only by running ./run.sh. Nevertheless, always prefer to run scripts as /bin/bash foo.sh or /bin/sh foo.sh when in docker, especially because you don't know what changes files have been sourced in images downloaded from public repositories.
So, your CMD statement would be:
CMD /bin/bash -c "/bin/bash run.sh"
You have to add the shell and the executable to the CMD array ...
CMD ["/bin/sh", "./run.sh"]
Related
Problem Description
I have a docker image which I build and run using docker-compose. Normally I develop on WSL2, and when running docker-compose up --build the image builds and runs successfully. On another machine, using Windows powershell, with an identical clone of the code, executing the same command successfully builds the image, but gives an error when running.
Error
[+] Running 1/1
- Container fastapi-service Created 0.0s
Attaching to fastapi-service
fastapi-service | exec /start_reload.sh: no such file or directory
fastapi-service exited with code 1
I'm fairly experienced using Docker, but am a complete novice with PowerShell and developing on Windows more generally. Is there a difference in Dockerfile construction in this context, or a difference in the execution of COPY and RUN statements?
Code snippets
Included are all parts of the code required to replicate the error.
Dockerfile
FROM tiangolo/uvicorn-gunicorn:python3.7
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY ./start.sh /start.sh
RUN chmod +x /start.sh
COPY ./start_reload.sh /start_reload.sh
RUN chmod +x /start_reload.sh
COPY ./data /data
COPY ./app /app
EXPOSE 8000
CMD ["/start.sh"]
docker-compose.yml
services:
web:
build: .
container_name: "fastapi-service"
ports:
- "8000:8000"
volumes:
- ./app:/app
command: /start_reload.sh
start-reload.sh
This is a small shell script which runs a prestart.sh if present, and then launches gunicorn/uvicorn in "reload mode":
#!/bin/sh
# If there's a prestart.sh script in the /app directory, run it before starting
PRE_START_PATH=/app/prestart.sh
HOST=${HOST:-0.0.0.0}
PORT=${PORT:-8000}
LOG_LEVEL=${LOG_LEVEL:-info}
echo "Checking for script in $PRE_START_PATH"
if [ -f $PRE_START_PATH ] ; then
echo "Running script $PRE_START_PATH"
. "$PRE_START_PATH"
else
echo "There is no script $PRE_START_PATH"
fi
# Start Uvicorn with live reload
exec uvicorn --host $HOST --port $PORT --log-level $LOG_LEVEL main:app --reload
The solution lies in a difference between UNIX and Windows systems, and the way they end lines. A discussion on the topic can be found [here].
(Difference between CR LF, LF and CR line break types?)
The presence/absence of these characters in the file, and configuration of the shell running the command leads to an error where the file being run is the Dockerfile start-reload.sh(CR-LF) but the file that exists is simply start-reload.sh, hence the no such file or directory error raised.
As it stands, my Dockerfile works as written below, but currently I have to run the two commented lines in order to pull, compile, and deploy my application to the server. I tried creating a shell script to run those commands using ADD and ENTRYPOINT, but when I run (using the docker commands below) the shell script runs and then the container exits.
What/How do I modify (I'm assuming, the docker run command) to fix this?
Is there an easier way to import libraries than the multiple URLS for RPM? I tried using YUM, but I wasn't sure how to set up my repo for installing anything.
Dockerfile
FROM registry.access.redhat.com/jboss-eap-7/eap71-openshift
USER root
RUN rpm -i [the URLS of the 40 libraries I need for SVN]
ADD subversion_installer_1.14.1.sh /home/svn_installer.sh
RUN yes | /home/svn_installer.sh
USER jboss
ARG REPO_USER
ARG REPO_PW
ARG REPO_URL
ENV REPO_USER=$REPO_USER
ENV REPO_PW=$REPO_PW
ENV REPO_URL=$REPO_URL
#RUN svn export --username="$REPO_USER" --password="$REPO_PW" "$REPO_URL" /usr/svn/myapp
#RUN /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/bin/jar -cvf $JBOSS_HOME/standalone/deployments/myapp.war /usr/svn/myapp
Docker commands
docker build . -t myapp:latest
docker run -d -p 8080:8080 -p 9990:9990 --env-file=svnvars.cfg myapp:latest
Found out what I was doing wrong. I was trying to use
/opt/eap/bin/standalone.sh
as the last command in my entrypoint script.
I discovered this was wrong by calling
docker images inspect myapp:latest
where I found
"Cmd": [
"/opt/eap/bin/openshift-launch.sh"
],
I was calling the wrong command. So I fixed this by replacing the command in my shell script and changing my ENTRYPOINT to CMD.
Here are the corrected files:
Dockerfile
FROM registry.access.redhat.com/jboss-eap-7/eap71-openshift
USER root
RUN rpm -i [too many libraries]
ADD subversion_installer_1.14.1.sh /home/svn_installer.sh
ADD svnvars.cfg /var/svn/svnvars.cfg
RUN yes | /home/svn_installer.sh
USER jboss
ARG REPO_USER
ARG REPO_PW
ARG REPO_URL
ENV REPO_USER=$REPO_USER
ENV REPO_PW=$REPO_PW
ENV REPO_URL=$REPO_URL
ADD entrypoint.sh /home/entrypoint.sh
CMD /home/entrypoint.sh
entrypoint.sh
#!/bin/bash
svn export --username="$REPO_USER" --password="$REPO_PW" "$REPO_URL" /usr/svn/myapp
cd /usr/svn/myapp
ant war
/opt/eap/bin/openshift-launch.sh
In this Dockerfile i have an ENTRYPOINT that calls a script that simply logs an echo "testing". This output works locally when I build and run the Dockerfile. It also logs to cloudwatch when I use in conjunction with a docker-compose for aws.
However the RUN and CMD commands do not output anything to the console or cloudwatch, how do i see their output? I would expect at least some errors
ENTRYPOINT bash -c "/migrate.sh"
WORKDIR /
RUN yarn
CMD ["yarn migration:run", "dist/src/main"]
I'm building just with docker build -t test:test . then docker run <imagename>
The RUN statement in the Dockerfile is only invoked when you build the container (at which point you should see the output of yarn in this case). When you docker run the container it will just execute the ENTRYPOINT and/or CMD (in this case the output of the ENTRYPOINT as there is no CMD)
I have a docker-compose file with a service called 'app'. When I try to run my docker file I don't see the service with docker ps but I do with docker ps -a.
I looked at the logs:
docker logs my_app_1
python: can't open file '//apps/index.py': [Errno 2] No such file or directory
In order to debug I wanted to be able to see the home directory and the files and dirs contained there when the app attempts to run.
Is there a command I can add to docker-compose that would show me the pwd and ls -l of the container when it attempts to run index.py?
My Dockerfile:
FROM python:3
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "apps/index.py"]
My docker-compose.yaml:
version: '3.1'
services:
app:
build:
context: ./app
dockerfile: ./Dockerfile
depends_on:
- db
ports:
- 8050:8050
My directory structure:
my_app:
* docker-compose.yaml
* app
* Dockerfile
* apps
* index.py
You can add a RUN statement in the application Dockerfile to run these commands.
Example:
FROM python:3
COPY . .
RUN pip install -r requirements.txt
# Run your commands
RUN pwd && ls -l
CMD ["python", "apps/index.py"]
Then you chan check the logs of the build process and view the results.
I hope this answer helps you.
If you're just trying to debug an image you've already built, you can docker-compose run an alternate command:
docker-compose run apps \
ls -l ./apps
You don't need to modify anything in your Dockerfile to be able to do this (assuming it uses CMD correctly; see below).
If you need to do more intensive debugging, you can docker-compose run apps sh (or, if your image has it, bash) to get an interactive shell. The container will include any mounted volumes and be on the same Docker network as the named container, but won't have published ports.
Note that the command here replaces the CMD in the Dockerfile. If your image uses ENTRYPOINT for its main command, or if it has a complete command split between ENTRYPOINT and CMD (especially, if you have ENTRYPOINT ["python"]), these need to be combined into a single CMD for this to work. If your ENTRYPOINT is a wrapper script that does some first-time setup and then runs the CMD, this approach will work fine; the debugging ls or sh will run after the first-time setup happens.
I made a webapp in Java and I would like to test the site locally using Docker.
The war file I created works perfectly but to be read correctly it must be inserted inside this path:
/tomcat/webapps/ROOT/
For these reasons I decided to use this form:
https://hub.docker.com/_/tomcat
In particular I decided to use this Dockerfile:
https://github.com/docker-library/tomcat/blob/ec2d88f0a3b34292c1693e90bdf786e2545a157e/9.0/jre11-slim/Dockerfile
I added this code towards the end:
...
EXPOSE 8080
CMD ["cd /usr/local/tomcat/webapps/"]
CMD ["mv ROOT ROOT.old"]
CMD ["mkdir ROOT"]
COPY ./esercitazione.1.maven/ /usr/local/tomcat/webapps/ROOT/
CMD ["catalina.sh", "run"]
I used this code at the Windows 10 prompt:
D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker build -t tomcat-9-java-11:v2.0 .
docker run -it --rm --name tomcat-9-java-11-container -p 8888:8080 tomcat-9-java-11:v2.0
When I enter this link on the browser:
http://192.168.99.103:8888/
I see this:
https://prnt.sc/n2vti1
I'm a beginner with both Docker and Tomcat and I need a little help.
Inside this path I put my unzipped .war file:
D:\DATI\Docker-Tomcat-Win10\esercitazione.1.maven
Thank you
%%%%%%%%%%%%%%%%%%%%%%%%%%
#Shree Tiwari
%%%%%%%%%%%%%%%%%%%%%%%%%%
First of all thank you for your help!
I deleted all the containers and all the images on Docker and used your Dockerfile (I only changed the name of the folder containing the .war files to be tested).
FROM tomcat:9-jre8
ENV JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:MaxMetaspaceSize=128m"
WORKDIR /usr/local/tomcat/webapps/
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./webapps/*.war /usr/local/tomcat/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]
I placed the .war file at this address:
D:\DATI\Docker-Tomcat-Win10\webapps\esercitazione.1.maven.war
I opened the Windows prompt and I typed:
D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker build -t tomcat:v1.0 .
docker run -it --rm --name tomcat-container -p 8888:8080 tomcat:v1.0
I entered this URL in the browser:
http://192.168.99.103:8888/esercitazione.1.maven/
then this other:
http://192.168.99.103:8888/
Unfortunately none of them went well.
The only mistake I encountered when creating the image is this:
"SECURITY WARNING: You are building to Docker image from Windows against a non-Windows Docker host. Files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories."
%%%%%%%%%%%%%%%%%%%%%%%%%%
#Miq
%%%%%%%%%%%%%%%%%%%%%%%%%%
First of all thank you for your help!
I also tested your code but it doesn't work.
Dockerfile:
FROM tomcat:9-jre11-slim
RUN mv webapps/ROOT webapps/ROOT.old && mkdir webapps/ROOT
COPY ./esercitazione.1.maven/ webapps/ROOT/
Code:
D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker build -t tomcat:v2.0 .
docker run -it --rm --name tomcat-container tomcat:v2.0
Browser:
http://192.168.99.103:8080/
Other tests:
FROM tomcat:9-jre11-slim
ENV JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:MaxMetaspaceSize=128m"
RUN mv webapps/ROOT webapps/ROOT.old && mkdir webapps/ROOT
WORKDIR /usr/local/tomcat/webapps/
RUN rm -rf /usr/local/tomcat/webapps/ROOT/*
COPY ./webapps/*.war /usr/local/tomcat/webapps/ROOT/
EXPOSE 8080
CMD ["catalina.sh", "run"]
docker ps -a
docker images
docker stop tomcat-container
docker rmi tomcat:v3.0
D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker build -t tomcat:v3.0 .
docker run -d --name tomcat-container -p 8888:8080 tomcat:v3.0
http://192.168.99.103:8888/
%%%%%%%%%%%%%%%%%%%%%%%%%%
Other tests: (8 April 2019)
FROM tomcat:9.0.17-jre11-slim
LABEL Author="Nome Cognome"
EXPOSE 8080
RUN rm -fr /usr/local/tomcat/webapps/ROOT
COPY ./esercitazione.1.maven.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]
>
docker build -t tomcat-eb:v.9.0.17 .
docker run -it --rm -p 8888:8080 tomcat-eb:v.9.0.17
>
I'm going here:
http://192.168.99.103:8888
and the browser sends me here:
https://192.168.99.103:8443
%%%%%%%%%%%%%%%%%%%%%%%%%%
Other tests: (I choose another image)
FROM tomee:8-jre-8.0.0-M2-webprofile
LABEL Author="Nome Cognome"
EXPOSE 8080
RUN rm -fr /usr/local/tomcat/webapps/ROOT
COPY ./esercitazione.1.maven.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]
>
docker build -t tomcat-eb:v.9.0.17 .
docker run -it --rm -p 8888:8080 tomcat-eb:v.9.0.17
>
If I go here:
http://192.168.99.103:8888
I see Tomcat home, non my webapp.
Is this a problem without a solution?
The biggest problem I see here is that you use CMD instead RUN in your dockerfile. CMD is to define a command that will be run when container is ran. With the Dockerfile you have now, only the last one is executed when you start your container and all of those mkdirs, moves, etc. are never executed. As said, you need to use RUN keywords to indicate command that build process should execute and commit as image layer. You probably also do not need the catalina.sh CMD as it might come from the image you base on, but you need to check it on doc page for the base image or take a peek to it's Dockerfile or use docker history imagename To see the layers and commands used to create them.
I took a deeper look into this and the base image you use. In addition to the CMD's your problem is that you change the workdir by running cd commands. catalina.sh exists in $CATALINA_HOME dir, which then is marked as workdir in base image. When you change the active directory by executing cd it breaks the image runtime.
I'd suggest you try with following dockerfile:
FROM tomcat:9-jre11-slim
RUN mv webapps/ROOT webapps/ROOT.old && mkdir webapps/ROOT
COPY ./esercitazione.1.maven/ webapps/ROOT/
No need to use EXPOSE and CMD as they are defined in base image. also, base image defines WORKDIR to $CATALINA_HOME and that's where you will be when executing any following commands (treat WORKDIR as cd but in docker style).
Hope that helps.
First you need to compile your Code to a war file then you can use below Dockerfile
FROM tomcat:9-jre8
ENV JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:MaxMetaspaceSize=128m"
WORKDIR /usr/local/tomcat/webapps/
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./esercitazione.1.maven/*.war /usr/local/tomcat/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]