Have a Shell script as follows
ENV HOME=/home
RUN mkdir $HOME
Above script is in a Docker file and getting exception as follows
[91mmkdir: missing operand
Try 'mkdir --help' for more information.
Build step 'Docker Build and Publish' marked build as failure
I just tried with a Dockerfile like this:
FROM scratch
ENV HOME=/home
RUN mkdir $HOME
and got that:
$ docker build .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM scratch
--->
Step 2/3 : ENV HOME=/home
---> Running in e9d03ee00aa7
Removing intermediate container e9d03ee00aa7
---> 16f6e2ba2f09
Step 3/3 : RUN mkdir $HOME
---> Running in 6847b9b34717
failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown
$
Then I edited the file to look like this:
FROM mariadb
ENV HOME=/home
RUN mkdir $HOME
and got that:
$ docker build .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM mariadb
---> ea81af801379
Step 2/3 : ENV HOME=/home
---> Running in 7d903215da74
Removing intermediate container 7d903215da74
---> 77276b970083
Step 3/3 : RUN mkdir $HOME
---> Running in f120f966144f
mkdir: cannot create directory '/home': File exists
The command '/bin/sh -c mkdir $HOME' returned a non-zero code: 1
$
From the error message I can see the $HOME variable would not have been an issue. It should have worked on your side as well.
Related
I am trying to build this Dockerfile:
FROM alpine:latest
WORKDIR /root
ENV PATH /root/bin:$PATH
COPY foo ./bin/
RUN test -e /root/bin/foo && echo file exists || echo file missing
RUN foo
but the build fails with the error
/bin/sh: foo: not found
although the line starting with RUN test -e outputs file exists, indicating that foo has been successfully copied to /root/bin/foo.
Why RUN foo not able to find foo?
$ docker build -t foo .
Sending build context to Docker daemon 48.07MB
Step 1/6 : FROM alpine:latest
latest: Pulling from library/alpine
df9b9388f04a: Already exists
Digest: sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454
Status: Downloaded newer image for alpine:latest
---> 0ac33e5f5afa
Step 2/6 : WORKDIR /root
---> Running in 2a8051ebc3e9
Removing intermediate container 2a8051ebc3e9
---> 21b2b4aaccc5
Step 3/6 : ENV PATH /root/bin:$PATH
---> Running in 81eab1ec7d30
Removing intermediate container 81eab1ec7d30
---> 658116549586
Step 4/6 : COPY foo ./bin/
---> 1ea30752ca5c
Step 5/6 : RUN test -e /root/bin/foo && echo file exists || echo file missing
---> Running in a739113a0ef1
file exists
Removing intermediate container a739113a0ef1
---> f9d9c0ecc747
Step 6/6 : RUN foo
---> Running in 2c8f07185f3c
/bin/sh: foo: not found
The command '/bin/sh -c foo' returned a non-zero code: 127
Use this way below:
FROM alpine:latest
WORKDIR /root
ENV PATH /root/bin:$PATH
COPY foo ./bin/
RUN test -e /root/bin/foo && echo file exists || echo file missing
RUN sh /root/bin/foo
I'm building a Docker image for node:10.20.1-buster-slim as base image and the Makefile runs fine, it creates the image and runs it.
However when using this Docker build in CI pipelines in Jenkins which is installed on a unique machine. Sometimes, the same image (node:10.20.1-buster-slim) is built from two processes simultaneously (e.g two Jenkins users run two Acceptance tests at the same time) so sometimes I end getting this kind of errors when trying to run the the container, after building the image:
failed to get digest sha256:3d3f18764dcb8026243f228a3eace39dabf06677e4468edaa47aa6e888122fd7: open /var/lib/docker/image/overlay2/imagedb/content/sha256/3d3f18764dcb8026243f228a3eace39dabf06677e4468edaa47aa6e888122fd7: no such file or directory
Makefile targets:
define down
docker-compose down
endef
define prune
rm -rf build/dist/*.js.map build/dist/*.js build/dist/*.css
docker container prune -f
docker volume prune -f
docker image prune -f
endef
build/dist/main.js: $(SRC)
$(call down)
$(call prune)
chmod -R a+rw build
docker build . --target=prod -t web_app_release
docker run --mount type=bind,source="$(PWD)"/web_app/build,target=/opt/web_app/build --rm web_app_release npm run build-prod
docker run --mount type=bind,source="$(PWD)"/web_app/build,target=/opt/web_app/build --rm web_app_release npm run build-css
Error (reproducible sometimes):
Compiling web_app ...
docker-compose down
Removing network webapp_default
Network webapp_default not found.
rm -rf build/dist/*.js.map build/dist/*.js build/dist/*.css
docker container prune -f
Total reclaimed space: 0B
docker volume prune -f
Total reclaimed space: 0B
docker image prune -f
Total reclaimed space: 0B
chmod -R a+rw build
docker build . --target=prod -t web_app_release
Sending build context to Docker daemon 122.5MB
Step 1/10 : FROM node:10.20.1-buster-slim AS base
---> cfd03db45bdc
Step 2/10 : ENV WORKPATH /opt/web_app/
---> Using cache
---> e1e61766fc45
Step 3/10 : WORKDIR $WORKPATH
---> Using cache
---> e776ef1390bd
Step 4/10 : RUN chown -R node $WORKPATH
---> Using cache
---> 1a5bb0d392a3
Step 5/10 : USER node
---> Using cache
---> bc682380a352
Step 6/10 : COPY --chown=node:node package* $WORKPATH
---> Using cache
---> 71b1848cad1e
Step 7/10 : RUN npm install --only=prod --no-progress
---> Using cache
---> de4ef62ab81e
Step 8/10 : CMD ["npm", "start"]
---> Using cache
---> 319f69778fb6
Step 9/10 : FROM base AS prod
---> 319f69778fb6
Step 10/10 : COPY --chown=node:node . $WORKPATH
---> 3d3f18764dcb
Successfully built 3d3f18764dcb
failed to get digest sha256:3d3f18764dcb8026243f228a3eace39dabf06677e4468edaa47aa6e888122fd7: open /var/lib/docker/image/overlay2/imagedb/content/sha256/3d3f18764dcb8026243f228a3eace39dabf06677e4468edaa47aa6e888122fd7: no such file or directory
Makefile:91: recipe for target 'build/dist/main.js' failed
make[1]: *** [build/dist/main.js] Error 1
Makefile:94: recipe for target 'web_app' failed
make: *** [web_app] Error 2
[Pipeline] error
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: Build failed
Finished: FAILURE
How could I make it so that users could run tests concurrently without the build of the same image overlapping each other?
Could I use some tool included in docker or check manually with bash scripts or in the Makefile?
I need to run a cmd that will create my home folder within a docker container. So, if my username in my linux box is josecz then I could use it from within a Dockerfile to run a cmd like:
RUN mkdir /home/${GetMyUsername}
and get the folder /home/josecz after the Dockerfile is processed.
The only way just as commented by folks: use ARG, next gives you a workable minimal example:
Dockerfile:
FROM alpine:3.14.0
ARG GetMyUsername
RUN echo ${GetMyUsername}
RUN mkdir -p /home/${GetMyUsername}
Execution:
cake#cake:~/3$ docker build --build-arg GetMyUsername=`whoami` -t abc:1 . --no-cache
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM alpine:3.14.0
---> d4ff818577bc
Step 2/4 : ARG GetMyUsername
---> Running in 4d87a0970dbd
Removing intermediate container 4d87a0970dbd
---> 8b67912b3788
Step 3/4 : RUN echo ${GetMyUsername}
---> Running in 2d68a7e93715
cake
Removing intermediate container 2d68a7e93715
---> 100428a1c526
Step 4/4 : RUN mkdir -p /home/${GetMyUsername}
---> Running in 938d10336daa
Removing intermediate container 938d10336daa
---> 939729b76f09
Successfully built 939729b76f09
Successfully tagged abc:1
Explaination:
When docker build, you could use whoami to get the username who run the docker build, then pass to args GetMyUsername. Then, in Dockerfile, you could use ${GetMyUsername} to get the value.
I am using as a base, the following image: https://github.com/Kaggle/docker-python/blob/main/Dockerfile.
Here is my Dockerfile:
FROM kaggle/python
RUN ["chmod", "+x", "/opt/conda/etc/profile.d/conda.sh"]
SHELL ["/bin/bash", "--login", "-c" ]
RUN ["/opt/conda/etc/profile.d/conda.sh"]
ENTRYPOINT [ "/usr/bin/env" ]
RUN ["exec '$#'"]
RUN ["bash"]
I am running the command: docker build -t kaggle/no-jupyter .
At line 4 I get the error:
> [3/7] RUN ["/opt/conda/etc/profile.d/conda.sh"]:
#6 0.263 standard_init_linux.go:228: exec user process caused: exec format error
Here is a link to the conda.sh file: https://pastebin.com/Epu4d7Nq
I read that this might be because I am building a Linux image on Windows10...Any ideas?
See RUN Syntax:
RUN
RUN has 2 forms:
RUN (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
RUN ["executable", "param1", "param2"] (exec form)
What you choose is exec form which won't run in a shell, so your /opt/conda/etc/profile.d/conda.sh surely fails.
To fix it you need to change to:
RUN ["bash", "/opt/conda/etc/profile.d/conda.sh"]
Or just use shell form:
RUN /opt/conda/etc/profile.d/conda.sh
Minimal example:
run.sh:
echo "hello"
Dockerfile:
FROM ubuntu:16.04
COPY run.sh /
RUN ["bash", "/run.sh"]
Execution:
$ docker build -t abc:1 . --no-cache
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM ubuntu:16.04
---> 065cf14a189c
Step 2/3 : COPY run.sh /
---> 8741ec438afd
Step 3/3 : RUN ["bash", "/run.sh"]
---> Running in 5e6754c79bc1
hello
Removing intermediate container 5e6754c79bc1
---> 3edd77959de4
Successfully built 3edd77959de4
Successfully tagged abc:1
AND, if not use bash, it will show error likes next:
Dockerfile:
FROM ubuntu:16.04
COPY run.sh /
RUN ["/run.sh"]
Execution:
$ docker build -t abc:1 . --no-cache
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM ubuntu:16.04
---> 065cf14a189c
Step 2/3 : COPY run.sh /
---> 82412a703847
Step 3/3 : RUN ["/run.sh"]
---> Running in 2a2b00f966c7
standard_init_linux.go:211: exec user process caused "exec format error"
The command '/run.sh' returned a non-zero code: 1
I am using this Dockerfile
ARG IMAGE_ONE
FROM ${IMAGE_ONE}
RUN cat /etc/debian_version
ARG IMAGE_TWO
FROM ${IMAGE_TWO}
RUN cat /etc/debian_version
But it fails because it does not use the second var IMAGE_TWO:
$ docker build --no-cache --build-arg IMAGE_ONE=debian:7 --build-arg IMAGE_TWO=debian:8 .
Sending build context to Docker daemon 2.048kB
Step 1/6 : ARG IMAGE_ONE
Step 2/6 : FROM ${IMAGE_ONE}
---> 90c038768099
Step 3/6 : RUN cat /etc/debian_version
---> Running in f842d9cf4f17
7.11
Removing intermediate container f842d9cf4f17
---> 0f7f7afdd8a6
Step 4/6 : ARG IMAGE_TWO
---> Running in ed3d36f2f9cb
Removing intermediate container ed3d36f2f9cb
---> ae4ae3cabc02
Step 5/6 : FROM ${IMAGE_TWO}
--->
Step 6/6 : RUN cat /etc/debian_version
---> Running in 6f1c165e2765
OCI runtime create failed: container_linux.go:296:
starting container process caused "exec: \"/bin/sh\":
stat /bin/sh: no such file or directory": unknown
Docker version:
$ docker --version
Docker version 17.12.0-ce, build c97c6d6
Is there something wrong in my Dockerfile or is the docker build command wrong?
The reason is because IMAGE_TWO is not in the same scope check this
https://docs.docker.com/engine/reference/builder/#scope
Basically the ARG IMAGE_TWO is still part of the first stage and goes out of scope when that stage ends and will not be part of the second stage.
Declaring the arguments at the beginning allow the IMAGE_TWO to be in the second stage.
ARG IMAGE_ONE
ARG IMAGE_TWO
FROM ${IMAGE_ONE}
RUN cat /etc/debian_version
FROM ${IMAGE_TWO}
RUN cat /etc/debian_version
docker build --build-arg=IMAGE_ONE=debian:7 --build-arg=IMAGE_TWO=debian:8 .