Sunday, July 30, 2023

Docker - what does docker run --restart always actually do?

Docker - what does docker run --restart always actually do?

Restart policies (--restart)

Use Docker’s --restart to specify a container’s restart policy. A restart policy > controls whether the Docker daemon restarts a container after exit. Docker supports the following restart policies:

always Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.

$ docker run --restart=always redis

Simple Container Commands To Create LAB Setup.

Simple Container Commands To Create LAB Setup.


docker network create --driver=bridge --subnet=172.19.0.0/16 MQMNTRK

docker volume create my-vol101
docker volume create my-vol201
docker volume create my-vol301
docker volume create my-vol401

docker run -it -d --privileged=true --restart=always --network MQMNTRK -v my-vol101:/u01 --ip 172.19.0.20 -h HOST01 --name BOX01 centos:latest /usr/sbin/init

docker run -it -d --privileged=true --restart=always --network MQMNTRK -v my-vol201:/u01 --ip 172.19.0.30 -h HOST02 --name BOX02 centos:latest /usr/sbin/init

docker run -it -d --privileged=true --restart=always --network MQMNTRK -v my-vol301:/u01 --ip 172.19.0.40 -h HOST03 --name BOX03 centos:latest /usr/sbin/init

docker run -it -d --privileged=true --restart=always --network MQMNTRK -v my-vol401:/u01 --ip 172.19.0.50 -h HOST04 --name BOX04 centos:latest /usr/sbin/init

Tuesday, July 25, 2023

What is Dockerfile ?

What is Dockerfile ?


A Dockerfile contains set of instructions to build a docker image.

-In a Dockerfile we will use DSL (Domain Specific Language).
-Docker Engine will read Dockerfile instructions from top to bottom to process.
-In Dockerfile we will use below keywords:

1) FROM
2) MAINTAINER
3) COPY
4) ADD
5) RUN
6) CMD
7) ENTRYPOINT
8) ENV
9) ARG
10) WORKDIR
11) EXPOSE
12) VOLUME
13) USER
14) LABEL

1.FROM
=> It represents base image to create our docker image

Syntax:

FROM java:1.8
FROM python:1.2
FROM mysql:8.5
FROM tomcat:9.5

2. Maintainer
=> It is used to specify docker file author information

Syntax:

MAINTAINER Qader <qader@oracle.com>

3. COPY
=> It is used to copy the files from source to destination while creating docker image

Syntax:
COPY <SRC> <DESTINATION>
Ex: COPY target/sb-api.war /app/tomcat/webapps/sb-api.war

4. ADD
=> It is used to copy the files from source to destination while creating docker image

Syntax:
ADD <SRC> <DESTINATION>
ADD <HTTP-URL> <DESTINATION>
Ex: ADD <url> /app/tomcat/webapps/sb-api.war

Difference between COPY and ADD command ?

COPY : It can copy from one path to another path with in the same machine.
ADD : It can copy from one path to another path & it supprts URL also as source.

5. RUN
=> RUN instructions will execute while creating docker image

Syntax:

RUN yum install git
RUN yum install maven
RUN git clone <repo-url>
RUN cd <repo-name>
RUN mvn clean package

Note: We can write multiple RUN instructions, docker engine will process from top to bottom.

6.CMD
=> CMD instructions will execute while creating docker container
=> Using CMD command we can run our application in container

Syntax:
CMD sudo start tomcat
CMD java -jar <jar-file>

Note: We can write multiple CMD instructions but docker engine will process only last CMD instruction.

Difference between RUN and CMD ?

=> RUN instructions will execute while creating docker image
=> CMD instructions will execute while creating docker container
=> We can write multiple RUN instructions, docker engine will process from top to bottom.
=> We can write multiple CMD instructions but docker engine will process only last CMD instruction.

Note: There is no use of writing multiple CMD instructions.

7. Sample Docker

FROM ubuntu
MAINTAINER Ashok<ashok.b@oracle.com>
RUN echo "Hi, i am run - 1"
RUN echo "Hi, i am run - 2"
RUN echo "Hi, i am run - 3"

CMD echo "Hi, i am CMD-1"
CMD echo "Hi, i am CMD-2"
CMD echo "Hi, i am CMD-3"

8.ENTRYPOINT
=> It is used to execute instructions while creating container

Synatx:
ENTRYPOINT [ "echo" , "Container Created Successfully" ]
ENTRYPOINT [ "java", "-jar", "target/springboot.jar" ]

Difference between CMD and ENTRYPOINT ?

=> CMD instructions we can override while creating container
=> ENTRYPOINT instructions we can't override

9. WORKDIR
=> It is used to specify working directory for image and container

Syntax:
WORKDIR /app/usr/

10. ENV
=> ENV is used to set Environment Variables

Ex:
ENV java /etc/softwares/jdk

11.EXPOSE
=> It is used to specify on which port number our docker container will run

EX:
EXPOSE 8080

11. ARG
=> By using ARG we can take dynamic values from CLI
=> It is used to remove hard coded values in Dockerfile

Ex:
ARG branch
RUN git clone -b $branch <repo-url>
$ docker build -t <imagename> --build-arg branch=master .


12. USER
=> It is used to specify username for creating image / container

Ex:
USER dockeruser

13. VOLUME
=> It is used to specify docker volume storage location
=> Volumes are used for storage purpose


14. LABEL
=> It is used to add METADATA to docker objects in key-value format

Ex:
LABEL name="sbi_image"

Docker Volumes

Docker Volumes


Volumes are the preferred mechanism for persisting data generated by and used by docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by docker. Volumes have several advantages over bind mounts:

-Volumes are easier to back up or migrate than bind mounts.
-You can manage volumes using docker CLI commands or the docker API.
-Volumes work on both Linux and Windows containers.
-Volumes can be more safely shared among multiple containers.
-Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
-New volumes can have their content pre-populated by a container.
-Volumes on docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.

=> We have 3 types of volumes in docker

1) Anonymous Volume ( No Name )
2) Named Volume
3) Bind Mounts


1) Anonymous volumes are created if the Dockerfile declares a path as VOLUME, but neither a volume, nor a bind is mapped into this path for a container instance of this image. This is the result of the design choices an image maintainer did for their image and your choice to not mount anything into that path.

2) Named volumes are volumes which you create manually with docker volume create VOLUME_NAME. They are created in /var/lib/docker/volumes and can be referenced to by only their name. Let's say you create a volume called "mysql_data", you can just reference to it like this docker run -v mysql_data:/containerdir IMAGE_NAME.

3) Bind mounts are basically just binding a certain directory or file from the host inside the container
(docker run -v /hostdir:/containerdir IMAGE_NAME)


# List or display docker volumes?
$ docker volume ls

# How to create Docker Volume?
$ docker volume create <vol-name>

# How to check the details of docker volume?
$ docker volume inspect <vol-name>

# How to remove docker volume?
$ docker volume rm <vol-name>

# How to remove all volumes
$ docker system prune --volumes


DEMO:

To create a Docker Volume use the command

docker volume create qadervol1
docker volume 1s
docker volume inspect qadervol1


Mounting a Volume using -v or --mount

docker run -it --name=server01 --mount source=qadervol1,destination=/data centos
docker run -it --name server04 -v qadervol1:/data centos
docker run -it --volumes-from server01 --name server02 centos /bin/bash


Mounting a host directory as a data volume?

mkdir files
cd files
docker run -it --volumes-from server01 --name server02 centos /bin/bash
touch file.txt
docker run -it -name server04 -v "$(pwd)":/datal centos




Monday, July 24, 2023

How to copy a file from a docker container to host?

How to copy a file from a docker container to host?

[root@osboxes ~]# docker ps -a

CONTAINER ID   IMAGE           COMMAND            CREATED             STATUS          PORTS     NAMES

d68f6730f12a   centos:latest   "/usr/sbin/init"   19 minutes ago      Up 19 minutes             pgdb3

2d13c81a8040   centos:latest   "/usr/sbin/init"   19 minutes ago      Up 19 minutes             pgdb2

1a9928f7c2f6   centos:latest   "/usr/sbin/init"   About an hour ago   Up 22 minutes             pgdb1

[root@osboxes ~]# docker cp 1a9928f7c2f6:/qader/qader1 /root

Successfully copied 1.54kB to /root

How to display the resource usage like CPU and RAM of a docker container ?

How to display the resource usage like CPU and RAM of a docker container ?

[root@osboxes ~]# docker stats --all

[root@osboxes ~]# docker stats

[root@osboxes ~]# docker stats --no-stream