Access docker build context inside dockerfile - docker

I have multiple projects/folders inside a single directory called root. There is a common Dockerfile that runs all projects/folders. I run the projects passing different build context in docker build command as below:
$ docker build -t project1:1.0.0 -f . root/project1
$ docker build -t project2:1.0.0 -f . root/project2
$ docker build -t project2:1.0.0 -f . root/project3
Now, I need to add some conditions based on docker build context in dockerfile. Can that be done? I didn't find a way to get docker build context.

I think you can pass the project name to the Dockerfile as an argument and build for instance:
ARG ENV
RUN if [ "$ENV" = "production" ] ; then yarn client:build:prod ; else yarn client:build ; fi
finally:
docker build -t node-image . --build-arg ENV=production

Related

Managing Development and Production Dockerfiles

This is my current setup for my project and I was wondering if there was a more elegant way. The current setup is as follows.
Directory structure
<root>
- Dockerfile_base # base image for the other two
- Dockerfile_dev # development image
- Dockerfile_prod # production image
- Makefile
The Dockerfiles:
# Dockerfile_base
FROM tensorflow/tensorflow:2.4.1-gpu
RUN pip install ...
# Dockerfile_dev
FROM eu.gcr.io/cool_project/cool_program_base:latest
RUN pip install <dev branch of this repo>
# Dockerfile_prod
FROM eu.gcr.io/cool_project/cool_program_base:latest
RUN pip install <master branch of this repo>
Makefile
deploybase:
docker build -f Dockerfile_base -t cool_program_base:latest .
docker tag cool_program_base:latest eu.gcr.io/cool_project/cool_program_base
docker push eu.gcr.io/cool_project/cool_program_base
deploydev:
docker build -f Dockerfile_dev -t cool_program_dev:latest .
docker tag cool_program_dev:latest eu.gcr.io/cool_project/cool_program_dev
docker push eu.gcr.io/cool_project/cool_program_dev
deployprod:
docker build -f Dockerfile_prod -t cool_program_prod:latest .
docker tag cool_program_prod:latest eu.gcr.io/cool_project/cool_program_prod
docker push eu.gcr.io/cool_project/cool_program_prod
Q1: Is there a way to combine the three Dockerfiles into a single one? I know that there are multistage builds but I could not find how to make this work.
Q2: If it is possible, can the Makefile also be written more compactly?
For the docker images, you could use build-args, that is having a single parametrized Dockerfile:
ARG BRANCH=dev-branch
FROM eu.gcr.io/cool_project/cool_program_base:latest
RUN pip install $BRANCH
Then:
docker build -f Dockerfile --build-arg BRANCH=master -t cool_program_prod:latest .
or
docker build -f Dockerfile --build-arg BRANCH=dev -t cool_program_dev:latest .
You actually do not need to push "cool_program_base" image, because its layers are already included in both of the dev and prod images.

docker build --build-arg with github password is not working

When my Dockerfile was like below, it was working well.
...
RUN pip install git+https://user_name:my_password#github.com/repo_name.git#egg=repo_name==1.0.0
...
But when I changed Dockerfile to the below
...
RUN pip install git+https://user_name:${GITHUB_PASSWORD}#github.com/repo_name.git#egg=repo_name==1.0.0
...
And used the command below, it's not working.
docker build -t my_repo:tag_name . --build-arg GITHUB_PASSWORD=my_password
You need to add an ARG declaration into the Dockerfile:
FROM ubuntu
ARG PASSWORD
RUN echo ${PASSWORD} > /password
Then build your docker image:
$ docker build -t foo . --build-arg PASSWORD="foobar"
After this, you can check for the existence of the parameter in your docker container:
$ docker run -it foo bash
root#ebeb5b33941e:/# cat /password
foobar
Therefore, add the ARG GITHUB_PASSWORD build arg into your dockerfile to get it to work.

Docker Build with ARG not passing

Docker file:
# copy over system jboss configs
ARG SYSTEM_TYPE
COPY $SYSTEM_TYPE/AP/standalone.conf $JBOSS_HOME/bin/standalone.conf
COPY $SYSTEM_TYPE/AP/standalone-ha-bob.xml $JBOSS_HOME/bin/standalone-ha-bob.xml
Docker Command:
docker build --build-arg SERVER_TYPE=jbossconf/ENT-UAT/ -f /usr/etc/repos/docker-files/test-dockerfile-app -t appserver/test:1.0 .
Docker Build Result:
Step 15 : COPY $SYSTEM_TYPE/AP/standalone.conf $JBOSS_HOME/bin/standalone.conf
lstat AP/standalone.conf: no such file or directory
The --build-arg does not appear to be passing to the dockerfile. Where am I going wrong?
On your build args you are passing SERVER_TYPE and referencing $SYSTEM_TYPE on your dockerfile, that should fix it!

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 .

docker build is not work

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.

Resources