Docker contd
Layers in Docker Image
- Docker image is collection of read only image layers and meta-data
Experiments
- Pull the alpine:3.17 image and inspect and focus on layers
docker image pull alpine:3.17
docker image inspect alpine:3.17
* Pull the jenkins/jenkins image and inspect and focus on layers
docker image pull jenkins/jenkins
docker image inspect jenkins/jenkins
* Use this Dockerfile and build the image with name exp:1.0
FROM alpine:3.17
* Now both alpine and exp:1.0 have same layers used
* Now consider the below dockerfile with name exp:1.1
FROM alpine:3.17
ADD https://referenceapplicationskhaja.s3.us-west-2.amazonaws.com/gameoflife.war /gameoflife.war
* Now as observed a new layer was created in exp:1.1
* Now build the exp:1.2 with the following Dockerfile
FROM alpine:3.17
ADD https://referenceapplicationskhaja.s3.us-west-2.amazonaws.com/gameoflife.war /gameoflife.war
RUN mkdir test
RUN cp /gameoflife.war /test/gameoflife.war
* Writing too many RUN instructions is not a good idea, so combine them. Use \
to increase Readability
FROM alpine:3.17
ADD https://referenceapplicationskhaja.s3.us-west-2.amazonaws.com/gameoflife.war /gameoflife.war
RUN mkdir test && \
cp /gameoflife.war \
/test/gameoflife.war \
&& java -version > version.txt
- Docker container when created will union all the image layers and one writable layer to create a logical disk. This is done by Storage Drivers. The most popular one is
overlay2
.
- Refer Here for the article explaining layers and drivers
- When we delete the container writable layer is deleted. To solve this we will learn docker volumes.
ENTRYPOINT and CMD in docker files
- Docker container when starting will use the following to formulate the start up command
ENTRYPOINT CMD # Both Entrypoint and CMD exists in Docker
ENTRYPOINT
CMD
- Whatever we have written in CMD can be overriten by passing arguments after image name during run/create
- The Dockerfile used is
FROM alpine:3.17
CMD ["sleep", "1d"]
- The Dockerfile is
FROM alpine:3.17
ENTRYPOINT ["sleep"]
CMD ["1d"]
* To chage entrypoint
* Difference between fork vs exec linux
* Difference between shell form and exec form in Dockerfile
* references
* Refer Here
* Refer Here
- Problem: Docker container deletes the writable layer when deleted i.e. all the changes done by the contianer is lost
Docker volumes
- Refer Here for blog article
- We can be explicit and create volumes when our container is created automatically by adding
VOLUME
instruction in dockerfile
- Now create the container
some-mysql
and look at volumes
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
* Now create a volume and mount it it mysql with same specific-mysql