Docker - 3
How many ways do we have to create image?
1) when you pull the image from docker hub.
2) when you work on existing container, make a new image from existing and working container and use in future.
3) use docker file to create a container/image
--> login into AWS account and start your EC2 instance access it from putty
--> now we have to create container from our own image
therefore, create one container first
--> docker run -it --name bhupicontainer ubuntu /bin/bash
--> cd tmp/
now create one file inside this tmp directory
-> touch myfile
Now if you want to see the difference between the base image and changes on it then
--> docker diff bhupicontainer
o/p -> C /root
A /root/.bash-history
C /tmp
A /tmp/myfile
D - deletion
C - change
A - append
Now create image of this container
docker commit newcontainer updateimage(new image name)
--> docker images
Now create container from this image
-------------------------------------
docker run -it --name rajcontainer updateimage /bin/bash
root@containerid# ls
#cd /tmp/
# ls
0/p --> myfile [you will get all files back}
--------------------------------------------------
Dockerfile
----------
-> Dockerfile is basically a test file it contains some set of instruction.
-> Automation of Docker image creation.
notes: D should be capital in "Dockerfile"
Docker components should be in capital letter like "FROM RUN COPY" etc.
Docker Components
-----------------
FROM -> for base image, this command must be on top of the dockerfile.(like i am creating a base image like on ubuntu)
RUN -> To excute commands, it will create a layer in image.
MAINTAINER -> Author/Owner/Description
COPY -> Copy files from local system (docker VM) we need to provide source, destination.
(We can not download file from internet and any remote repo.)
ADD -> Similar to COPY but, it provides a feature to download files from internet, also we extract file at docker image side.
EXPOSE -> To expose ports, such as port 8080 for tomcat, port 80 for nginx etc.
WORKDIR -> To set working directory for a contianer.
CMD -> Execute commands but during container creation.(inside the container)
Enteypoint -> Similar to CMD, but has higher priority over CMD, first commands will be executed by ENTRYPOINT only.
ENV -> Environment Variables.(suppose myname = bhupinder then wherever we got myname it will be replaced by bhupinder. it just like a key = value.)
ARG -> (Arguement) - ARG instruction defines a variable that can be passed at build time. Once it is defined in the Dockerfile you can pass with this flag --build-arg while building the image. We can have multiple ARG instruction in the Dockerfile
ARG are also known as build-time variables. They are only available from the moment they are ‘announced’ in the Dockerfile with an ARG instruction up to the moment when the image is built. Running containers can’t access values of ARG variables.
---------------------------------------------------
Dockerfile
1) Create a file named Dockerfile.
2) Add instructions in Dockerfile.
3) Build dockerfile to create image.
4) Run image to create container.
Vi Dockerfile
FROM ubuntu
RUN echo "Technical guftu" >/tmp/testfile
To create image out of dockerfile
-----------------------------------
docker build -t myimg .
docker ps -a
docker images
Now, create container from the above image.
docker run -it --name mycontainer myimg /bin/bas
-> Cat /tmp/testfile
Create new Dockerfile
---------------------
vi Dockerfile
FROM ubuntu
WORKDIR /tmp
RUN echo "Susbcribe Technical guftugu > /tmp/testfile
ENV myname bhupinderrajput
COPY testfile1 /tmp
ADD test.tar.gz /tmp
once you build the image from dockerfile then you can reuse the same dockerfile with new instruction and create a new image. once it created then there will no problem in old image if we rework on it.
Comments
Post a Comment