Command not found in Docker when running from Dockerfile - docker

I am trying to build and run the docker according the Dockerfile.
The code is (before I cloned my git repo etc):
sh 'docker build -t myimage .'
sh 'docker run myimage'
The Dockerfile looks like:
FROM node:12.2.0-alpine AS build
COPY . /frontend
WORKDIR /src
CMD apt-get update
CMD gradle build
The docker build command seems to be success with result:
Successfully built 0329878899fc
Successfully tagged myimage:latest
but when executing the docker run command, it says:
+ docker run myimage
/bin/sh: gradle: not found
ERROR: script returned exit code 127
I tried to remove CMD gradle and kept only CMD apt-get update, but then it says apt-get not found.
If I replaced CMD with RUN, then the docker build is not executed and says the same error.

Apt won't be available, as you're using an alpine Linux container which uses the Apk package manager.
It seems the node image does not include Gradle by default, so you will have to install it using a RUN command.
You can do that through apt by switching to a debian based image, or by searching for the Gradle package for Alpine linux

Related

Docker CMD and WORKDIR getting converted to RUN

I've created a docker image with Artifactory and Terraform to be used by pods in a K8s Cluster but it wont persist, the pod gets deleted immediately after spinning up and wasn't able to execute the job it was assigned to. Upon checking the pushed image in Artifactory converts the WORKDIR and CMD to RUN, is there anything I'm missing?
Here's the Dockerfile:
FROM alpine:3.16.2
LABEL maintainer="Platform Engineering"
# install dependencies
RUN apk add terraform
RUN apk add curl
RUN apk add tree
RUN curl -fL https://install-cli.jfrog.io | sh
RUN curl --location --output /usr/local/bin/release-cli "https://release-cli-downloads.s3.amazonaws.com/latest/release-cli-linux-amd64"
RUN chmod +x /usr/local/bin/release-cli
# check version of installed dependencies
RUN terraform -v
RUN jf -v
RUN release-cli -v
# target ci workspace under /tmp directory
WORKDIR /tmp/ci-workspace
CMD ["/bin/sh"]
Here's how the layers look like in Artifactory:
Tried rebuilding in Windows and other machine and installing one binary at a time, nothing worked.

Copy file from dockerfile build to host - bandit

I just started learning docker. To teach myself, I managed to containerize bandit (a python code scanner) but I'm not able to see the output of the scan before the container destroys itself. How can I copy the output file from inside the container to the host, or otherwise save it?
Right now i'm just using bandit to scan itself basically :)
Dockerfile
FROM python:3-alpine
WORKDIR /
RUN pip install bandit
RUN apk update && apk upgrade
RUN apk add git
RUN git clone https://github.com/PyCQA/bandit.git ./code-to-scan
CMD [ "python -m bandit -r ./code-to-scan -o bandit.txt" ]
You can mount a volume on you host where you can share the output of bandit.
For example, you can run your container with:
docker run -v $(pwd)/output:/tmp/output -t your_awesome_container:latest
And you in your dockerfile:
...
CMD [ "python -m bandit -r ./code-to-scan -o /tmp/bandit.txt" ]
This way the bandit.txt file will be found in the output folder.
Better place the code in your image not in the root directory.
I did some adjustments to your Dockerfile.
FROM python:3-alpine
WORKDIR /usr/myapp
RUN pip install bandit
RUN apk update && apk upgrade
RUN apk add git
RUN git clone https://github.com/PyCQA/bandit.git .
CMD [ "bandit","-r",".","-o","bandit.txt" ]`
This clones git in your WORKDIR.
Note the CMD, it is an array, so just devide all commands and args as in the Dockerfile about.
I put the the Dockerfile in my D:\test directory (Windows).
docker build -t test .
docker run -v D:/test/:/usr/myapp test
It will generate you bandit.txt in the test folder.
After the code is execute the container exits, as there are nothing else to do.
you can also put --rm to remove the container once it finishs.
docker run --rm -v D:/test/:/usr/myapp test

How to use "colcon build" in a Dockerfile?

I am trying to create a docker image based on ubuntu:20.04 where I want to install ROS2, ignition gazebo and the ROS2-ign-bridge with a Dockerfile.
The installation of ROS2 and ign work without any issue but during the bridge installation I need to use colcon. Heres that part from the Dockerfile:
## install ROS2 ignition gazebo bridge
RUN export IGNITION_VERSION=edifice
RUN mkdir -p ros_ign_bridge_ws/src
RUN git clone https://github.com/osrf/ros_ign.git -b foxy ros_ign_bridge_ws/src
WORKDIR ros_ign_bridge_ws
RUN rosdep install -r --from-paths src -i -y --rosdistro foxy
RUN colcon build
RUN source ros_ign_bridge_ws/install/setup.bash
RUN echo "source ros_ign_bridge_ws/install/setup.bash" >> ~/.bashrc
It fails during the colcon build step when I use
docker build -f Dockerfiles/companion_base.Dockerfile -t companion_base .
, but when I run the image created up to that step
docker run -it c125a17c2f68 /bin/bash
and then execute colcon build inside the container it works without any issue.
So what is the difference between RUN colcon build and running colcon build inside the container ?
The issue was that when you source something in a previous docker build step, it isn't available in the next step. So what I needed to do was do the sourcing and building in the same step:
RUN /bin/bash -c "source /opt/ros/foxy/setup.bash; colcon build"

Docker, Output is hidden

Inside a dockerfile I had:
RUN python3 --version
While I see the command being executed when I build my docker image like this:
docker build -t dn ./
I'm not seeing an output.
So I though about changing it to:
RUN echo python3 --version
But that didn't solve my problem, any suggestions?
Note: I don't want all commands to show output in terminal only the ones I specify.
Try setting the environment variable DOCKER_BUILDKIT=0 and then execute the docker build command again. DOCKER_BUILDKIT=0 docker build -t dn ./
as explained here https://makeoptim.com/en/tool/docker-build-not-output

docker build with inline fails

I am trying to build a docker image with inline commands, it goes well until copy statement where it fails saying it cannot find the file. When I put the build statements in Dockerfile and run build, it works fine.
docker build -t casspy -<<EOF
FROM alpine:latest
RUN apk -v add python3 py-pip bash && \
pip install ldap3 cassandra-driver configargparse boto3
COPY script.py .
EOF
Step 3/3 : COPY script.py .
COPY failed: stat /var/lib/docker/tmp/docker-builder451609694/script.py: no such file or directory
You need a specify a build context for docker.
Omitting the build context can be useful in situations where your Dockerfile does not require files to be copied into the image, and improves the build-speed, as no files are sent to the daemon.
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
docker build -t casspy -f- . <<EOF

Resources