docker build is not work - docker

I can't seem to get docker build to run correctly:
wangyaos-MBP-3:~ wangyao$ cd /Users/wangyao/Ozintel/docker/flexcloud/
wangyaos-MBP-3:flexcloud wangyao$ ls
Dockerfile apache-tomcat-7.0.62 jdk1.8.0_45.jdk
wangyaos--3:flexcloud wangyao$ docker build -t="Users/wangyao/Ozintel/docker/flexcloud" .
Invalid namespace name (Users). Only [a-z0-9-_] are allowed.
wangyaos-MBP-3:flexcloud wangyao$ cd /Users/wangyao/
wangyaos-MBP-3:~ wangyao$ docker build -t="Users/wangyao/Ozintel/docker/flexcloud" .
Cannot locate Dockerfile: Dockerfile
wangyaos-MBP-3:~ wangyao$ docker build -t="Users/wangyao/Ozintel/docker/flexcloud"
docker: "build" requires 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build a new image from the source code at PATH
How should I use docker build?

Slow down and take a look at the docs.
To use docker build, the easiest way is to cd into the directory with the Dockerfile then run something like:
$ docker build -t flexcloud .
The -t argument specifies the repository name and tag, not the directory with the Dockerfile. If you want to give a path to a different Dockerfile, you can use the -f argument. The . at the end specifies the "build context", in this case the current working directory.

Related

how to change docker source of the context of the build

Dockerfile is:
FROM nginx
COPY html /usr/share/nginx/html
Dockerfile is in pwd directory(which is home/ubuntu/app), when use following command:
docker build -t mynginx .
I was giving an error:
copy failed: stat /var/lib/docker/tmp/docker-builder344/html: no such file or directory.
how to change docker source of the context of the build?
Docker build only look files in Dockerfile file context, mean the build will copy /home/ubuntu/app/ from this location where your Dockerfile is.
So better to place your html the /home/ubuntu/app in this location as docker build send tar of the context to docker daemon so it is recommended to keep the context minimal.
docker build -t mynginx . would expect to find Dockerfile in the current directory. Moreover, relative paths used by Dockerfile instructions would be relative to the current directory.
You can set the build context path to a different path docker build -t mynginx [some_folder_path]. Docker would search for Dockerfile there. You can modify the path to Dockerfile using -f option docker build -f [path_to_dockerfile] -t mynginx [some_folder_path]

Why does Docker require PATH when building from custom Dockerfile in another directory?

When building docker images with a Dockerfile in the same directory, the following works every time
$ docker build -t project/app:latest .
Sending build context to Docker daemon 135.9MB
...
However, when using -f to specify a different Dockerfile to use, docker complains ...
$ docker build -t project/app:latest -f ../some/path/Dockerfile.other
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
I can easily provide the PATH, and the build will work, but why is the PATH still required if I'm specifying the absolute path to the Dockerfile with -f?
The PATH is for specifying the build context (the tree from which COPY instructions copy things), which need not have any relation to the location of the Dockerfile.
Quoting the docs:
The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

What does this command do?: `docker build . -f Dockerfile2 -t `

From my tutorial, it creates clone Dockerfile (Dockerfile2) and build the second Docker.
docker build . -f Dockerfile2 -t
But I don't understand what . does.
According to Docker documentation.:
-t : tag ...
-f : file...
What is this command doing? - Thanks
Obviously the tag is missing. Anyway you're telling Docker:
Hey Docker, the current directory is your build context so copy everything from this location (except the files and directories mentioned in the .dockerignore file) and build the image for me using the instructions from the Dockerfile2 file. Also, please tag it using the provided tag so I can reference it easily.
If you ommit the file (drop the -f argument), then the default Dockerfile file is assumed.
. means the current directory where you are. And the Dockerfile is in it. If you do not in the directory of the Dockerfile, you will get the error.
The full command : docker build path -f Dockfile -t containerName. Also the document docker build [OPTIONS] PATH | URL | -.
From the Docker build documentation:
Usage:
docker build [OPTIONS] PATH | URL | -
build: Build an image from a Dockerfile
.: Specifies that the PATH is ., and so all the files in the local directory get tar d and sent to the Docker daemon. The PATH specifies where to find the files for the “context” of the build on the Docker daemon.
--file , -f: Name of the Dockerfile (Default is ‘PATH/Dockerfile’).
--tag , -t: Name and optionally a tag in the ‘name:tag’ format. You can apply multiple tags to an image.
So, what is happening is: Docker I want to build an image from a Dockerfile called Dockerfile2 tagged with the value (you didn't set the value of the tag) in the current path.

Why is the docker image not building?

I was following the tutorial on the official website.
https://docs.docker.com/get-started/part2/#build-the-app
I created a directory named dir and added the Dockerfile, app.py and requirements.txt. When I try to build this, the error is-
root#ubuntu:~/dir# docker build -t hello
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | - [flags]
Build an image from a Dockerfile
You forgot to mention location of context-root
#>docker build -t hello .
Add a . at the end if Dockerfile is at the current location

How to build dockerfile

I have made images ubuntu 14:04 on dockerfile
I am running the syntax
$ sudo docker build -t mypostgres .
but I am still confused as to build the dockerfile
how to build it?
sudo docker build -t mypostgres . means:
process the file named 'Dockerfile' (default name)
located in the current folder (that is the final .)
and build as a result the image named mypostgres
So if you have a Dockerfile starting with FROM postgres, you can execute your command and have your own postgres image in no time.
Dockerfile is not as complex as it looks. here's a good start article that could help you to build your first docker file easily - http://rominirani.com/2015/08/02/docker-tutorial-series-writing-a-dockerfile/
You may want to read the doc of Dockerfile best practice by Docker, better than any article IMHO.
You can build a docker file direct from git repository or from a director.
to build a docker file first create a docker file inside your project and name it just Docker without any extension. Now inside that file write necessary command for building an image. For example
FROM node:alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY ./ ./
CMD ["npm", "start"]
->Build from git:
sudo docker build https://github.com/lordash/mswpw.git#fecomments:comments
in here:
fecomments is branch name and comments is the folder name.
->building from git with tag and version:
sudo docker build https://github.com/lordash/mswpw.git#fecomments:comments -t lordash/comments:v1.0
->Now if you want to build from a directory: first go to comments directory the run command sudo docker build .
->if you want to add tag you can use -t or -tag flag to do that:
sudo docker build -t lordash . or sudo docker build -t lordash/comments .
-> Now you can version your image with the help of tag:
sudo docker build -t lordash/comments:v1.0 .
->you can also apply multiple tag to an image:
sudo docker build -t lordash/comments:latest -t lordash/comments:v1.0 .

Resources