1

I am new to Docker and need some help. I want to make an interactive docker image for a c code. I have written a small c code for the same. The below is my c code:

**CODE:** 

#include<stdio.h>
#include<stdlib.h>
int main()
{
  int i,sum=0,n,num[10];
  printf("How many integers do you want to enter? ");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
     scanf("%d",&num[i]);
     sum+=num[i];
  }
  printf("Total Sum: %d\n",sum);
  printf("---------------------\n\n");
  return 0;
}

OUTPUT: How many integers do you want to enter? 3(User Input)
1 3 5(User Input)

Total Sum: 9

**DockerFile code**
          File: Dockerfile                             

FROM debian:latest
RUN mkdir -p /home/arup123/ExtendedAdd
COPY . /home/arup123/ExtendedAdd
CMD /home/arup123/ExtendedAdd/ExtendedAddition

My Attempt

I thought we could create an interactive shell like bash shell within the current shell to take user input but i am getting "Segmentation fault (core dumped)"

docker build -t image1 .
docker run -it --name image2 image1 /bin/bash 
/# 3
bash: 3: command not found
/# 1 2 3
bash: 1: command not found
/# exit
exit
docker commit image2 myuser/myimage:2.1
# docker run e2807f8b1966(image id of image1)
Segmentation fault (core dumped)

Please do let me know where I am going wrong and how i could correct the same!
-Thank you

learner
  • 99
  • 8

1 Answers1

1

First, don't docker run with /bin/bash at the end: that overrides the CMD defined in your Dockerfile.

If you have build your image (check the output of docker images), use:

docker run --rm -it --name prg myImage

If the segfault persists, that might be an issue with your C code, or its compilation (make sure it was compiled using a similar OS as the one used in your Docker image/Dockerfile.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • docker run --rm -it --name prg myimage Unable to find image 'myimage:latest' locally docker: Error response from daemon: pull access denied for myimage, repository does not exist or may require 'docker login'. See 'docker run --help'. I have got the above error!!! – learner Jun 14 '18 at 07:13
  • @learner yes, that is why I mentioned `docker images`, in order for you to pick the name you gave to your image when you built it with `docker build --tag myImageName` – VonC Jun 14 '18 at 07:25
  • It is working because of the -it option, an interactive session with try allocated, allowing for your program to have stdout and stderr. – VonC Jun 14 '18 at 08:57
  • what do you mean by allocating try? and why was the CMD command being overriden by /bin/bash?Is there any other alternative approach? – learner Jun 14 '18 at 09:02
  • See my other answer https://stackoverflow.com/questions/35550694/why-do-there-exist-i-and-t-options-for-the-docker-exec-command/35551071#35551071 – VonC Jun 14 '18 at 09:11