Docker Environment variable not working in CMD - docker

i'm trying to pass the name of the script from the docker run but its not getting the script name in the cmd command.
Not sure what's wrong here, same thing works fine in springboot/java projects
Below is the docker file
FROM python:3.8.8
RUN apt-get update && apt-get upgrade -y && \
apt-get install -y nodejs
RUN apt-get install -y npm
WORKDIR /rubix-kyc
COPY . /rubix-kyc
RUN pip install -r /rubix-kyc/requirements.txt
ARG SCRIPT_NAME
ENV SCRIPT_NAME ${SCRIPT_NAME}
RUN mkdir -p video_recording/
RUN npm install
RUN npm install elastic-apm-node --save
EXPOSE 4443
CMD [ "npm", "run" , "${SCRIPT_NAME}"]
Updating the script for running docker.
docker run \
-e SCRIPT_NAME=start-local \
-p 4443:4443 $1
Need you help here

For the variables used in CMD it is important to pass it as environment variable on docker run besides defining with ARG and assigning with ENV in Dockerfile, as it is evaluated on runtime, e.g.:
docker run --rm -ti -e SCRIPT_NAME=value-of-script-name <docker-image-id>
Please adjust your Dockerfile as well:
ARG SCRIPT_NAME
ENV SCRIPT_NAME=$SCRIPT_NAME
...
CMD npm run $SCRIPT_NAME

Related

Docker exits container

I am trying to build my own docker image for apache2 and PHP. Can anyone tell my why my container exits after run when it supposes to run ["apache2ctl", "-D", "FOREGROUND"]?
FROM ubuntu:latest
RUN apt update -y && apt upgrade -y
RUN apt install software-properties-common -y
RUN add-apt-repository ppa:ondrej/php -y
RUN apt update -y && apt upgrade -y
ARG DEBIAN_FRONTEND=noninteractive
RUN DEBIAN_FRONTEND=noninteractive apt install -y nano vim iputils-ping sudo git curl php php-cli php-fpm
RUN apt install -y php-json php-mysql
RUN apt install -y php-zip php-mbstring php-curl php-xml php-pear php-bcmath
RUN apt install psmisc -y
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOF_DIR /var/log/apache2
# RUN useradd -ms /bin/bash devwl
EXPOSE 80/tcp
ENTRYPOINT ["/bin/sh", "-c"]
CMD ["apache2ctl", "-D", "FOREGROUND"]
Build command:
docker build -t www .
Run command:
docker run -itd -p 80:80 www
Ouput docker ps:
Just tried to build your Dockerfile. docker logs shows a problem with start command. Running container without -D option works well...
CMD ["apache2ctl", "start"]
Do you need to use <IfDefine ...> in conf files?
You need to delete the ENTRYPOINT line.
Since you have both an ENTRYPOINT and a CMD, they get combined together into a single argument list. That means you have an effective command like
ENTRYPOINT+CMD ["/bin/sh", "-c", "apache2ctl", "-D", "FOREGROUND"]
But sh -c only reads in the single next argument and executes it. The remaining arguments would be accessible inside that string as positional parameters $0, $1, ... but unless you refer to one of those then the command you're eventually running is only apachectl with no arguments.
You only need to invoke a shell at all if your command uses shell features (referencing environment variables or running multiple commands). Yours doesn't, so you don't need anything that mentions a shell; just delete the ENTRYPOINT and have the existing CMD start Apache.
In a Dockerfile, you shouldn't usually need to say sh -c at all. If you do need to invoke a shell to run some command, you can use Docker shell syntax as a plain string without the JSON-array syntax; for example
# needs a shell because of $VARIABLE and command; command syntax
CMD touch "$APACHE_LOG_DIR/started"; exec apache2ctl -DFOREGROUND
(If you do need to override this command with docker run arguments or in a Compose command:, these syntaxes will not automatically insert a shell wrapper and there you do need to specifically say sh -c 'some command' if you need a shell to process the command string; again note the single quotes to make the command string a single argument.)

Cannot run installed tool in Dockerfile even though its there

I installed diesel-cli in a Dockerfile:
FROM alpine:latest
ENV PATH="/root/.cargo/bin:${PATH}"
RUN apk update
RUN apk add postgresql curl gcc musl-dev libpq-dev bash
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
WORKDIR /app
RUN cargo install diesel_cli --no-default-features --features postgres
COPY . .
EXPOSE 8000
CMD [ "docker/entrypoint.sh"]
That works fine. The entrypoint.sh is:
#!/bin/bash
export PATH="/root/.cargo/bin:${PATH}"
ls /root/.cargo/bin/diesel
bash -c "/root/.cargo/bin/diesel setup"
The strange this is that the ls shows that the diesel binary is there. But when running the docker container it still says:
bash: line 1: /root/.cargo/bin/diesel: No such file or directory
I also tried calling diesel right from the Dockerfile with the same result.
Why can't I run diesel this way?
See comment by The Fool!
Using a different base image resolves the problem:
FROM debian:bullseye-slim
ENV PATH="/root/.cargo/bin:${PATH}"
RUN apt update -y
RUN apt install postgresql curl gcc libpq-dev bash -y
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
WORKDIR /app
# This may take a minute
RUN cargo install diesel_cli --no-default-features --features postgres
COPY . .
# provision the database
EXPOSE 8000
CMD [ "docker/entrypoint.sh"]

How do i launch my jenkins-cli through docker?

i have created the docker image that installs java,jenkins,jenkins-cli. now i need to pass some argument through jenkins-cli, so i need to launch jenkins-cli. How do i do it? i have no idea how it launches.
Here is my script
FROM ubuntu:14.04
RUN apt update; \
apt upgrade -y; \
apt install -y default-jdk curl wget git maven nano unzip; \
apt-get clean
ENV JAVA_HOME /usr
ENV PATH $JAVA_HOME/bin:$PATH
RUN apt-get autoclean $$ apt-get clear cache
RUN apt-get -yqq update
RUN apt-get -yqq --no-install-recommends install git bzip2 curl unzip
RUN apt-get update
# copy jenkins war file to the container
ADD http://mirrors.jenkins.io/war-stable/2.107.1/jenkins.war /opt/jenkins.war
RUN chmod 644 /opt/jenkins.war
ENV JENKINS_HOME /jenkins
# configure the container to run jenkins, mapping container port 8080 to that host port
ENTRYPOINT ["java", "-jar", "/opt/jenkins.war"]
EXPOSE 8080
RUN mkdir /jenkins/
RUN echo 2.107.1 > /jenkins/jenkins.install.UpgradeWizard.state
RUN echo 2.107.1 > /jenkins/jenkins.install.InstallUtil.lastExecVersion
#jenkin-cli installation
RUN cd /tmp && curl --insecure -OL http://192.168.99.100:8080/jnlpJars/jenkins-cli.jar
ADD /tmp/jenkins-cli.jar /opt/jenkins/jenkins-cli.jar
RUN chmod 644 /opt/jenkins-cli.jar
WORKDIR /opt/jenkins
ENTRYPOINT ["java", "-jar", "jenkins-cli.jar", "-noCertificateCheck", "-noKeyAuth"]
CMD ["--help"]
this is error:
Step 19/24 : RUN cd /tmp && curl --insecure -OL http://192.168.99.100:8080/jnlpJars/jenkins-cli.jar
---> Using cache
---> 9a6210009f84
Step 20/24 : ADD jenkins-cli.jar /opt/jenkins/jenkins-cli.jar
ADD failed: stat /mnt/sda1/var/lib/docker/tmp/docker-builder945232568/jenkins-cli.jar: no such file or directory[1]
This error is causing me for may other build too,if i can find best solution i can solve my all other problems.
2nd question is, is my scripting for my current problem is right? How can i modify?
Can anyone help me with this..?
thank u in advance
You forgot to create the /opt/jenkins folder
Change
ADD http://mirrors.jenkins.io/war-stable/2.107.1/jenkins.war /opt/jenkins.war
to
RUN mkdir /opt/jenkins
ADD http://mirrors.jenkins.io/war-stable/2.107.1/jenkins.war /opt/jenkins/jenkins.war
Given your cli tool is available from within your container with a simple command such as cli-tool_command this should work:
docker run --rm -it {container_image_name} {cli_tool_command} {cli_tool_args}

Why won't my docker container run unless I use -i -t?

If I run my Dockerfile with the following command, the docker container starts running and all is well.
docker run --name test1 -i -t 660c93c32a
However, if I run this command without the -it, the container does not appear to be running as docker ps returns nothing:
docker run -d --name test1 660c93c32a
.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
All I'm trying to do is run the container and then be able to attach and/or open a shell in the container later.
Not sure if the issue is in my dockerfile or not, so have pasted the dockerfile below.
############################################################
# Dockerfile to build Ubuntu/Ansible/Django
############################################################
# Set the base image to Ansible
FROM ubuntu:16.10
# File Author / Maintainer
MAINTAINER David
# Install Ansible and Related Deps #
RUN apt-get -y update && \
apt-get install -y python-yaml python-jinja2 python-httplib2 python-keyczar python-paramiko python-setuptools python-pkg-resources git python-pip
RUN mkdir /etc/ansible/
RUN echo '[local]\nlocalhost\n' > /etc/ansible/hosts
RUN mkdir /opt/ansible/
RUN git clone http://github.com/ansible/ansible.git /opt/ansible/ansible
WORKDIR /opt/ansible/ansible
RUN git submodule update --init
ENV PATH /opt/ansible/ansible/bin:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin
ENV PYTHONPATH /opt/ansible/ansible/lib
ENV ANSIBLE_LIBRARY /opt/ansible/ansible/library
# Update the repository sources list
RUN apt-get update -y
RUN apt-get install python -y
RUN apt-get install python-dev -y
RUN apt-get install python-setuptools -y
RUN apt-get install python-pip
RUN mkdir /ansible/
WORKDIR /ansible
COPY ./ansible ./
WORKDIR /
RUN ansible-playbook -c local ansible/playbooks/installdjango.yml
ENV PROJECTNAME davidswebsite
CMD django-admin startproject $PROJECTNAME
When you run your container, command after CMD or ENTRYPOINT becomes $1 process of you container. If this process doesn't run well, your container will die.
So, check container logs using: docker logs <container id>
and recheck your command in CMD django-admin startproject $PROJECTNAME

Docker CMD doesn't see installed components

I am trying to build a docker image using the following docker file.
FROM ubuntu:latest
# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# Update packages
RUN apt-get -y update && apt-get install -y \
curl \
build-essential \
libssl-dev \
git \
&& rm -rf /var/lib/apt/lists/*
ENV APP_NAME testapp
ENV NODE_VERSION 5.10
ENV SERVE_PORT 8080
ENV LIVE_RELOAD_PORT 8888
# Install nvm, node, and angular
RUN (curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash -) \
&& source /root/.nvm/nvm.sh \
&& nvm install $NODE_VERSION \
&& npm install -g angular-cli \
&& ng new $APP_NAME \
&& cd $APP_NAME \
&& npm run postinstall
EXPOSE $SERVE_PORT $LIVE_RELOAD_PORT
WORKDIR $APP_NAME
EXPOSE 8080
CMD ["node", "-v"]
But I keep getting an error when trying to run it:
docker: Error response from daemon: Container command 'node' not found or does not exist..
I know node is being properly installed because if I rebuild the image by commenting out the CMD line from the docker file
#CMD ["node", "-v"]
And then start a shell session
docker run -it testimage
I can see that all my dependencies are there and return proper results
node -v
v5.10.1
.....
ng -v
angular-cli: 1.0.0-beta.5
node: 5.10.1
os: linux x64
So my question is. Why is the CMD in Dockerfile not able to run these and how can I fix it?
When using the shell to RUN node via nvm, you have sourced the nvm.sh file and it will have a $PATH variable set in it's environment to search for executable files via nvm.
When you run commands via docker run it will only inject a default PATH
docker run <your-ubuntu-image> echo $PATH
docker run <your-ubuntu-image> which node
docker run <your-ubuntu-image> nvm which node
Specifying a CMD with an array execs a binary directly without a shell or a $PATH to lookup.
Provide the full path to your node binary.
CMD ["/bin/node","-v"]
It's better to use the node binary rather than the nvm helper scripts due to the way dockers signal processing works. It might be easier to use the node apt packages in docker rather than nvm.

Resources