Unable to run shell in my container [duplicate] - docker

This question already has an answer here:
docker executable file not found in $PATH
(1 answer)
Closed 5 years ago.
I want to dockerize my application and I want to run and enter my container in order to see whether packages were installed properly, files were copied, etc.
Here's my Dockerfile:
FROM node:8.6
RUN mkdir /app
WORKDIR /app
COPY .*.json .
COPY src/ .
USER node
RUN yarn global add #angular/cli
EXPOSE 4200
The problem is, I can't run my container via docker run:
docker run my-notes -it --rm ash
I see errors:
container_linux.go:262: starting container process caused "exec: \"-it\": executable file not found in $PATH"
docker: Error response from daemon: oci runtime error: container_linux.go:262: starting container process caused "exec: \"-it\": executable file not found in $PATH".
ERRO[0000] error waiting for container: context canceled
What am I doing wrong?

The issue is that you need to pass the docker options before the image name not after that:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
I have answered this question here as well. Hope it helps.

The node image comes with sh, bash and dash.
If you want ash you can install the package, but it's just a symlink to /bin/dash so you can just run:
docker run -it --rm my-notes dash
To install the ash package, add the following to your Dockerfile
RUN set -uex; \
apt-get update; \
apt-get install ash; \
apt-get
Then ash/dash can be run with
docker run -it --rm my-notes ash

Related

Docker's "executable file not found in $PATH: unknown" trying to run "cd"

I've written the following Dockerfile which is supposed to run an arbitrary command (by providing one through arguments of docker run):
FROM ubuntu:20.04
RUN apt -y update && apt-get -y update
RUN apt install -y python3 git
CMD bash
But when I'm trying to pass the command, e.g. cd workspace I get the following:
C:\Users\user>docker run -it cloudbuildtoolset:latest cd workspace
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "cd": executable file not found in $PATH: unknown.
What am I doing wrong?
Please don't suggest me to restart my machine/docker/whatever
cd is a special built-in utility, in the language of the POSIX shell specification. It's something that changes the behavior of the running shell and not a standalone program. The error message means what it says: there is no /bin/cd or similar executable you can run.
Remember that a Docker container runs a single process, then exits, losing whatever state it has. It might not make sense for that single command to just change the container's working directory.
If you want to run a process inside a container but in a different working directory, you can use the docker run -w option
docker run -it \
-w /workspace \
cloudbuildtoolset:latest \
the command you want to run
or, equivalently, add a WORKDIR directive to your Dockerfile.
You can also launch a shell wrapper as the main container process. This would be able to use built-in commands like cd, but it's more complex to use and can introduce quoting issues.
docker run -it cloudbuildtoolset:latest \
/bin/sh -c 'cd /workspace && the command you want to run'

Start docker from docker - Can't connect to daemon

I'm trying to start a docker container from inside a docker container. I found multiple posts about this problem, but not for this specific case. What I found out so far is, that I need to install docker in the container and mount the hosts /var/run/docker.sh to the container's /var/run/docker.sh.
However I get the error:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
My Dockerfile:
FROM golang:alpine as builder
RUN mkdir /build
ADD . /build/
WORKDIR /build
RUN go build -o main .
FROM alpine
RUN adduser -S -D -H -h /app appuser
RUN apk update && apk add --no-cache docker-cli
COPY --from=builder /build/main /app/
WORKDIR /app
USER root
ENTRYPOINT [ "/app/main" ]
The command I'm running from my Go code:
// Start a new docker
cmd := exec.Command("docker", "ps") // Changed to "ps" just as a quick check
cmd.Run()
And the command I run to start the first docker container:
docker run --privileged -v /var/run/docker.sh:/var/run/docker.sh firsttest:1.0
Why can't the container connect to the docker daemon? Do I need to include something else? I tried to run the Go command as sudo, but as expected:
exec: "sudo": executable file not found in $PATH
And I tried to change the user in the Dockerfile to root, this did not change anything. Also I cannot start the docker daemon on the container itself:
exec: "service": executable file not found in $PATH
Did I misunderstand something or do I need to include something else in the Dockerfile? I really can't figure it out, thanks for the help!
I am not sure as to why you would want to run Docker inside a Docker container, except if you are a Docker developer. When I have felt tempted to do things like this, there was some kind of underlying architectural problem that I was trying to work around and that I should have fixed in the first place.
If you really want this, you could mount /var/run/docker.sock into your container:
docker run --privileged -v /var/run/docker.sh:/var/run/docker.sh -v /var/run/docker.sock:/var/run/docker.sock firsttest:1.0

docker image running error with `make` commands

I'm trying to build a docker image for my project.
The docker image could be found here: https://hub.docker.com/r/haipengzhang/merchant_score_project
The corresponding docker file is:
FROM continuumio/anaconda3
RUN apt-get update
RUN pip install lightgbm && \
pip install docopt==0.6.2 && \
pip install deap
When I try to run the project in this docker image with make commands under my project's repo:
docker run --rm -v /$(pwd):/home/xxx/ haipengzhang/merchant_score_project make -C /home/xxx/ clean
I get the following error:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"make\": executable file not found in $PATH": unknown.
Could anybody please help? Thanks!
The reason
I now know what happend here. When this question was originally asked, the image in question did not have the make command inside it.
Adding RUN apt-get install -y build-essential to the Dockerfile solves the issue. But, this image is built on dockerhub, so when the image was rebuilt after adding this instruction - it was not refreshed locally. You would actively need to pull the image again like I mention later in this answer.
I verified this, but rebuilding the image from your original Dockerfile definition and confirming that make is not in that image.
Original answer text
I checked, and the image haipengzhang/merchant_score_project does infact have have the make command in the path /usr/bin/make, and it is executable. The user in the image is root, and I am able to start the make command just fine.
There is a little problem with your docker run command, but I do not think that it is related to the issue:
You should not preceed the $(pwd) command with a / in the volume mapping. Try this instead:
docker run --rm -v $(pwd):/home/xxx/ haipengzhang/merchant_score_project make -C /home/xxx/ clean
The error that you are getting, idicates that the make command cimply cannot be found in the path or local working directory inside the image - if I try to pass a nonexisting command, I get the same error:
docker run --rm -v $(pwd):/home/xxx/ haipengzhang/merchant_score_project blabla
I get the same output:
docker: Error response from daemon: OCI runtime create failed:
container_linux.go:349: starting container process caused "exec: \"blabla\": executable file not found in $PATH": unknown.
This leads me to believe that there is something out of order with the revision of the image, I would be concerned that you do not have the latest image locally?
To refresh the local image:
docker image rm haipengzhang/merchant_score_project:latest
docker pull haipengzhang/merchant_score_project
You could try debugging the image with somthing like:
docker run --rm -v $(pwd):/home/xxx/ haipengzhang/merchant_score_project which make
docker run --rm -v $(pwd):/home/xxx/ haipengzhang/merchant_score_project ls -la /usr/bin/make
docker run --rm -v $(pwd):/home/xxx/ haipengzhang/merchant_score_project ls -la /home/xxx
And see if you get some indication that something is out of place

Error in creating a docker container from docker image

I am trying to use docker file for a flask app. I have this docker file(end of file):
CMD ["/usr/bin/python3 manage.py"]
it's been build successfully with the command sudo docker build -t server . then I run it with sudo docker run -dit -p 5000:5000 -t server:latest but I get :
6acfe48c74d96c12eeda2c2cc98e27d2e5478edaa44f2061336102f04cdf54c4
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"/usr/bin/python3 manage.py\": stat /usr/bin/python3 manage.py: no such file or directory": unknown.
Let me know if you need more information. ( I used which python3 and pasted the PATH and used ls and saw manage.py exists there)
When you say:
CMD ["/usr/bin/python3 manage.py"]
You are directing the system to run exactly that file; but there is no file named python3 manage.py in the /usr/bin directory. If you have a separate command and arguments then they need to be two separate things in the CMD listing:
CMD ["/usr/bin/python3", "manage.py"]

dockerfile, how to support docker run options -d, -v and -p?

I have a very simple dockerfile:
FROM ubuntu:16.04
ADD node-v6.11.1 /usr/local
RUN ln -s /usr/local/bin/node /usr/local/bin/nodejs
RUN node -v
COPY server /server
RUN cd /server && npm install
EXPOSE 80 443
VOLUME ["/server/public"]
CMD cd /server && node server
sudo docker run server works as expected.
sudo docker run server -v /public:/server/public results in:starting container process caused "exec: \"-v\": executable file not found in $PATH".
sudo docker run server -d results in:
starting container process caused "exec: \"-d\": executable file not found in $PATH"
sudo docker run server -p 80:80 gives similar error.
You have to pass the options before the image name as follow:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
For example:
sudo docker run -v /public:/server/public server

Resources