Creating my first docker image with a local zip - docker

very new to Docker here. I am trying to use a maven 2.1.0 zip to create a docker image.
my
dockerfile.docker file is :
# syntax=docker/dockerfile:1
FROM scratch
LABEL maintainer="Myname"
LABEL maintainer="myemail"
RUN wget HTTP://archive.apache.org/dist/maven/binaries/apache-maven-2.1.0-bin.zip
RUN unzip
I am not exactly sure if I am doing this right
docker build -t apache-maven:2.1.0 .
Essentially I just wanted to create this image locally so I could then push it out to my targeted endpoint. Any help realizing what I did wrong would be appreciated. Whenever I run this build command it tells me it failed to read the dockerfile and that there's no such file or directory.

By default, it will try to find the file with the exact name Dockerfile.
If for any reason, you want to have a different file name like your scenario, you should use next:
docker build -f dockerfile.docker -t apache-maven:2.1.0 .
Detail refers to Specify a Dockerfile (-f)

Related

How does to download and extract .tar files in a container for Dockerfile?

Over here is a use case - I want to download and extract all files from a particular website and allow users to specify from which workweek it might be done. Please, imagine using one docker command and specifying only the variable which tells where to go, download and extract files.
The problem is I want to allow a user to manipulate variables that refer to a particular workweek.
Now it is only my idea, not sure If I am thinking right before I start to design my Dockerfile.
Dockerfile:
...
ENV TARGET="$WW_DIR"
...
Now you can imagine that the first user wants to download files from WW17 so he can type:
docker container run -e TARGET=WW17 <image_name>
The second one wants to download files from WW25:
docker container run -e TARGET=WW25 <image_name>
Etc.
Underhood Dockerfile knows that it must go to the directory from WW17 (in the first scenario) or WW25 (in the second scenario). My imagination is that a new container is created then using for example "curl" files are downloaded from an external server and extracted.
Can you recommend to me the best methods with some examples of how to solve it? Apply bash script inside of the container?
Thanks.
There is no Dockerfile at docker container run, it just runs the command. So write a command that does what you want or add the data to the image when building it with Dockerfile.
# Dockerfile
FROM your_favourite_image
COPY your_script /
RUN chmod +x /your_script
CMD /your_script
# your_script
#!/usr/bin/env your_favourite_langauge_like_python_or_bash_or_perl
# download the $TARGET or whatever you want to do
And then
docker build -t image .
docker run -r TARGET=WW1 image
Reading: https://docs.docker.com/engine/reference/builder/#cmd https://docs.docker.com/engine/reference/builder/#entrypoint https://docs.docker.com/get-started/overview/ https://btholt.github.io/complete-intro-to-containers/dockerfile

How do I build a docker image that has the same tag as the base image?

I have a case where I want to create a docker image from some base image making a couple of modifications along the way (as you do) and I'm looking for some way to preserve the base image tag.
Let me elaborate with an example.
I want to build a new logstash image, with this Dockerfile:
FROM docker.elastic.co/logstash/logstash:5.5.2
RUN /opt/logstash/bin/logstash-plugin install logstash-input-jdbc
RUN mkdir /opt/logstash/vendor/jdbc
RUN curl -Lo /opt/logstash/vendor/jdbc/postgresql-42.1.1.jar https://jdbc.postgresql.org/download/postgresql-42.1.1.jar
I would like the resulting image to also end up with tag 5.5.2, just like the original base image.
I know I can simply tag it from the command line once the build is done, but I am looking for some way (if one exists) to either:
"extract" this tag from the Dockerfile, so i can use it in the docker build -t option
pass some variable into the Dockerfile that can be used in place of the 5.5.2 image tag
Thanks for your help & ideas!
You need to use build arguments in your docker file
ARG LOGSTASH_VERSION
FROM docker.elastic.co/logstash/logstash:${LOGSTASH_VERSION}
RUN /opt/logstash/bin/logstash-plugin install logstash-input-jdbc
RUN mkdir /opt/logstash/vendor/jdbc
RUN curl -Lo /opt/logstash/vendor/jdbc/postgresql-42.1.1.jar https://jdbc.postgresql.org/download/postgresql-42.1.1.jar
Then pass the value using docker build command line
docker build --build-arg LOGSTASH_VERSION=5.5.2 .....

How to run custom Docker file?

So here is a very cool Docker file.
To run it, I do:
wget https://cran.r-project.org/src/contrib/FastRCS_0.0.7.tar.gz
tar -xvzf FastRCS_0.0.7.tar.gz
docker run --rm -ti -v $(pwd):/mnt rocker/r-devel-ubsan-clang check.r --setwd /mnt -a --install-deps FastRCS_0.0.7.tar.gz
But now suppose I want to save this DockerFile and run the saved version from the current directory (i.e. not just the one on github).
How can I do this?
The idea is that I need to customize this DockerFile a bit and run the customized version.
Sounds like you want to download the raw file from https://raw.githubusercontent.com/rocker-org/r-devel-san-clang/master/Dockerfile
and save it into a file named Dockerfile
Then you could edit the file to make your changes, and then just build your image with docker build . when you are in the Dockerfile directory
This is a basic Docker usage question--look into docker commit.
You may want to study one of the many fine Docker tutorials out there.

docker: "build" requires 1 argument. See 'docker build --help'

Trying to follow the instructions for building a docker image from the docker website.
https://docs.docker.com/examples/running_redis_service/
this is the error I get will following the instructions on the doc and using this Dockerfile
FROM ubuntu:14.04
RUN apt-get update && apt-get install -y redis-server
EXPOSE 6379
ENTRYPOINT ["/usr/bin/redis-server"]
sudo docker build -t myrepo/redis
docker: "build" requires 1 argument. See 'docker build --help'.
How do resolve?
You need to add a dot, which means to use the Dockerfile in the local directory.
For example:
docker build -t mytag .
It means you use the Dockerfile in the local directory, and if you use docker 1.5 you can specify a Dockerfile elsewhere. Extract from the help output from docker build:
-f, --file="" Name of the Dockerfile(Default is 'Dockerfile' at context root)
In my case this error was happening in a Gitlab CI pipeline when I was passing multiple Gitlab env variables to docker build with --build-arg flags.
Turns out that one of the variables had a space in it which was causing the error. It was difficult to find since the pipeline logs just showed the $VARIABLE_NAME.
Make sure to quote the environment variables so that spaces get handled correctly.
Change from:
--build-arg VARIABLE_NAME=$VARIABLE_NAME
to:
--build-arg VARIABLE_NAME="$VARIABLE_NAME"
Did you copy the build command from somewhere else (webpage or some other file)? Try typing it in from scratch.
I copied a build command from an AWS tutorial and pasted it into my terminal and was getting this error. It was driving me crazy. After typing it in by hand, it worked! Looking closer and my previous failed commands, I noticed the "dash" character was different, it was a thinner, longer dash character than I got if I typed it myself using the "minus/dash" key.
Bad:
sudo docker build –t foo .
Good:
sudo docker build -t foo .
Can you see the difference?.. Cut and paste is hard.
In case anyone is running into this problem when trying to tag -t the image and also build it from a file that is NOT named Dockerfile (i.e. not using simply the . path), you can do it like this:
docker build -t my_image -f my_dockerfile .
Notice that docker expects a directory as the parameter and the filename as an option.
Use the following command
docker build -t mytag .
Note that mytag and dot has a space between them . This dot represents the present working directory .
Just provide dot (.) at the end of command including one space.
example:
command: docker build -t "blink:v1" .
Here you can see "blink:v1" then a space then dot(.)
Thats it.
You Need a DOT at the end...
So for example:
$ docker build -t <your username>/node-web-app .
It's a bit hidden, but if you pay attention to the . at the end...
From the command run:
sudo docker build -t myrepo/redis
there are no "arguments" passed to the docker build command, only a single flag -t and a value for that flag. After docker parses all of the flags for the command, there should be one argument left when you're running a build.
That argument is the build context. The standard command includes a trailing dot for the context:
sudo docker build -t myrepo/redis .
What's the build context?
Every docker build sends a directory to the build server. Docker is a client/server application, and the build runs on the server which isn't necessarily where the docker command is run. Docker uses the build context as the source for files used in COPY and ADD steps. When you are in the current directory to run the build, you would pass a . for the context, aka the current directory. You could pass a completely different directory, even a git repo, and docker will perform the build using that as the context, e.g.:
docker build -t sudobmitch/base:alpine --target alpine-base \
'https://github.com/sudo-bmitch/docker-base.git#main'
For more details on these options to the build command, see the docker build documentation.
What if you included an argument?
If you are including the value for the build context (typically the .) and still see this error message, you have likely passed more than one argument. Typically this is from failing to parse a flag, or passing a string with spaces without quotes. Possible causes for docker to see more than one argument include:
Missing quotes around a path or argument with spaces (take note using variables that may have spaces in them)
Incorrect dashes in the command: make sure to type these manually rather than copy and pasting
Incorrect quotes: smart quotes don't work on the command line, type them manually rather than copy and pasting.
Whitespace that isn't white space, or that doesn't appear to be a space.
Most all of these come from either a typo or copy and pasting from a source that modified the text to look pretty, breaking it for using as a command.
How do you figure out where the CLI error is?
The easiest way I have to debug this, run the command without any other flags:
docker build .
Once that works, add flags back in until you get the error, and then you'll know what flag is broken and needs the quotes to be fixed/added or dashes corrected, etc.
On older versions of Docker it seems you need to use this order:
docker build -t tag .
and not
docker build . -t tag
You can build docker image from a file called docker file and named Dockerfile by default. It has set of command/instruction that you need in your docker container.
Below command creates image with tag latest, Dockerfile should present on that location (. means present direcotry)
docker build . -t <image_name>:latest
You can specify the Dockerfile via -f if the file name in not default (Dockerfile)
Sameple Docker file contents.
FROM busybox
RUN echo "hello world"
Open PowerShelland and follow these istruction.
This type of error is tipically in Windows S.O.
When you use command build need an option and a path.
There is this type of error becouse you have not specified a path whit your Dockerfile.
Try this:
C:\Users\Daniele\app> docker build -t friendlyhello C:\Users\Daniele\app\
friendlyhello is the name who you assign to your conteiner
C:\Users\Daniele\app\ is the path who conteins your Dockerfile
if you want to add a tag
C:\Users\Daniele\app> docker build -t friendlyhello:3.0 C:\Users\Daniele\app\
The following command worked for me. Docker file was placed in my-app-master folder.
docker build -f my-app-master/Dockerfile -t my-app-master .
My problem was the Dockerfile.txt needed to be converted to a Unix executable file. Once I did that that error went away.
You may need to remove the .txt portion before doing this, but on a mac go to terminal and cd into the directory where your Dockerfile is and the type
chmod +x "Dockerfile"
And then it will convert your file to a Unix executable file which can then be executed by the Docker build command.
#Using a file other than Dockerfile instead.
#Supose my file is `Dockerfile-dev`
docker build -t mytag - < Dockerfile-dev
In my case I was using a dash (slightly longer hyphen) symbol – before the t option was the problem.
docker build –t simple-node .
Replace with a hyphen/ minus symbol.
docker build -t simple-node .
I got this error when using Docker with Jenkins pipeline within a pipeline script. The solution was to use this syntax in the pipeline script:
docker.build("[my_docker_image_tag]", "-f ./path/to/my/Dockerfile.jvm .")
Docker Build Command Format
In your powershell :
There is this type of error because you have not specified a path whith your Dockerfile.
Try this:
$ docker build -t friendlyhello:latest -f C:\\TestDockerApp\\Dockerfile.txt
friendlyhello is the name you assign to your container and add the version , just use the :latest
-f C:\TestDockerApp\Dockerfile.txt
- you want to add a tag because the build command needs a parameter or tag
- The DockerFile is a text document so explicitly add the extension .txt
**Try this format :
$ docker build -t friendlyhello:latest -f C:\\TestDockerApp\\Dockerfile.txt .**

Sending docker build contexts

My directory structure is as follows
cassandra
Dockerfile
downloads
225M file
I am inside cassandra directory. My build command is
docker build -t image_cassandra .
I know that it will send all the contents in . current directory. So it takes so much of time to send this 225M file. I need this file in my Dockerfile.
Add downloads/ /tmp/
I want to avoid this much of delay. And I know that, we cannot use ../ in docker ADD command. So is there any way to reduce the size of build context and have this ADD command.
This file is not part of web. So i cannot use any apt-get wget statements. Or isn't possible?
You could separate the project into two docker images, a big one that changes infrequently, and a small one that you can change fast.
Project1/
Dockerfile
bigfile
Project2/
Dockerfile
Project1/Dockerfile would look like this:
FROM ubuntu
RUN apt-get install cassandra
ADD bigfile
Then if you build it and tag it with docker build -t project1 Project1, you can use the result in Project2/Dockerfile:
FROM project1
RUN fast configuration commands

Resources