Dockerfile contains instructions
on how the image should be built. Here are some of the most common instructions that you can meet in a Dockerfile:python
FROM
is used to specify a base image
for this build. It's similar to the builder configuration which we defined in a Packer template, but in this case instead of describing characteristics of a VM, we simply specify a name of a container image used for build. This should be the first instruction in the Dockerfile. ADD
and COPY
are used to copy a file/directory to the container. See the difference between the two.
ADD
allows <src>
to be an URL<src>
parameter of ADD
is an archive in a recognised compression format, it will be unpacked ENV
sets an environment variable available within the container.WORKDIR
changes the working directory of the container to a specified path. It basically works like a cd
command on Linux.VOLUME
command will mount a directory inside your container and store any files created or edited inside that directory on your hosts disk outside the container file structure, bypassing the union file system.The idea is that your volumes can be shared between your docker containers and they will stay around as long as there's a container (running or stopped) that references them.The fundamental difference between VOLUME
and -v
is this: -v
will mount existing files from your operating system inside your docker container and VOLUME
will create a new, empty volume on your host and mount it inside your container.docker run -v /<container_dirctory> <image name> // will create a volume under <container_directory> inside of the container, and it will presist if the container stop. git
docker run -v <host machine directory>:/<container_dirctory> <image name> // will mount the dir on hostmachie <host machine directory> on container dir <container_dirctory>, the files will be shared between the host and the containergithub
docker run -it --volumes-from container1 --name container2 busybox // create the 2nd container which will share the volums from the first container
<instruction> <command>
docker
e,g shell
ENV name John Dow ENTRYPOINT echo "Hello, $name"
when container runs as docker run -it <image>
will produce outputapp
Hello, John Dow
This is the preferred form for CMD and ENTRYPOINT instructions.ide
<instruction> ["executable", "param1", "param2", ...]
ui
When instruction is executed in exec form it calls executable directly, and shell processing does not happen. For example, the following snippet in Dockerfilethis
ENV name John Dow ENTRYPOINT ["/bin/echo", "Hello, $name"]
when container runs as docker run -it <image>
will produce outputgoogle
Hello, $name
Note that variable name is not substituted.
RUN
is used to run a command inside the image. Mostly used for installing packages and construct the images. can do somthing like RUN pip install -r requirements.txt // requirements.txt incldues all the python package requried
CMD
sets a default command, which will be executed when a container starts. This should be a command to start your application. http://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/
Prefer ENTRYPOINT to CMD when building executable Docker image and you need a command always to be executed. Additionally use CMD if you need to provide extra default arguments that could be overwritten from command line when docker container runs.
Choose CMD if you need to provide a default command and/or arguments that can be overwritten from command line when docker container runs.
(git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples )
In the directory which includes Dockerfile run
export PROJECT_ID="$(gcloud config get-value project -q)"
docker build -t gcr.io/${PROJECT_ID}/hello-app:v1 .
it will build the image using the Dockerfile
in the current directory and tag it with a name, such as gcr.io/my-project/hello-app:v1
. The gcr.io
prefix refers to Google Container Registry, where the image will be hosted
gcloud auth configure-docker
gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v1
To push image to Github
> docker login
for existing images
> docker tag local-image:tagname <<hub-user>/<reponame>:tagname
for new build images
>docker build -t <hub-user>/<repo-name>[:<tag>]
To push to repo
docker push <hub-user>/<repo-name>:<tag>
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
e.gdocker commit c3f279d17e0a svendowideit/testimage:version3