Docker Image building using Dockerfile
- Docker images are built using Dockerfile. Official reference of Dockerfile
- Consider this Dockerfile
FROM openjdk:8
RUN wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
-
When we create a image using Dockerfile
docker image build -t hellodocker2 .
- A container using the base image gets created
- container with image openjdk:8 gets created
- Then some instructions such as RUN etc gets executed
- Copy of this container is created as a Docker image
- Image is created with the name specified in the command
- A container using the base image gets created
-
When we run a container using our created image (hellodocker2), then ENTRYPOINT/CMD gets executed
-
While building a docker image divide activities into following categories
- Base image with necessary software (if available)
- execute steps for configuring/deploying application
- steps for running your application
-
docker image build builds an image from Dockerfile and set of files specified in the PATH (Local directory) or URL (GIT Repository Location) which is called as context
docker image build -t hellodocker <PATH or URL>
-
Dockerfile is a standard name but if you want to write instruction in a different file that is possible
docker image build -t <imagetag> -f <path to Dockerfile>
Docker image conventions
- Docker image naming convention
[organization/]imagename:<tag>
openjdk:8
openjdk:9
openjdk => openjdk:latest
shaikkhajaibrahim/springpetclinic:1.0
- Default tag is latest
- Docker pull is downloading the image from docker public registry to local
docker image ls
docker image pull jenkins
docker image ls
docker image pull jenkins:alpine
docker image ls
Dockerfile
- Docker file is sequence of instructions in the format of
INSTRUCTION arguments
- Popular Instructions available are
- FROM
- This instructuion sets the base image
- Syntax
FROM <image>[:tag] [AS <name>]
- Examples:
FROM ubuntu FROM openjdk:8
- Best practice: always mention tag donot use latest
- also gives and optional way to specify platforms lick linux/amd64, linux/arm64, window/arm64 etc
FROM [--platform=<platform>] <image>[:tag] [AS <name>]
- RUN:
- This instruction execute the commands in the created base image container in a new layer
- Run command runs with /bin/bash -c on Linux and cmd on windows
- Syntax:
RUN <command> RUN ["executable", "param1", "param2"]
- Example:
RUN wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar RUN /bin/bash -c 'echo sample' RUN ["/bin/bash", "-c", "echo sample"]
- Commands have two forms
- shell =>
/bin/bash -c 'echo sample' java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
- exec => represent in square brackets
["/bin/bash", "-c", "echo sample"] ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
- ENTRYPOINT
- This is the command that gets executed when starting the container. if ENTRYPOINT is not found then CMD is executed when starting the container
- Syntax:
ENTRYPOINT ["executable", "param-1", "param-2" ] ENTRYPOINT executable param-1 param-2
- If both ENTRYPOINT and CMD Exists then ENTRYPOINT will be executable and CMD will be arguments
- CMD
- Syntax
CMD ["executable", "param-1", "param-2" ] CMD executable param-1 param-2 CMD param-1 param-2
- FROM
ENTRYPOINT and CMD Example
- Create a image with tag testing:1.0 with the following Dockerfile
FROM openjdk:8
RUN wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
- Create a image with tag testing:2.0 with the following Dockerfile
FROM openjdk:8
RUN wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
- Create a image with tag testing:2.0 with the following Dockerfile
FROM openjdk:8
RUN wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java"]
CMD ["-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
- Execute
docker image ls
- Now execute the following commands
docker container run -P -d testing:1.0
docker container run -P -d testing:2.0
docker container run -P -d testing:3.0
docker container ls
- To the container run command we can pass arguments
- ENTRYPOINT cannot be changed with args but whatever we write in CMD can be overwritten with args
# no impact on application startup
docker container run -P testing:1.0 echo helloworld
# startup is changed
docker container run -P testing:2.0 echo helloworld
# startup is notchanged but the arguments are changed
docker container run -P testing:3.0 echo helloworld
-
Next Instructions
-
LABEL
-
EXPOSE
-
ADD
-
VOLUME
-
WORKDIR
-
USER
-
ARG
-
ONBUILD
-
STOPSIGNAL
-
HEALTHCHECK
-
SHELL
-
ENV