ibse
ibse

Reputation: 593

Run the program in a Linux environment on Windows using Docker

I need to find out how a simple C console application will work in Ubuntu. I have Windows installed on my machine. In order not to run the virtual machine, I decided to use Docker, it seems to be intended for this purpose. But I don't understand how to do it.

I downloaded and installed Docker Toolbox from here https://docs.docker.com/toolbox/toolbox_install_windows/

Then I run Docker Quickstart Terminal and write $ docker run ubuntu gcc-o hello hello.c there and get an error:

C:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"gcc\": executable file not found in $PATH": unknown.

hello.c - source code in C that prints "hello world" to the screen. This file is located in the same directory as docker.exe Other commands from ubuntu, such as $ docker run ubuntu echo 'Hello world' work

enter image description here

I'm new to Docker. Am I using Docker as intended? If so, why doesn't it work ?

Upvotes: 0

Views: 1400

Answers (1)

Carlos Garcia
Carlos Garcia

Reputation: 2970

Create a file and name it dockerfile next to your hello.c. Your directory structure should look like this

- tempdir
   |_ hello.c
   |_ dockerfile

In the dockerfile file you will give instructions to docker on how to build your container image. Paste into dockerfile theses instructions

FROM gcc
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
RUN gcc -o myapp hello.c
CMD ["./myapp"]

Then you can build your image using this command

C:\tempdir> docker build . --tag helloworldfromgcc 
note: make sure you are in the dockerfile folder

and last, run your container :)

docker run helloworldfromgcc

Explanations on the dockerfile instructions

# Here you are telling docker that as a base image it should use gcc.
# That image will be downloaded from here: https://hub.docker.com/_/gcc
# that gcc image has the linux kernel and gcc installed on it (not accurate, but good enough to understand)
FROM gcc


# This line will copy your files from your machine disk to the container virtual disk.
# This means the hello.c file will be copied into /usr/src/myapp folder inside the container
COPY . /usr/src/myapp

# This is like doing 'cd /usr/src/myapp'
WORKDIR /usr/src/myapp

# You know this one :) just call gcc with the standard params
RUN gcc -o myapp hello.c

# CMD differs from run because it will be executed when you run the container, and not when you are building the image
CMD ["./myapp"]

Upvotes: 4

Related Questions