run 2 build commands in a single docker run command - maven

I have to run maven and angular build inside a docker container with sh command as beolw:
docker run -v maven command and then ng build.
How to run/concatenate both commands in single docker run command?

The solution to this is :
sh """
docker run -v ....\\
"maven command" \\
&& cd directory && ng build'
"""
That worked for me.

In order to run multiple commands in docker, use /bin/bash -c with a semicolon ;
In this case, the second command ng build will be executed only if the first command (cd) returns no error or exit status. To avoid this use && instead of ; (semi-colon)
docker run image /bin/bash -c "cd directory && ng build"

Related

Pass a bash script command-line option from a Dockerfile

I have a bash script that is run as part of a Dockerfile build process.
my_script.sh
poetry install
Dockerfile
RUN ./my_script.sh
What I would like to do is optionally invoke poetry install -vvv when I build the Docker image.
I tried something like:
Dockerfile
ARG POETRY_OPTION="-vvv"
RUN ./my_script.sh ${POETRY_OPTION}
and my_script.sh
poetry install $1
But that isn't being honored. It runs as if that option is not there.
It seems to me that maybe that works for regular arguments, but won't work for "options".
You can have the ARG inside of the Dockerfile as ARG POETRY_OPTION. While running the docker build command use docker build --build-arg POETRY_OPTION="vvv" .

Bash script has no access to PATH when run from Gradle task in IntelliJ IDEA

I have a gradle task which runs a bash script which in turn cleans up some docker containers, it's part of a pipeline of tasks.
If I run this task from the command line, it works fine; if I run it from within IntelliJ IDEA, it fails because I cannot find the docker command.
This is the task:
task localDockerClean(type: Exec) {
executable './cleanDocker.sh'
ignoreExitValue true
}
This is the script:
#! /bin/bash
echo "Init - Clean Docker Containers"
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker system prune --force --volumes
echo "End - Clean Docker Containers"
And this is the output that only happens when run from IntelliJ IDEA:
> Task :scripts:migrations:localDockerClean
Init - Clean Docker Containers
./cleanDocker.sh: line 4: docker: command not found
./cleanDocker.sh: line 4: docker: command not found
./cleanDocker.sh: line 5: docker: command not found
./cleanDocker.sh: line 5: docker: command not found
./cleanDocker.sh: line 6: docker: command not found
End - Clean Docker Containers
Looks like the script has no access to the docker command which is in the PATH in my MacOS but only when run from the IDE, otherwise it works fine.
Any idea how to fix this issue in my IDE?
Thank you!

bash does not work using Docker's SHELL instruction

I need to use bash in the scripts of a CI/CD pipeline, so I'm trying with this simple image:
FROM alpine:3.4
RUN apk update -q && apk add --no-cache bash -q
SHELL ["/bin/bash", "--login", "-c"]
RUN echo $0
What I get in my terminal is this:
$ docker build .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM alpine:3.4
---> b7c5ffe56db7
Step 2/4 : RUN apk update -q && apk add --no-cache bash -q
---> Using cache
---> 18b729da453c
Step 3/4 : SHELL ["/bin/bash", "--login", "-c"]
---> Using cache
---> 03ca0df4a543
Step 4/4 : RUN echo $0
---> Running in a65419dafc4b
/bin/bash
Removing intermediate container a65419dafc4b
---> 2963f9e0e563
Successfully built 2963f9e0e563
After that, I run that container and get this:
$ docker run -it 2963f9e0e563
/ $ echo $0
/bin/sh
/ $
(it runs as root but I changed "#" with "$" for this post)
Why is it using sh instead of bash?
The SHELL command only modifies the shell that is used to execute the instructions inside the Dockerfile.
If you want to change the shell that's used for the container runtime, you can add CMD ["/bin/bash"] to your Dockerfile in order to change the container default executable to /bin/bash. You could also specify the executable from the command line using docker run -it <image> /bin/bash.

Run sh script from docker container

I build a docker image with bellow Dockerfile:
FROM node:8.9.4
ADD setENV.sh /usr/local/bin/setENV.sh
RUN chmod +x /usr/local/bin/setENV.sh
CMD [ "/bin/bash" "usr/local/bin/setENV.sh" ]
The setENV script is:
#!/bin/sh
echo "PORT=${PORT:-1234}" >> .env
echo "PORT_SERVICE=${PORT_SERVICE:-8888}" > .env
echo "HOST_SERVICE=${HOST_SERVICE:-1234}" > .env
I build the image as:
docker image build -t my-node .
And then I run the image as:
docker run -it my-node bash
But the script is not executed.
From inside the container a run the script as:
/bin/bash usr/local/bin/setENV.sh
And working fine.
Note that I am using docker for windows.
The command
docker run -it my-node bash
just runs bash.
To run the CMD, you have to do
docker run -it my-node
However, note that your container will immediately exit because there is nothing to do after writing to the file. So to see the result, you would need to add cat .env or something to setENV.sh.
I think the last line should be
CMD [ "/bin/bash", "usr/local/bin/setENV.sh" ]
You missed a ,.

Dockerfile: code 127 when trying to RUN shell-script

Running Docker Toolbox on Windows 10 host.
There is a Dockerfile:
FROM 16.04
...
RUN if [ some_condition ]; then ./foo.sh; fi
...
There is a foo.sh:
#!/bin/bash
...
echo 'Me working'
Now when trying to build the Docker image:
docker build -t name_of_the_image .
Getting error:
Step 7/12 : RUN ./foo.sh
---> Running in e7e0703d3f8f
/bin/sh: 1: ./foo.sh: not found
The command '/bin/sh -c ./foo.sh' returned a non-zero code: 127
I would assume error 127 would be the Docker doesn't see the bash. Any suggestion how to fix this?
Edit: already copying all files into the Docker, Dockerfile:
FROM ubuntu:16.04
MAINTAINER Mr Anderson "mr#anderson.com"
# set workdir
COPY . /app
WORKDIR /app
# Run scripts
RUN ./foo.sh
You'll need to copy/COPY the file into the container before you can execute/RUN the script.
Also since you're using a relative path when you call the script be sure to set a WORKDIR.
COPY ./foo.sh /app/foo.sh
WORKDIR /dir
RUN chmod +x /app/foo.sh
RUN if [ some_condition ]; then ./foo.sh; fi
Also make sure the script is executable.
After some further investigation:
Using CMD over RUN is not a perfect solution because of the way those commands work. RUN can be used any amount of times, to build Docker image layer by layer, while CMD can be executed only once when the image has been build.
In my case the solution was to:
Open ./foo.sh file with VIM and run: :set fileformat=unix and save the file.
Long story short: the line ending in the shell-script were incorrect and had to be converted to the Unix ones.

Resources