Install Docker on a linux machine
- Create a linux vm
- AWS EC2 Refer Here
- Azure VM Refer Here
- Softwares to be installed on your system
- Refer Here
- Windows Terminal Refer Here
- Installing latest version of docker on linux
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
* After installing docker, the connection between client and docker engine is not working due to permission issue
* Option 1: run docker as root user
* Option 2: Add current user to the docker group
sudo usermod -aG docker <username>
# exit and relogin
Image Building process
- overview:
Manual approach
- To build a image that runs our application, we need to know the manual steps of running our application.
- In this case, lets consider the application to be spring pet clinic.
- runtime: Java 11 (JDK 11)
- software package Refer Here
- runs on port 8080
- To build a image we will be using existing image, When we pick the existing image for our application
- option 1: take a image and install runtime and download package
- option 2: take a image which already has a runtime & download and run application.
- We have opted for option 2 and chosen
openjdk:11
(image:tag). - Lets create a container and login into it
docker container run -p <port of linux>:<port of application> -it <image>:<tag> <terminal>
docker container run -p 8080:8080 -it openjdk:11 /bin/bash
* Now exit after downloading the application
* Now in this container we have spring petclinic
* Now lets create image from container
Automated Approach
- We need to create Dockerfile.
- The Dockerfile has set of instructions
<INSTRUCTION> VALUE
- Sample Dockerfile
FROM openjdk:11
LABEL author=khaja
RUN wget https://referenceapplicationskhaja.s3.us-west-2.amazonaws.com/spring-petclinic-2.4.2.jar
EXPOSE 8080
CMD ["java", "-jar", "/spring-petclinic-2.4.2.jar"]
- In the above Dockerfile the instructions are
- FROM
- LABEL
- RUN
- EXPOSE
- CMD
- This Dockerfile when executed with
docker image build -t <image>:<tag> <directory with docker file>
will try to create a container based on base image and run all the necessary instructions and creates the image - When we start the container from image whatever is mentioned in CMD/ENTRYPOINT gets executed.
- Lets try to create one more application
gameoflife
. This application requires- server: tomcat
- runtime: java 8
- port 8080
- Download gameoflife into webapps
https://referenceapplicationskhaja.s3.us-west-2.amazonaws.com/gameoflife.war
- Refer Here for the tomcat image
FROM tomcat:9.0.64-jdk8-temurin-focal
LABEL author=khaja
LABEL why=forfun
RUN curl https://referenceapplicationskhaja.s3.us-west-2.amazonaws.com/gameoflife.war --output /usr/local/tomcat/webapps/gameoflife.war
EXPOSE 8080
-
Lets build to create image
- Refer Here for Dockerfile Instructions