Who creates the writable Layer?
- Refer here for more info
- Writable layers are created by Storage Drivers
- Storage Drivers:
- overlay2: Storage driver on all linux flavors
- aufs : Was a recommended driver for ubuntu earlier
- devicemapper: Was a recommended driver for centos & RHEL earlier
- zfs and btrfs: use these drivers when you write a lot
Docker Manual Installation
- Followed instructions from here
- When docker is installed a group called as docker is created. Docker daemon access will be given to this group
- Lets add user to group docker and then exit and reconnect
sudo usermod -aG docker ubuntu
Docker CLI
- Refer Here
- One more stuff would use any docker cheatsheet.
Lets Create Docker Image
- This step is also called as containerization of applications
- Scenario 1:
- Build a container for spring pet clinic
- Manual Steps
- Install java8
sudo apt-get update && sudo apt-get install openjdk-8-jdk
- download spring pet clinic jar file
wget https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar
- Execute jar file by using following command
java -jar spring-petclinic.jar
- Build a docker image for spring petclinic
- Two possible base images are
- ubuntu
- Search for ubuntu in DockerHUb
- Create a directory with a Dockerfile
FROM ubuntu:18.04
- ADD Metadata to the Dockerfile, we will be using LABEL
FROM ubuntu:18.04
LABEL Author="Khaja"
LABEL Org="QT"
- Lets install java in ubuntu. For that we need to execute the following command ‘apt-get update && apt-get install openjdk-8-jdk -y’
FROM ubuntu:18.04
LABEL Author="Khaja"
LABEL Org="QT"
RUN apt-get update
RUN apt-get install openjdk-8-jdk -y
- Writing multiple run statements lead to creation of multiple image layers. so to optimize it lets use &&
FROM ubuntu:18.04
LABEL Author="Khaja"
LABEL Org="QT"
RUN apt-get update && apt-get install openjdk-8-jdk -y
ADD https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar /spring-petclinic.jar
docker image build -t spc:ubuntu .
- Lets create a container from this image & check the process
docker container run spc:ubuntu
docker container ls
# container is not shown
docker ps -a
- As of now the problem is container is getting stopped/killed
- To recover we need to execute a command during the process of creating container from image. This command should be alive or inwork as long as your container is alive.
- we observed manually java -jar spring-petclinic.jar exactly does the same thing.
- To acheive this in docker we add CMD instruction
FROM ubuntu:18.04
LABEL Author="Khaja"
LABEL Org="QT"
RUN apt-get update && apt-get install openjdk-8-jdk -y
ADD https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar /spring-petclinic.jar
CMD ["java", "-jar", "/spring-petclinic.jar" ]
- building the image & running the container will solve the problem of container being killed. But to access your app you need 8080 port to be accessible. For this we use EXPOSE instruction and in docker container creatiton we use Port Forwarding
FROM ubuntu:18.04
LABEL Author="Khaja"
LABEL Org="QT"
RUN apt-get update && apt-get install openjdk-8-jdk -y
ADD https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar /spring-petclinic.jar
EXPOSE 8080
CMD ["java", "-jar", "/spring-petclinic.jar" ]
- Lets run the container after building the image
docker image build -t spc:ubuntufinal
docker container run -p 8088:8080 -d spc:ubuntufinal
docker container run -P -d spc:ubuntufinal
# port of host will be decided dynamically
- If java is installed on the base image
- By using the openjdk as the base image
FROM openjdk:8
LABEL Author="Khaja"
LABEL Org="QT"
ADD https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar /spring-petclinic.jar
EXPOSE 8080
CMD ["java", "-jar", "/spring-petclinic.jar" ]
- Now run the container after building the image and check it.
- using alpine jdk to optimize image size
FROM openjdk:8u212-jdk-alpine3.9
LABEL Author="Khaja"
LABEL Org="QT"
ADD https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar /spring-petclinic.jar
EXPOSE 8080
CMD ["java", "-jar", "/spring-petclinic.jar" ]
Compare sizes of the docker image by executing docker images
Like this:
Like Loading...