Create container from local file - docker

I have a file called Dockerfile which contains a Docker configuration.
If I try to build a container from the file:
docker build Dockerfile
I get:
unable to prepare context: context must be a directory: C:\Path\To\Dockerfile
So how can I create a Docker container from a local configuration file?

According to https://docs.docker.com/engine/reference/commandline/build/ the parameter to docker build is not the Dockerfile, but a directory or URL containing the Dockerfile. So your command should rather be
docker build .
or even better, considering docker defaults to the current dir:
docker build
If your Dockerfile happens do be named differently (it really shouldn't), you can tell docker to use it by passing it to the -f or --file option:
docker build -f MyDockerfile

Related

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

How to copy a file to a Docker container before starting/running it?

I'm starting with a docker image that is already built.
I would like to do this
Create a docker container from this image. (Don't start)
Copy a file to this container
Start the container
How can this be achieved. It looks like if i run the following commands the file doesn't end up in the container
docker create --name my_container my_image
docker cp file my_container:/tmp/file
docker start my_container
Any idea how this can be achieved ?
You will have to create a new image from a Dockerfile that inherit from the one that is already built, and the use the COPY tag:
Dockerfile
FROM my_image
COPY file /tmp/file
Finally, build that new Dockerfile:
$ docker build -t new_image .
Create a dir (e.g. init) put your sql or shell script in it.
bind mount the dir ~/init:/docker-entrypoint-initdb.d/

\Dockerfile: The system cannot find the file specified

I have created a docker file to use node.js and nginx. When I run docker -t build <my docker file name> . I get the following error:
Dockerfile: The system cannot find the file specified.
Then in the docker file directory I created a folder name web and place my index.html and style.css file in it.
Question is: Any idea why I am getting this error?
The command docker -t build <my docker file name> . is being misused. It should be:
docker build -t <image-name> -f dockerFile .
where dockerFile is the name you gave to the Dockerfile.
The -t option specifies the tag or name to give to the Docker image.
The -f must be used, if you name the Dockerfile something other than Dockerfile
The . specifies the docker build context.

Resources