docker' is not recognized as an internal or external command - docker

I'm trying to deploy in docker container using Jenkins on windows but my build failed with following errors.
1>' docker' is not recognized as an internal or external command,
operable program or batch file.
2>Build step 'Execute Windows batch command' marked build as failure
docker build -t reactapp .
docker run --rm -p 8080:8080 --name "recipes" reactapp

' docker' is not recognized as an internal or external command'
This errors means that you dont have docker in your PATH environment variable.
You need to look for the docker.exe location, I did a default installation and I have it in : C:\Program Files\Docker\Docker\resources\bin
and add it to your path environment variable.
How to Add to Windows PATH Environment Variable

Related

Running Jenkins job with docker command on kubernetes cluster fails "docker: not found"

We are running a Kubernetes cluster for building Jenkins jobs. For the pods we are using the odavid/jenkins-jnlp-slave JNLP docker image. I mounted the /var/run/docker.sock to the pod container and added jenkins(uid=1000) user to the docker group on the host systems.
When running a shell script job in Jenkins with e.g. docker ps it fails with error docker: not found.
$ /bin/sh -xe /tmp/jenkins6501091583256440803.sh
+ id
uid=1000(jenkins) gid=1000(jenkins) groups=1000(jenkins)
+ docker ps
/tmp/jenkins2079497433467634278.sh: 8: /tmp/jenkins2079497433467634278.sh: docker: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
The interesting thing is that when connecting into the pod manually and executing docker commands directly in the container as jenkins user, it works:
kubectl exec -it jenkins-worker-XXX -- /bin/bash
~$ su - jenkins
~$ id
uid=1000(jenkins) gid=1000(jenkins) groups=1000(jenkins),1000(jenkins)
~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
What is doing Jenkins in its job differently? Same user, same container, only groups=1000(jenkins),1000(jenkins) lists 1000(jenkins) as group 2 times when connecting manually. What am i missing?
/var/run/docker.sock is just the host socket that allows docker client to run docker commands from the container.
What you are missing is the docker client in your container.
Download the docker client manually and place it on a persistent volume and ensure that he docker client is in the system path. Also, ensure that the docker client is executable.
This command will do it for you. You may have to get the right version of the docker client for your environment
curl -fsSLO https://get.docker.com/builds/Linux/x86_64/docker-17.03.1-ce.tgz &&
tar --strip-components=1 -xvzf docker-17.03.1-ce.tgz -C /usr/local/bin
You may even be able to install the docker using the package manager for your image.

Compile error when running a docker script in Jenkins build

I have been working through the docker book and I am now learning about CI. I tried to run this script within the execute shell of my build:
# Build the image to be used for this job.
IMAGE=$(sudo docker build . | tail -1 | awk '{ print $NF }')
# Build the directory to be mounted into Docker.
MNT="$WORKSPACE/.."
# Execute the build inside Docker.
CONTAINER=$(sudo docker run -d -v $MNT:/opt/project/ $IMAGE /bin/ bash -c 'cd /opt/project/workspace; rake spec')
# Attach to the container so that we can see the output.
sudo docker attach $CONTAINER
# Get its exit code as soon as the container stops.
RC=$(sudo docker wait $CONTAINER)
# Delete the container we've just used.
sudo docker rm $CONTAINER
# Exit with the same value as that with which the process exited.
exit $RC
Running this script ends in the build failing. It shows these two errors:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
and
sudo docker run -d -v /private/var/jenkins_home/jobs/${Docker_test_job}/workspace/..:/opt/project/ /bin/ bash -c cd /opt/project/workspace; rake spec
docker: invalid reference format.
See 'docker run --help'.
+ CONTAINER=
Build step 'Execute shell' marked build as failure
Recording test results
ERROR: Step ‘Publish JUnit test result report’ failed: No test report files were found. Configuration error?
Finished: FAILURE
I don't understand how to fix it as I've been following the instructions in the book. I tried using $PWD to try and fix my issue but that didn't work either.
Actaully the jenkins user does not have the permission to run docker command. To do this, add your jenkins user to the docker group:
sudo usermod -aG docker jenkins
Then restart your jenkins server to refresh the group.
Please be informed that ther is a warning "The docker group grants privileges equivalent to the root user. For details on how this impacts security in your system."

Why isn't docker recognizing my "-f" option?

Following this post -- docker: "build" requires 1 argument. See 'docker build --help', I'm trying to build my docker image using a file with a non-traditional name ("local.Dockerfile") on Mac 10.13.6. I tried the below
localhost:mydir davea$ docker build -t mycontainer -f local.Dockerfile
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
But docker is choking on me. I'm running version 19.03.5.
Basic command to build docker image:
docker build -t <image_tag> -f <Dockerfile_name> <Path_of_Dockerfile>
So you are missing to specify the path of your local.Dockerfile (which is mandatory). If your dockerfile is in the current directory from where you are running the command, then run below command, else update the path accordingly:
docker build -t mycontainer -f local.Dockerfile .
Note: You can specify the Path_of_Dockerfile in any way: relative path or absolute path, whichever way you feel comfortable.

Docker run uses host PATH when chaining commands

I have written an image that bundles utils to run commands using several CLIs. I want to run this as an executable as follows:
docker run my_image cli command
Where CLI is my custom CLI and command is a command to that CLI.
When I build my image I have the following instruction in the Dockerfile:
ENV PATH="/cli/scripts:${PATH}"
The above works if I do not chain commands to the container. If I chain commands it stops working:
docker run my_image cli command && cli anothercommand
Command 'cli' not found, but can be installed with...
Where the first command works and the other fails.
So the logical conclusion is that cli is missing from path. I tried to verify that with:
docker run my_image printenv PATH
This actually outputs the containers PATH, and everything looks alright. So I tried to chain this command too:
docker run my_image printenv PATH && printenv PATH
And sure enough, this outputs first the containers PATH and then the PATH of my system.
What is the reason for this? How do I work around it?
When you type a command into your shell, your local shell processes it first before any command gets run. It sees (reformatted)
docker run my_image cli command \
&& \
cli anothercommand
That is, your host's shell picks up the &&, so the host first runs docker run and then runs cli anothercommand (if the container exited successfully).
You can tell the container to run a shell, and then the container shell will handle things like command chaining, redirections, and environment variables
docker run my_image sh -c 'cli command && cli anothercommand'
If this is more than occasional use, also consider writing this into a shell script
#!/bin/sh
set -e
cli command
cli another command
COPY the script into your Docker image, and then you can docker run my_image cli_commands.sh or some such.

jenkins - docker: command not found.. path setup

SO i'm very new to jenkins and I'm trying to use jenkins to automatically build my docker image.
using the freestyle project
under build step i added an execute shell
added "docker images" (to see if docker worked)
cont
following error:
command:
docker images
output:
/var/folders/ym/d71xv1gx4fq16slmbtkmwr680000gn/T/jenkins80660521833580 63134.sh: line 2: docker: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
however
if I issue the following command
/usr/local/bin/docker images -- this works
question
How do I setup the path variable for docker so that I don't have to specify the path to the docker binary?
I would suggest checking what's the job PATH variable is. In your execute shell script add echo $PATH on the top, run the job again and see in the console output the result of that echo command, if the /usr/local/bin is in the PATH. If not, you should probably modify your PATH in the global jenkins configuration - Jenkins -> Manage Jenkins -> Configure System -> under Global Properties, Environment Variables should be checked, PATH var added and it should contain the /usr/local/bin path (together with all the other paths). For testing purposes, you can run export PATH=$PATH:/usr/local/bin on the top section of your shell script to see if the docker command runs.
This is what worked for me:
enables extras
sudo yum-config-manager --enable rhui-REGION-rhel-server-extras
installs docker
yum -y install docker-ce
starts docker
sudo systemctl start docker
test runs if docker is installed
sudo docker run hello-world
enables docker to start up on boot up
sudo systemctl enable docker.service
with these commands the above error didn't occur.

Resources