How to customize the docker gcc command and add ow

2019-09-17 02:35发布

问题:

I'm trying to compile a C file named test.c using docker's gcc container.

I'm using the following command, I have put the test.c in my homein ubuntu.

sudo docker run -v /home/moomal/workspace/FypProject/WebContent/file/:/myapp.c:ro gcc:4.9 sh -c "gcc -o myapp /home/moomal/workspace/FypProject/WebContent/file/myapp.c; ./myapp"

It works cool, but, I want to change the folder from home to a folder inside my eclipse web project folder. I have an editor on a web page and then on compile it creates a test.c file inside a folder. I want to access that file.

I tried adding the path like /home/moomal/workspace/FypProject/WebContent/file but I get the error

   gcc: error: /home/moomal/workspace/FypProject/WebContent/file/myapp.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.
sh: 1: ./myapp: not found

回答1:

You seem to be confused about several things here.

The -v HOST_PATH:CON_PATH argument can be used to mount files inside a container, as you seem to be attempting. The HOST_PATH can be a file or a directory. The CON_PATH determines where that file or directory will be in the container. In your case, I think you want:

-v /home/moomal/workspace/FypProject/WebContent/file/myapp.c:/myapp.c

Not just ...file/:/myapp.c. I'm not sure what you expected your version to do (how can a directory be mapped to a file?).

Also, in the shell command you give the path on the host, but as this is processed in the container, you need the path in the container i.e:

gcc -o myapp /myapp.c

Putting it together, I would expect to see something like:

sudo docker run -v /home/moomal/workspace/FypProject/WebContent/file/myapp.c:/myapp.c:ro gcc:4.9 sh -c "gcc -o myapp /myapp.c; ./myapp"


标签: gcc docker