0

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.

Potion
  • 785
  • 1
  • 14
  • 36
  • `docker run --rm -dit -v $(PWD):/projects dockerImage bash -c "cd /projects &&"` , add the d ? – LinPy Aug 29 '19 at 04:26
  • Would it not make more sense to set `WORKDIR` in the `Dockerfile`? Do you mean you want to have an interactive shell which should switch to a specific directory? I suppose you could do `bash -c "cd there && exec bash -i"` – tripleee Aug 29 '19 at 04:28
  • @tripleee thank you that worked. WORKDIR would be an okay solution but my projects share the same container. I have different projects and on startup I want it to CD to the project location for each unique makefile command. – Potion Aug 29 '19 at 04:35
  • Possible duplicate of [Bash, execute command but continue with interactive session](https://stackoverflow.com/questions/4584736/bash-execute-command-but-continue-with-interactive-session) – tripleee Aug 29 '19 at 04:37

1 Answers1

0

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/.

takacsmark
  • 3,933
  • 23
  • 26