Required Softwares to be installed
Containerization Continued
- Build the package (jar/war/exe/dll) and docker image as part of docker image build
- Build Java => maven and to run the applciation i dont need the build tool
- Docker introduced multi staged build for this.
- Image building process is divided into stages, the docker image will contain only the last stage.
- Spring petclinic:
- Stage 1
# java 10, maven 3
# get the code
git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
mvn package
- Stage 2: Here we need jdk 11 and jar file: Refer Here for official docs
- Refer Here for the docker file with multistage build.
ENV and ARG
- ARG instruction is used to pass some value for the purpose of image building
- ENV instruction sets the environmental variable with value in the container.
- Lets assume the developer wants to pass the tag/branch of the code to be build.
- Refer Here for the official docs
- ARG Demo Refer Here
- ENV is used to set ENVIRONMENTAL variables in the container adn these variables can be changed when starting the container.
FROM alpine
ENV TEST='hello'
CMD ["set"]
- If there is any flexibility required at runtime when starting the application inside container we use ENV instruction which sets environmental variable.
- Best Example: Mysql container giving ENV variables to set usernames and password’s Refer Here
- to pass env
docker container run -e "TEST=hai" <image>
USER instruction
- This is used to set the default user in the container when the container is started/created.
- Before using this instruction user should be created
FROM openjdk:8-jdk
RUN apt-get update && apt-get install -y git curl && rm -rf /var/lib/apt/lists/*
ARG user=jenkins
ARG group=jenkins
ARG uid=1000
ARG gid=1000
ARG http_port=8080
ARG agent_port=50000
ENV JENKINS_HOME /var/jenkins_home
ENV JENKINS_SLAVE_AGENT_PORT ${agent_port}
# Jenkins is run with user `jenkins`, uid = 1000
# If you bind mount a volume from the host or a data container,
# ensure you use the same uid
RUN groupadd -g ${gid} ${group} \
&& useradd -d "$JENKINS_HOME" -u ${uid} -g ${gid} -m -s /bin/bash ${user}
USER ${user}
CMD ["echo", "hello"]
- Create a image and create a container with interactive terminal and check the user. The username for the above image should be jenkins
Experiments
- Lets create a simple docker image
FROM alpine
CMD ["echo", "hello"]
- create a image and run the following command to create container
- Now lets run the following command
- IN the following commadn
docker container run mytest:1.0 echo $PATH
, echo $PATH replaces the CMD in image - Consider the following dockerfile
FROM alpine
ENTRYPOINT ["echo"]
CMD ["hello"]
* When we start the docker container the command that the container runs is ENTRYPOINT + CMD
* When we pass additional options, CMD will be overriden not ENTRYPOINT
Exercise – 1
- Exercise: Create a docker image for simple flask application
- Steps:
git clone https://github.com/Sysnove/flask-hello-world.git
cd flask-hello-world
mv hello.py app.py
pip3 install flask
flask run -h "0.0.0.0"
Exercise -2
- Refer Here for the manual steps to install nop commerce