DOCKER: Running shell script inside container hangs - docker

I am currently trying to dockerize a codeigniter application cloned from here.
I used a base lamp stack image from docker hub found here
My dockerfile looks like this currently:
FROM firespring/apache2-php
WORKDIR /var/www/spaceship
COPY . .
EXPOSE 8000
CMD ["bin/server.sh"]
But when I try to run it it with:
$ docker run -p 1234:8000 spaceship
it hangs.
I tried verifying if my base image was not compatible with it:
$ docker run --rm -it --entrypoint=/bin/bash spaceship
root#fa09751cd081:/var/www/spaceship# ls
Dockerfile README.md application bin composer.json composer.lock favicon.ico public vendor
root#fa09751cd081:/var/www/spaceship# bin/server.sh
PHP 5.5.9-1ubuntu4.24 Development Server started at Sat Apr 6 02:51:44 2019
Listening on http://127.0.0.1:8000
Document root is /var/www/spaceship/public
Press Ctrl-C to quit.
but apparently it works inside the container locally.
So I guess my question is how do i write the proper dockerfile for this?

Can you try and add "-d" to your docker run command:
docker run -d -p 1234:8000 spaceship

Related

Revel and Docker container

I am attempting to create a docker container that contains the revel skeleton app. Everything seems to build OK and the container is created but when I go to localhost:9000 in my browser nothing comes up.
To make sure my environment is working properly I created a simple hello world go app and created a docker container for it. It worked OK using the same port 9000. This makes me think that there is something not configured properly in my dockerfile.
Dockerfile:
#Compile stage
FROM golang:1.11.4-alpine3.8 AS build-env
ENV CGO_ENABLED 0
RUN apk add --no-cache git
ADD . /go/src/revelapp
# Install revel framework
RUN go get -u github.com/revel/revel
RUN go get -u github.com/revel/cmd/revel
#build revel app
RUN revel build revelapp app dev
# Final stage
FROM alpine:3.8
EXPOSE 9000
WORKDIR /
COPY --from=build-env /go/app /
ENTRYPOINT /run.sh
Docker command used:
docker build -t revelapp . && docker run -p 9000:9000 --name revelapp revelapp
After command is executed and container is created the console shows:
INFO 17:25:01 app run.go:32: Running revel server
INFO 17:25:01 app plugin.go:9: Go to /#tests to run the tests.
Revel engine is listening on.. localhost:9000
When I go to localhost:9000 I would expect to see the text It Works!
You're listening on localhost:9000, so 127.0.0.1 points to your container and not your local machine.
You have two solutions to make it work:
Listen on 0.0.0.0:9000
Use --network="host" in your docker run command: 127.0.0.1 in your docker container will point to your docker host.

docker file for tomcat

I wrote one docker file for tomcat installation, the file is
FROM openjdk:8-jre-alpine
RUN wget http://mirrors.fibergrid.in/apache/tomcat/tomcat-8/v8.5.34/bin/apache-tomcat-8.5.34.tar.gz
RUN gzip apache-tomcat-8.5.34.tar.gz
ADD https://github.com/spagop/quickstart/raw/master/management-api-examples/mgmt-deploy-application/application/jboss-as-helloworld.war /apache-tomcat-8.5.34/*/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]
And I built the above docker file by using
$ docker build -t tomacat -f docker file name .
And created the container by using above docker image, command is
$ docker run --name=tom1 -d -it -p 9090:8080 tomcat
After running the docker run container is up and running
But tomcat server is not up and running in background
I replaced the catalina.sh with startup.sh in CMD area and also i getting same problem
Please help me for resolving problem
As mentioned in the question, I did everything as stated and started a container. After that I accessed the tomcat through HOST_IP:HOST_PORT and I was able to access the Tomcat. The issue may be that you are not accessing the Tomcat correctly like HOST_IP is not correct.

Docker: Why does my home directory disappear after the build?

I have a simple docker file:
FROM ubuntu:16.04
MAINTAINER T-vK
RUN useradd -m -s /bin/bash -g dialout esp
USER esp
WORKDIR /home/esp
COPY ./entrypoint_script.sh ./entrypoint_script.sh
ENTRYPOINT ["/home/esp/entrypoint_script.sh"]
when I run docker build . followed by docker run -t -i ubuntu and look for the directory /home/esp it is not there! The whole directory including it's files seem to be gone.
Though, when I add RUN mkdir /home/esp to my docker file, it won't build telling me mkdir: cannot create directory '/home/esp': File exists.
So what am I misunderstanding here?
I tested this on Debian 8 x64 and Ubuntu 16.04 x64.
With Docker version 1.12.2
Simply change you Docker build command to:
docker build -t my-docker:dev .
And then to execute:
docker run -it my-docker:dev
Then you'll get what you want. you didn't tag docker build so you're actually running Ubuntu image.

Docker can't find OpenJDK

I am experimenting with Docker for the first time, and am trying to get a Spring Boot web app to run inside a Docker container. I am building the app (which packages up into a self-contained jar) and then adding it to the Docker image (which is what I want).
I am following the instructions from the OpenJDK Docker base image here. You can find my SSCCE at this Bootup repo on GitHub, whose README has all the instructions to reproduce what I'm seeing. But basically:
I build the web app into a jar
Run docker build -t bootup . which succeeds
Run docker run -it --rm --name bootup bootup which gives me the error below and then exits
The error:
/bin/sh: 1: /bin/sh: [java,: not found
According to the Google Gods, this used to be a problem with the Oracle JDK images, but should not be happening with OpenJDK images.
Looking at my Dockerfile (which is also up in that GitHub repo), can anyone spot where I'm going awry:
FROM openjdk:8
RUN mkdir /opt/bootup
ADD build/libs/bootup.jar /opt/bootup
WORKDIR /opt/bootup
ENTRYPOINT ['java', '-jar', 'bootup.jar']
CMD ['']
Thanks in advance!
Update:
Output of docker ps:
CONTAINER ID IMAGE COMMAND CREATED
16bde964ff6b bootup "/bin/sh -c 'java -ja" 2 days ago
STATUS PORTS NAMES
Up 14 seconds 0.0.0.0:8080->8080/tcp bootup
I had it working fine using this dockerfile:
FROM openjdk:8
RUN mkdir /opt/bootup
ADD build/libs/bootup.jar /opt/bootup
WORKDIR /opt/bootup
EXPOSE 8080
ENTRYPOINT java -jar bootup.jar
It runs just fine with this commad:
docker run -it -p 8080:8080 --name bootup bootup
I am no java developer and I don't know why it ignores your configuration that requires it to start on port 9200, since your app starts on port 8080, but from a docker point of view everything is working with my config and I can connect to the app from my browser on localhost:8080
Here the screenshot:
Also, since you posted your github repo I suggest you to modify the readme so that users can start gradle from docker without the need of a java environment in the host machine running this one time command:
docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp openjdk:8 /usr/src/myapp/gradlew clean build

Docker add warfile to official Tomcat image

I pulled official Docker image for Tomcat by running this command.
docker run -it --rm tomcat:8.0
By using this as base image I need to build new image that contains my war file in the tomcat webapps folder. I created Dockerfile like this.
From tomcat8
ADD warfile /usr/local/tomcat
When I run this Dockerfile by building image I am not able to see Tomcat front page.
Can anybody tell me how to add my warfile to official Tomcat images webapp folder.
Reading from the documentation of the repo you would do something like that
FROM tomcat
MAINTAINER xyz
ADD your.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]
Then build your image with docker build -t yourName <path-to-dockerfile>
And run it with:
docker run --rm -it -p 8080:8080 yourName
--rm removes the container as soon as you stop it
-p forwards the port to your host (or if you use boot2docker to this IP)
-it allows interactive mode, so you see if something get's deployed
Building on #daniel's answer, if you want to deploy your WAR to the root of tomcat, I did this:
FROM tomcat:7-jre7
MAINTAINER xyz
RUN ["rm", "-fr", "/usr/local/tomcat/webapps/ROOT"]
COPY ./target/your-webapp-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]
It deletes the existing root webapp, copies your WAR to the ROOT.war filename then executes tomcat.
docker run -it --rm --name MYTOMCAT -p 8080:8080 -v .../wars:/usr/local/tomcat/webapps/ tomcat:8.0
where wars folder contains war to deploy
How do you check the webapps folder?
The webapps folder is within the docker container.
If you want to access your webapps container you could mount a host directory within your container to use it as webapps folder. That way you can access files without accessing docker.
Details see here
To access your logs you could do that when you run your container e.g.
docker run -rm -it -p 8080:8080 **IMAGE_NAME** /path/to/tomcat/bin/catalina.sh && tail -f /path/to/tomcat/logs
or you start your docker container and then do something like:
docker exec -it **CONTAINER_ID** tail -f /path/to/tomcat/logs
If you are using spring mvc project then you require server to run your application suppose you use tomcat then you need base image of tomcat that your application uses which you can specify through FROM command.
You can set environment variable using ENV command.
You can additionally use RUN command which executes during Docker Image buiding.
eg to give read write execute permissions to webapps folder for tomcat to unzip war file
RUN chmod -R 777 $CATALINA_HOME/webapps
And one more command is CMD. Whatever you specifying in CMD command it will execute at a time of container running. You can specify options in CMD command using double quotes(" ") seperated by comma(,).
eg
CMD ["catalina.sh","start"]
(NOTE : Remember RUN command execute at a time of image building and CMD execute at a time of running container this is confusing for new users).
This is my Dockerfile -
FROM tomcat:9.0.27-jdk8-openjdk
VOLUME /tmp
RUN chmod -R 777 $CATALINA_HOME/webapps
ENV CATALINA_HOME /usr/local/tomcat
COPY target/*.war $CATALINA_HOME/webapps/myapp.war
EXPOSE 8080
CMD ["catalina.sh","run"]
Build your image using command
docker build -t imageName <path_of_Dockerfile>
check your docker image using command
docker images
Run image using command
docker run -p 9999:8080 imageName
here 8080 is tomcat port and application can access on 9999 port
Try accessing your application on
localhost:9999/myapp/

Resources