How to run a private Docker image - docker

docker run -i -t testing bash
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"bash\": executable file not found in $PATH": unknown.
I created the image in Docker Hub , it is private image.
FROM scratch
# Set the working directory to /app
WORKDIR Desktop
ADD . /Dockerfile
RUN ./Dockerfile
EXPOSE 8085
ENV NAME testing
This is in my Dockerfile
I tired to run it, when i run docker images i am getting the details

I think you need to do login in command prompt.useing below command.
docker login -u username -p password url

Apart from the login which should not cause these, as you build an image on your local system which I assume it should exist on local system which will only pull image if not exist on local, the real reason is you are building an image from scratch and there are no binaries in scratch image, even no bash or sh.
Second mistake:
RUN ./Dockerfile
Your Dockerfile is a file, not binaries, while here you are trying to execute using RUN directive.
While scratch appears in Docker’s repository on the hub, you can’t
pull it, run it, or tag any image with the name scratch. Instead, you
can refer to it in your Dockerfile. For example, to create a minimal
container using scratch:
FROM scratch
COPY hello /
CMD ["/hello"]
While here hello can be an executable file such as a C++ compiled file.
Docker scratch image
But what I would suggest to say "hello" in Docker is to use Busybox or Alpine as a base image which has a shell and both are under 5MB.
FROM busybox
CMD ["echo","hello Docker!"]
now build and run
docker build -t hello-docker .
docker run --rm -it hello-docker

Related

How ro access docker volume files from the code on docker container

i have creted a docker volume with such command
docker run -ti --rm -v TestVolume1:/testvolume1 ubuntu
then i created a file there, called TestFile.txt and added text to it
Also i have a simple "Hello world" .net core app with Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0
COPY bin/Release/net6.0/publish/ ShareFileTestInstance1/
WORKDIR /ShareFileTestInstance1
ENTRYPOINT ["dotnet", "ShareFileTestInstance1.dll"]
I published it using
dotnet publish -c Release
then ran
docker build -t counter-image -f Dockerfile .
And finally executed
docker run -it --rm --name=counter-container counter-image -v TestVolume1:/testvolume1 ubuntu
to run my app with a docker volume
So what i want to achive to access a file which is in a volume("TestFile.txt" in my case) from a code in the container.
for example
Console.WriteLine(File.Exists("WHAT FILE PATH HAS TO BE HERE") ? "File exists." : "File does not exist.");
Is it also possible to combine all this stuff in a Dockerfile? I want to add one more container next and connect to the volume to save data there.
The parameters for docker run can be either for docker or for the program running in the docker container. Parameters for docker go before the image name and parameters for the program in the container go after the image name.
The volume mapping is a parameter for docker, so it should go before the image name. So instead of
docker run -it --rm --name=counter-container counter-image -v TestVolume1:/testvolume1 ubuntu
you should do
docker run -it --rm --name=counter-container -v TestVolume1:/testvolume1 counter-image
When you do that, your file should be accessible for your program at /testvolume1/TestFile.txt.
It's not possible to do the mapping in the Dockerfile as you ask. Mappings may vary from docker host to docker host, so they need to be specified at run-time.

Is it possible to create a docker image that just contains non executable files copied from host?

Is it possible to create an image of the contents of a folder on host. And later extract the content from image to the machine?
If so how?
Here is my failed attempt:
Dockerfile
WORKDIR '/data'
COPY ./hostfolder .
Command executed:
docker build -t mydata .
Folder structure:
Error:
Sending build context to Docker daemon 3.584kB
Error response from daemon: No build stage in current context
Yes, you can use a docker image as a place to store and then extract files.
First, you are missing a FROM directive in your Dockerfile. This is the reason for your error:
FROM alpine
WORKDIR '/data'
COPY . .
Then, to build the image:
$ docker build -t temp .
Then, to extract files, start the container:
$ docker run --detach --name data temp
and copy from the container to the host:
$ docker cp data:/data ./result

How to run docker commands using a docker file

I've certain basic docker command which i run in my terminal. Now what i want is to use all the basic docker commands into one docker file and then, build that docker file.
For eg.
Consider two docker files
File - Docker1, Docker2
Docker1 contains list of commands to run
And inside Docker2 i want to build Docker1 and run it as well
Docker2:(Consider the scenario with demo code)
FROM ubuntu:16.04
MAINTAINER abc#gmail.com
WORKDIR /home/docker_test/
RUN docker build -t Docker1 .
RUN docker run -it Docker1
I want to do something like this. But it is throwing - docker: error response from daemon oci runtime create failed container_linux.go
How can I do this? Where am I going wrong
P.S - I'm new to Docker
Your example is mixing two steps, image creation and running an image, that can't be mixed that way (with a Dockerfile).
Image creation
A Dockerfileis used to create an image. Let's take this alpine3.8 docker file as a minimal example
FROM scratch
ADD rootfs.tar.xz /
CMD ["/bin/sh"]
It's a base image, it's not based on another image, it starts FROM scratch.
Then a tar file is copied and unpacked, see ADD and the shell is set as starting command, see CMD. You can build this with
docker build -t test_image .
Issued from the same folder, where the Dockerfile is. You will also need the rootfs.tar.xz in that folder, copy it from the alpine link above.
Running a container
From that test_image you can now spawn a container with
docker run -it test_image
It will start up and give you the shell inside the container.
Docker Compose
Usually there is no need to build your images over and over again before spawning a new container. But if you really need to, you can do it with docker-compose. Docker Compose is intended to define and run a service stack consisting of several containers. The stack is defined in a docker-compose.yml file.
version: '3'
services:
alpine_test:
build: .
build: . takes care of building the image again before starting up, but usually it is sufficient to have just image: <image_name> and use an already existing image.

How to add the file in docker

I am new to docker, I installed docker as per the instructions provided in the official site.
# build docker images
docker build -t iky_backend:2.0.0 .
docker build -t iky_gateway:2.0.0 frontend/.
Now, while I am running these commands in the terminal after the installation of docker, I am getting the below error. I tried with by adding sudo also. But no use.
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/esh/Dockerfile: no such file or directory
Your docker images should execute just fine (may require sudo if you are unable to connect to docker daemon).
docker build requires a Dockerfile to present at the same directory (you are executing at your home folder - dont do that) or you need to use -f to specify the path instead of .
Try this:
mkdir build
cd build
create your Dockerfile here.
docker build -t iky_backend:2.0.0 .
docker images

Attaching to a docker container with base image scratch?

I have a docker file as follows:
FROM scratch
ARG VERSION=NOT_SET
ENV VERSION $VERSION
COPY foobar foobar
COPY foobar-*.yaml /etc/
COPY jwt/ /etc/jwt/
EXPOSE 8082
ENTRYPOINT ["./foobar"]
CMD ["-config", "/etc/foobar-local.yaml"]
Now, docker ps shows the following:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
653a9b287eb6 7693481.dkr.ecr.us-east-1.amazonaws.com/foobar:0.0.1 "./foobar -config /e" About a minute ago Up About a minute foobar
When I try to exec to this container with the following command:
sudo docker exec -it 653a9b287eb6 /bin/bash
it shows the following error:
rpc error: code = 2 desc = oci runtime error:
exec failed: exec: "/bin/bash": stat /bin/bash: no such file or directory
You need to add a shell to your empty base image (SCRATCH) in order to attach to it.
Right now, your image only include an executable, which is not enough.
As mentioned in issue 17896
FROM scratch literally is an empty, zero-byte image / filesystem, where you add everything yourself.
See for example, the hello-world which, produces an image that's 860 bytes total.
If you need a shell to attach to it through docker exec, start from a small image like Alpine (which has only /bin/sh though: you would need apk add bash to add bash, as commented below by user2915097).
If you are using Kubernetes, I have a repo that installs busybox on FROM scratch -image: https://github.com/phzfi/scratch-debug
In principle, by using the same process, you can also install busybox and shell on any other docker container, but I need to still make a script for debugging e.g. docker-compose and swarm orchestrators.

Resources