How do you create docker image?
There are multiple ways to create new docker images- docker commit: creates a new layer (and a new image) from a running container.
- docker build: performs a repeatable build sequence using dockerfile.
- docker import: loads a tarball into Docker engine, as a standalone base layer.
We don't prefer to use docker import, because the import option can be used for various hacks, but its main purpose is to bootstrap the creation of base images.
Working on Docker Custom Image
Docker images can be pulled from Docker registries. Docker owns Docker Hub and Docker cloud public repositories where you could find the free images. Docker store is a place where Docker images could be on sale. You can create an image and sell it who need it. Docker client is CLI which allows us to deal with the Docker objects. Docker objects are Docker images, Containers, Network, volumes and services. An image is a read-only template that allows us to create instances in runtime containers.Dockerfile structure |
We can create an image from a base image by adding some customizations like installing new libraries, software. For example, using oraclelinux as base image on top it installs httpd.
Here base image could be available in the public registry or custom image build for your project.
The easiest way to customize our docker image using Dockerfile, Where Dockerfile includes instructions to create layers in your docker image. Let's explore how to build our own image with dockerfile with syntax
How to create an efficient image via a Dockerfile?
- Start with an appropriate base image
- Do NOT Combine multiple applications into a single container
- Avoid installing unnecessary packages
- Use multi-stage builds
Dockerfile format
The dockerfile is a construct of a set of instructions to build the docker image. The dockerfile starts with comments where every scripting language allows us to use '#'(hash symbol) to comment a line. Next base image FROM where the image can be pulled. Here we have two options Docker Private registry for your organization otherwise Docker Hub public cloud which can be used for the initial learnings or for Proof of Concepts to built.Create a Dockerfile in any editor, my preferrable editor is VisualStudio Code, which gives me nice syntax highlighting for Dockerfile instructions.
# This is first sample Dockerfile FROM busybox LABEL AUTHOR Pavan Devarakonda RUN echo "Building sample docker image" CMD echo "Hello Vybhava Container!"
To create the docker image following command:
docker build -t hello .Here the important thing is that Dockerfile don’t follow any root directory you need to specify at the end of the build command in the above we have used dot(.) that is the current directory. You have other option too you can define other PATH as per your needs of project team collaboration.
Docker build command best options
docker build -f /path/to/a/Dockerfile . #absolute path docker build -t vybhava/myapp . #relative path of Dockerfile docker build -t vybhava/myapp:1.0 . # with tag label value
Usage of .dockerignorefile
To increase the build performance, exclude files and directories by adding a .dockerignore file to the context directory.
*.md !README.md *.tmp
Escape Parser Directive
Exploring Docker instructions
Let's examine each command in detail how it works.FROM Instruction
The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. Let's see an example# This is first sample Dockerfile FROM ubuntu CMD ["/usr/bin/wc","--help"]Ref: https://docs.docker.com/engine/reference/builder/#from
COPY and ADD instructions
copies all files and folders from current directory to /app folder in container-ImageCOPY . /app
copies everything from the web folder to container-Image /app/web
COPY ./web /app/web
copies specific file to /data/my-sample.txt file inside container-Image
COPY sample.txt /data/my-sample.txt
Compressed files such as tar, tar.gz, gz or zip files when copying to container-Image it will uncompresses it which can be any format.
ADD jdk-8u241-linux-x64.tar.gz /u01/app ADD APEX_21.1.zip /u01/
You can add files from the url to a container Image
ADD http://github.com/sample.txt /data/
EXPOSE
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. https://docs.docker.com/engine/reference/builder/#expose The LABEL instruction adds metadata to an image. A LABEL is a key-value pair. To include spaces within a LABEL value, use quotes and backslashes as you would in command-line parsing.Reference : https://docs.docker.com/engine/reference/builder/#label
RUN instruction
RUN has 2 forms:RUNThe RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.(shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows) RUN ["executable", "param1", "param2"] (exec form)
Reference: https://docs.docker.com/engine/reference/builder/#run
ENV instruction
The ENV instruction sets the environment variable 'key' to the value "value". Example:
ENV JDK_DIR /tmp ENV myvar=/home/tomcat ENV myvar=/home/apache2 var2=$myvar ENV var3=$myvar
The environment variables set using ENV will persist when a container is run from the resulting image.
WORKDIR instruction
Once the container comes to running state will switch to the directory which you mentioned as WORKDIR instruction. The value you pass it to WORKDIR is a PATH that should be existing in the container.Example: WORKDIR /tmp
ENTRYPOINT and CMD instructions
The ENTRYPOINT and CMD both the commands are executed run-time that is Container startup time. We cannont have multiple ENTRYPOINT or CMD commands in a Dockerfile
An ENTRYPOINT allows configuring a container that will run as an executable.
ENTRYPOINT has two forms:
Example1
ENTRYPOINT has two forms:
ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred) ENTRYPOINT command param1 param2 (shell form)
Example1
ENTRYPOINT with CMD params
# This is Entrypoint sample dockerfile FROM alpine LABEL MAINTAINER BHAVANISHEKHAR@GMAIL.COM ENTRYPOINT ["ping"] CMD ["-c","4","www.docker.com"]Build the image from the above instructions:
docker build -t entrycp:1.0 -f entrycmdparm.Dockerfile . #Run the container docker run entrycp:1.0Example 2:
ENTRYPOINT param with CMD param
# This is Entrypoint sample dockerfile FROM ubuntu LABEL USER Pavan ENTRYPOINT ["top", "-b"] CMD ["-c"]
dockerfile is a blue-print for docker image created with instructions |
No comments:
Post a Comment