Run the program in a Linux environment on Windows using Docker - 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
I'm new to Docker. Am I using Docker as intended? If so, why doesn't it work ?

Create a file and name it dockerfile next to your hello.c. Your folder 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"]

Related

How to copy a file from the host into a container while starting?

I am trying to build a docker image using the dockerfile, my purpose is to copy a file into a specific folder when i run the "docker run" command!
this my dockerfile code:
FROM openjdk:7
MAINTAINER MyPerson
WORKDIR /usr/src/myapp
ENTRYPOINT ["cp"]
CMD ["/usr/src/myapp"]
CMD ls /usr/src/myapp
After building my image without any error (using the docker build command), i tried to run my new image:
docker run myjavaimage MainClass.java
i got this error: ** cp: missing destination file operand after ‘MainClass.java’ **
How can i resolve this? thx
I think you want this Dockerfile:
FROM openjdk:7
WORKDIR /usr/src/myapp
COPY MainClass.java .
RUN javac MainClass.java
ENV CLASSPATH=/usr/src/myapp
CMD java MainClass
When you docker build this image, it COPYs your Java source file from your local directory into the image, compiles it, and sets some metadata telling the JVM where to find the resulting .class files. Then when you launch the container, it will run the single application you've packaged there.
It's common enough to use a higher-level build tool like Maven or Gradle to compile multiple files into a single .jar file. Make sure to COPY all of the source files you need in before running the build. In Java it seems to be common to build the .jar file outside of Docker and just COPY that in without needing a JDK, and that's a reasonable path too.
In the Dockerfile you show, Docker combines ENTRYPOINT and CMD into a single command and runs that command as the single main process of the container. If you provide a command of some sort at the docker run command, that overrides CMD but does not override ENTRYPOINT. You only get one ENTRYPOINT and one CMD, and the last one in the Dockerfile wins. So you're trying to run container processes like
# What's in the Dockerfile
cp /bin/sh -c "ls /usr/src/myapp"
# Via your docker run command
cp MainClass.java
As #QuintenScheppermans suggests in their answer you can use a docker run -v option to inject the file at run time, but this will happen after commands like RUN javac have already happened. You don't really want a workflow where the entire application gets rebuilt every time you docker run the container. Build the image during docker build time, or before.
Two things.
You have used CMD twice.
CMD can only be used once, think of it as the purpose of your docker image. Every time a container is run, it will always execute CMD if you want multiple commands, you should use RUN and then lastly, used CMD
FROM openjdk:
MAINTAINER MyPerson
WORKDIR /usr/src/
ENTRYPOINT ["cp"]
RUN /usr/src/myapp
RUN ls /usr/src/myapp
Copying stuff into image
There is a simple command COPY the syntax being COPY <from-here> <to-here>
Seems like you want to run myjavaimage so what you will do is
COPY /path/to/myjavaimage /myjavaimage
CMD myjavaimage MainClass.java
Where you see the arrows, I've just written dummy code. Replace that with the correct code.
Also, your Dockerfile is badly created.
ENTRYPOINT -> not sure why you'd do "cp", but it's an actual entrypoint. Could point to the root dir of your project or to an app that will be run.
Don't understand why you want to do ls /usr/src/myapp but if you do want to do it, use RUN and not CMD
Lastly,
Best way to debug docker containers are in interactive mode. That means ssh'ing in to your container, have a look around, run code, and see what is the problem.
Run this: docker run -it <image-name> /bin/bash and then have a look inside and it's usually the best way to see what causes issues.
This stackoverflow page perfectly answers your question.
COPY foo.txt /data/foo.txt
# where foo.txt is the relative path on host
# and /data/foo.txt is the absolute path in the image
If you need to mount a file when running the command:
docker run --name=foo -d -v ~/foo.txt:/data/foo.txt -p 80:80 image_name

Not able to create docker image of .net core web API - COPY failed: stat /var/lib/docker/tmp/../netcoreapp2.1/publish

I'm trying to build an image of a .netcore v2.2 WebAPI app from docker file. But I'm getting a "no such file or directory" error while I try to build the image using following command:
docker build -t helloworld .
I have both microsoft/dotnet "latest" and "2.2-aspnetcore-runtime" base images installed in my machine.
Here's my docker file
FROM microsoft/dotnet
RUN mkdir /app
COPY ./bin/Release/netcoreapp2.2 /app
WORKDIR /app
ENTRYPOINT ["dotnet", "/app/HelloWorld.dll"]
# ASP.NET Core: Kestrel should listen on all IPs
ENV ASPNETCORE_URLS="http://0.0.0.0:5000"
Here's the folder structure of my solution (I tried the command inside the src):
Here's the output directory
Here's the full error details from the docker:
COPY failed: stat /var/lib/docker/tmp/docker-builder896068669/bin/Release/netcoreapp2.2: no such file or directory
you may change ./bin/Release/netcoreapp2.2 to ./bin/release/netcoreapp2.2
folder names are case sensitive
It seems the file ./bin/Release/netcoreapp2.2/publish does not exists on the machine on which you are running docker build
When you run docker build -t helloworld . it will copy all the file in . (i.e. the current directory from which you run the command) to the build context which will then be used when running COPY instructions, so you need to make sure you are running your command from the proper directory where files will be available to COPY
You can also run the same command from another directory by specifying the path to be used as build context such as docker build -t helloworld /path/to/my/dir

OCI runtime create failed: container_linux.go:296 - no such file or directory

End of my Dockerfile:
ENTRYPOINT ["ls /etc"]
Terminal:
...Rest of the building above is fine
Step 8/8 : ENTRYPOINT ["ls /etc"]
---> Using cache
---> ea1f33b8ab22
Successfully built ea1f33b8ab22
Successfully tagged redis:latest
k#Karls ~/dev/docker_redis (master) $ docker run -d -p 6379:6379 --name red redis
71d75058b94f088ef872b08a115bc12cece288b53fe26d67960fe139953ed5c4
docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"ls /etc\": stat ls /etc: no such file or directory": unknown.
For some reason, it won't find the directory /etc. I did a pwd and the current working directory is /. I also did a ls / on the entrypoint and that displayed the /etc directory fine.
OCI runtime create failed: container_linux.go:296
In my experience this is an error with the docker daemon itself, not the container you are trying to run. Try deleting all containers, restarting the daemon. I think we also had to clean up the docker networks.
I appear to be having the same issue. Here is what I am doing.
Dockerfile
FROM gcc:7.2.0
COPY src/ /usr/src/myapp
WORKDIR /usr/src/myapp
RUN set -x gcc -o myapp main.c
CMD ["./myapp"]
Build
$ docker build -t test .
Sending build context to Docker daemon 3.584kB
Step 1/6 : FROM gcc:7.2.0
...
---> 3ec35c7d2396
Successfully built 3ec35c7d2396
Successfully tagged test:latest
SECURITY WARNING: You are building a Docker image from Windows against a
non-Windows Docker host. All files and directories added to build context
will have '-rwxr-xr-x' permissions. It is recommended to double check and
reset permissions for sensitive files and directories.
Run
$ docker run -it test
D:\Docker Toolbox\docker.exe: Error response from daemon: OCI runtime create
failed: container_linux.go:296: starting container process caused "exec:
\"./myapp\": stat ./myapp: no such file or directory": unknown.
Changed CMD to ENTRYPOINT and removed the set -x seemed to resolve the problem. Though we are still unsure what the cause was or if this will also work for you.
Make sure that /etc exists or is created as the main.c wasn't compiling.
Dockerfile
FROM gcc:7.2.0
COPY src/ /usr/src/myapp
WORKDIR /usr/src/myapp
RUN gcc -o myapp main.c
ENTRYPOINT ["./myapp"]
On OSX, I fixed it by clearing the volume data manually. Close docker, and remove everything in ~/Library/Containers/com.docker.docker
I've expirienced the same issue after updating my Windows credentials, try following: Docker settings > Shared Drives > Reset credentials > Select drives again > Apply and re-enter your credentials. This solved the problem for me multiple times
The command you are trying to execute inside the container does not exist. In this case ls /etc does not exist in the image. There's a /bin/ls binary, but not a /bin/"ls /etc" binary, which itself would be invalid since the name of a file on the filesystem cannot include a /, though it can include a space.
Of course what you wanted to run was ls with the argument /etc, and for that, you need to separate each argument if you run with the exec syntax.
ENTRYPOINT ["ls", "/etc"]
Or if you wanted to allow a shell to parse the string, same way as if you were at a bash prompt inside the container running ls /etc on the command line, then switch to the string syntax that runs a shell:
ENTRYPOINT ls /etc

Docker- Image and Container to build and run an Executable file

Am new to Docker technologies, I have build an executable file in Visual Studio 2015 on Windows 10 machine.
Am looking to dockerize this executable file. I have build an Image and Container but the exe file is not being execute.
Here is my work so far:
Docker file:
FROM microsoft/aspnetcore:1.1
COPY . /app
WORKDIR /app
ENTRYPOINT ["dotnet","Controller.exe"]
CMD ["Controller.exe","RUN"]
Command used to create Image:
docker build -t imagename:version
Command used to create controller:
docker run -it --name Controller_name Image_ID run
Am trying to start the controller, but the controller is not starting
I have performed docker logs and here is the information in the log file.
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\app'.

Cannot start docker container from scratch

Nowadays I am trying to learn about the Docker but still have some ideas not clear;
I have tried to run an image that I just created, here is the directory;
hello (directory) :
Dockerfile
start.sh
And the Dockerfile :
FROM scratch
ADD start.sh /var/
CMD ["/var/start.sh"]
start.sh :
#!/bin/bash
echo "hello world"
I have tagged as using :
docker build -t mozer/hello .
Once I run the command;
docker run mozer/hello
no such file or directory
Error response from daemon: Cannot start container f22019aabc81f29fe17e849a2c040902ccadefe6cb8a8fe2612c83fe8eda40ea: [8] System error: no such file or directory
and once I run the command:
docker run mozer/hello /bin/sh -c
exec: "/bin/sh": stat /bin/sh: no such file or directory
Error response from daemon: Cannot start container 3b54584092e70b639671aca66122a0b1f6b1e4327cb2471a8792c3b2337b0bcc: [8] System error: exec: "/bin/sh": stat /bin/sh: no such file or directory
Can you please give me some ideas to find the solution ?
P.S. : I am working on a machine which is not connected to internet !
FROM scratch is a completely empty filesystem. You have no installed libraries, and no shell (like /bin/sh) included in there. To use this as your base, you'd need a statically linked binary, or you'll need to install all of the normal tools that are included with a linux distribution.
The latter is what is prepackaged in the various busybox, debian, ubuntu, centos, etc images on the docker hub. The fast way to make your image work with a minimal base image is to change the from to FROM busybox and change your /bin/bash to /bin/sh.
Scratch image is an empty file system, there is not /bin/sh inside it.
Look at Creating a simple base image using scratch
You can use Docker’s reserved, minimal image, scratch, as a starting
point for building containers. Using the scratch “image” signals to
the build process that you want the next command in the Dockerfile to
be the first filesystem layer in your image.
You can add an executable compiled program and run it.

Resources