Docker Instructions
- ADD: Refer Here for official docs
- Can download the contents from context/local folders as well as URLs, Recently support for git repos is also added
-
COPY: Refer Here for official docs
- Can download the contents from context/local folders
- Docker build context: This is set of the files located in specific PATH or URL specified during docker image build
Copy instruction
- Using copy to copy the local file
FROM alpine:3.18.2
LABEL author=shaikkhajaibrahim
ARG JAVA_PACKAGE=openjdk11-jdk
ARG USER=spc
ARG HOME_DIR=/spc
ARG USER_SHELL=/bin/sh
RUN apk update && \
apk add ${JAVA_PACKAGE} && \
echo ${JAVA_TEST}
RUN adduser -h ${HOME_DIR} -s ${USER_SHELL} -D ${USER}
USER ${USER}
WORKDIR ${HOME_DIR}
COPY --chown=${USER}:${USER} ./spring-petclinic-2.4.2.jar spring-petclinic-2.4.2.jar
EXPOSE 8080
ENTRYPOINT ["java"]
CMD ["-jar", "spring-petclinic-2.4.2.jar"]
- Now build the image
- If we use Add instruction i can directly use URL
FROM alpine:3.18.2
LABEL author=shaikkhajaibrahim
ARG JAVA_PACKAGE=openjdk11-jdk
ARG USER=spc
ARG DOWNLOAD_LOCATION=https://referenceapplicationskhaja.s3.us-west-2.amazonaws.com/spring-petclinic-2.4.2.jar
ARG HOME_DIR=/spc
ARG USER_SHELL=/bin/sh
RUN apk update && \
apk add ${JAVA_PACKAGE} && \
echo ${JAVA_TEST}
RUN adduser -h ${HOME_DIR} -s ${USER_SHELL} -D ${USER}
USER ${USER}
WORKDIR ${HOME_DIR}
ADD --chown=${USER}:${USER} ${DOWNLOAD_LOCATION} spring-petclinic-2.4.2.jar
EXPOSE 8080
ENTRYPOINT ["java"]
CMD ["-jar", "spring-petclinic-2.4.2.jar"]
* Lets try to build the docker image of some python app Refer Here
* Lets consider the following Dockerfile by using Add from git repo Refer Here
FROM python:3.7-alpine
LABEL author=KHAJA
LABEL blog=directdevops.blog
ARG HOME_DIR='/studentcourses'
ADD https://github.com/DevProjectsForDevOps/StudentCoursesRestAPI.git $HOME_DIR
ENV MYSQL_USERNAME='directdevops'
ENV MYSQL_PASSWORD='directdevops'
ENV MYSQL_SERVER='localhost'
ENV MYSQL_SERVER_PORT='3306'
ENV MYSQL_DATABASE='test'
EXPOSE 8080
WORKDIR $HOME_DIR
RUN pip install -r requirements.txt
ENTRYPOINT ["python", "app.py"]
- This was failing we will fix this
-
We have build the images directly using docker image build
Refer Here
- .DockerIgnore Refer Here
Running Docker Containers
- The storage in docker is based on union of layers, which is done by special storage drivers. Now docker majorly uses overlay as a storage driver.
- When the container is running the disk contents are union of all the read-only layers and one writable mounted as a single storage entity with the help of overlay
- When the docker container is removed then we loose writable layer
-
There are two kinds of applications
- Stateless: These applications donot store state locally rather they rely on external systems such as databases, external mounts etc.
- In modern applications all the microservices are stateless and they store state in some database
- Stateful: These are applications which use local storage to store some data.
- Stateless: These applications donot store state locally rather they rely on external systems such as databases, external mounts etc.
- To solve this problem with data loss, we have Docker volumes.
- Docker volumes ensure the data of the specific folder is persisted and has a life time beyond docker container