Run docker run command as npm script - docker

I have the following command that runs when run by itself (Output: Hello):
$ docker run -it --rm --name fetch_html -v ${pwd}:/usr/src/myapp -w /usr/src/myapp php:7.4-cli php
Hello
However, I want to run it as a npm script as it's a little tedious writing the whole thing out every time:
{
...
"scripts": {
"fetch_html": "docker run -it --rm --name fetch_html -v ${pwd}:/usr/src/myapp -w /usr/src/myapp php:7.4-cli php scripts/fetch_html/cli.php"
},
...
Then:
$ npm run fetch_html
But it gives me the following error:
docker: Error response from daemon: create ${pwd}: "${pwd}" includes
invalid characters for a local volume name, only
"[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a
host directory, use absolute path.
I've tried to change it to $(pwd) as I recall Windows and Linux having different syntax here(?). The host machine is Windows 10.

If you are using the Windows 10 CMD interpreter, try this:
{
...
"scripts": {
"fetch_html": "docker run -it --rm --name fetch_html -v %cd%\\:/usr/src/myapp -w /usr/src/myapp php:7.4-cli php scripts/fetch_html/cli.php"
},
...

Related

Trying to copy a script into a detached Docker container, and execute it with docker exec

Right now I am setting my Docker instance running with:
sudo docker run --name docker_verify --rm \
-t -d daoplays/rust_v1.63
so that it runs in detached mode in the background. I then copy a script to that instance:
sudo docker cp verify_run_script.sh docker_verify:/.
and I want to be able to execute that script with what I expected to be:
sudo docker exec -d docker_verify bash \
-c "./verify_run_script.sh"
However, this doesn't seem to do anything. If from another terminal I run
sudo docker container logs -f docker_verify
nothing is shown. If I attach myself to the Docker instance then I can run the script myself but that sort of defeats the point of running in detached mode.
I assume I am just not passing the right arguments here, but I am really not clear what I should be doing!
When you run a command in a container you need to also allocate a pseudo-TTY if you want to see the results.
Your command should be:
sudo docker exec -t docker_verify bash \
-c "./verify_run_script.sh"
(note the -t flag)
Steps to reproduce it:
# create a dummy script
cat > script.sh <<EOF
echo This is running!
EOF
# run a container to work with
docker run --rm --name docker_verify -d alpine:latest sleep 3000
# copy the script
docker cp script.sh docker_verify:/
# run the script
docker exec -t docker_verify sh -c "chmod a+x /script.sh && /script.sh"
# clean up
docker container rm -f docker_verify
You should see This is running! in the output.

Docker: standard_init_linux.go:211

I try to run python script with docker on windows pro in PowerShell.
When I run:
docker run -it --name mypython -v ${PWD}/myfirst:/app python /app/myfirst.py
I am getting an error:
standard_init_linux.go:211: exec user process caused "exec format error"
myfirst.py includes only a print statement:
print('Python in Containers!')
In same time, the following pieces of code work fine:
docker run -it --name mypython -v ${PWD}/myfirst:/app python
>>> exec(open('/app/myfirst.py').read())
Python in Containers!
and
docker run -it --name mypython -v ${PWD}/myfirst:/app python /bin/bash
root#fc18bbcfb818 cd /app
root#fc18bbcfb818 python myfirst.py
Python in Containers!
Any ideas why this happens?
The python Docker image has the CMD python3. This means that if you provide arguments after the image name, the CMD will be overwritten. So OP's example is equivalent to running /app/myfirst.py on the command line (note how this is different from python /app/myfirst.py). To fix this, use python /app/myfirst.py.
docker run -it --name mypython -v ${PWD}/myfirst:/app python python /app/myfirst.py

Save current directory between docker exec commands

I have a list of commands which I need to issue one by one to a running docker container. However, when I "cd" in the container, it's not working as expected. For example:
docker run -di --name example alpine:latest
for CMD in 'mkdir -p example && touch example/file' 'cd example' 'ls'
do
docker exec -w='/root' example sh -c "$CMD"
done
Will printout example instead of file. How should I properly execute series of statements, but preserving the working directory between their execution? Preferably, if it possible to do this without concatenating all the commands?
I think you should use this format:
dingrui#gdcni:~/onie$ docker exec -w /root example sh -c 'mkdir -p example; touch example/file; cd example; ls'
file
or write these commands to a script and then mount it to container and run it in container:
dingrui#gdcni:~/onie$ docker run -itd -w /root -v $(pwd):/app --name example busybox /app/test.sh
12f7f2b55182bd18c45ce31e03390544adaedc1a2dd923d3bc4293b214301650
dingrui#gdcni:~/onie$ docker logs example
file

Create MakeFile that Runs Docker Image and Changes Directory?

I would like to create a makefile that runs a docker container, automatically mount the current folder and within the container CD to the shared directory.
I currently have the following which runs the docker image and mounts the directory with no issue. But I am unsure how to get it to change directory.
run:
docker run --rm -it -v $(PWD):/projects dockerImage bash
I've seen some examples where you can append -c "cd /projects" at the end so that it is:
docker run --rm -it -v $(PWD):/projects dockerImage bash -c "cd /projects"
however it will immediately exit the bash command afterwards. Ive also seen an example where you can append && at the end so that it is the following:
docker run --rm -it -v $(PWD):/projects dockerImage bash -c "cd /projects &&".
Unfortunately the console will just hang.
You can specify the working directory in your docker run command with the -w option. So you can do something like this:
docker run --rm -it -v $(PWD):/projects -w /projects dockerImage bash
You can find this option in the official docs here https://docs.docker.com/engine/reference/run/.

How do I append to PATH environment variable when running a Docker container?

I want to mount a volume and add it to the container's PATH environment variable. I've tried the following and none is working.
docker run -it -v $(PWD):/app -e PATH=$PATH:/app/bin debian:jessie bash
docker run -it -v $(PWD):/app -e PATH='$PATH:/app/bin' debian:jessie bash
docker run -it -v $(PWD):/app -e PATH='$$PATH:/app/bin' debian:jessie bash
docker run -it -v $(PWD):/app -e PATH='\$PATH:/app/bin' debian:jessie bash
How do I append the mounted volume to PATH?
If you you use -e option, the $PATH value is the PATH of the host instead of the container.
You can do it like this:
docker run -it -v $(PWD):/app debian:jessie bash -c 'export PATH=$PATH:/app/bin; bash'
Within the docker command line, you can't get "what will be the value of $PATH at runtime". Thus, you cannot append a PATH to the PATH variable, with docker's -e flag. To achieve what you want to do, you will need to do that in a script that will get executed as the cmd / entrypoint of your container.
You can define a fixed Path for your imported Apps and add the new Path to the Apps into the Environment-Variable "Path"
Let's take your Path "/app". In your Dockerfile add the following Line:
ENV PATH=${PATH}:/app/bin
Build your modified Docker
Now you can access all Apps located under < external Directory >/bin that you mount to "/app" via
-v <external Directory>:/app
You can use a shell script (let's call it run.sh):
#/bin/bash
PATH=$PATH:/app/bin
"$#"
and call it from docker:
docker run -it -v $(PWD):/app debian:jessie /app/run.sh bash

Resources