Search code examples
dockermakefilemountcd

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.


Solution

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