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/.
Related
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.
So recently came across this weird case where I am trying to access file in my local dir into my container.
When I run the following command on terminal it runs fine and shows the expected list of file
docker run -it --rm -v $(pwd):/mnt/data -w /mnt/data-sink artprod.dev.abc.com/org/cli ls
But when I try to run this via makefile it shows nothing,(I run make in the same path where I run the docker cmd in the previous step)
docker-publish: build
echo "Publishing $(APP_NAME) snapshot $(VERSION)"
docker run -it --rm -v $(pwd):/mnt/data -w /mnt/data-sink artprod.dev.abc.com/org/cli ls
$(shell pwd) worked simply $(pwd) in make didnt do the shell interpolation.
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
I'm having an issue with docker run syntax... I've tried these 2 commands and they both seem to generate the error message on the last line.
docker run --rm ${DOCKER_ARGS} -i ${BUILD_IMAGE} /bin/bash -c "diff /usr/local/bin/protoc /go/bin/protoc2"
docker run --rm ${DOCKER_ARGS} -i ${BUILD_IMAGE} diff /usr/local/bin/protoc /go/bin/protoc2
diff: /go/bin/protoc2/protoc: No such file or directory
Am I missing something syntax-wise? I'm not sure why it's not running my command properly.
$ sudo cp /bin/ls /bin/ls2
$ docker run --rm -v /bin/ls2:/bin/ls2 ubuntu:16.04 diff /bin/ls /bin/ls2
Binary files /bin/ls and /bin/ls2 differ.
So I am pretty sure that your local mount doesn't exists and creates a directory.
Change your run statement to
docker run --rm ${DOCKER_ARGS} -i ${BUILD_IMAGE} /bin/bash -c "ls -alh /go/bin/protoc2 && diff /usr/local/bin/protoc /go/bin/protoc2"
And you will know what is wrong
I'm trying to create a Dockerfile that copies all the files in the currently directory to a specific folder.
Currently I have
COPY . /this/folder
I'm unable to check the results of this command, as my container closes nearly immediately after I run it. Is there a better way to test if the command is working?
you can start a container and check.
$ docker run -ti --rm <DOCKER_IMAGE> sh
$ ls -l /this/folder
If your docker image has ENTRYPOINT setting, then run below command:
$ docker run -ti --rm --entrypoint sh <DOCKER_IMAGE>
$ ls -l /this/folder
If it is only for testing, include the below command in your docker file:
RUN cd /this/folder && ls
This will list the directory contents while docker build