-
Notifications
You must be signed in to change notification settings - Fork 1
Docker
Docker is an open-source platform for developing, shipping, and running applications in a containerized environment. It allows developers to package their applications and their dependencies into lightweight containers that can be easily moved from one environment to another, such as from a developer's laptop to a testing environment or to a production server.
Docker containers provide a standardized environment for running applications, which helps to ensure that the application behaves consistently across different machines and operating systems. Docker also provides tools for managing and orchestrating containers at scale, making it possible to deploy and manage large distributed systems with ease.
Overall, Docker has become a popular technology for building and deploying applications, especially in cloud-native and microservices architectures, because of its portability, efficiency, and flexibility.
Install Docker in Amazon Linux
$ sudo yum update -y
$ sudo yum install docker -y
$ sudo service docker star
$ docker info
Restart the session
$ exit
Install Docker in Windows
download docker .exe file from below link and install docker in your local machine
https://docs.docker.com/desktop/install/windows-install/
Dockerfile : It contains set of instructions to create / build docker image
Docker Image : Docker Image is a package which contains application code + application dependencies
Docker Container : When we run Docker Image it will create Docker Container to execute our application
build run
Dockerfile ----------------> Docker Image ----------------> Docker Container*
It is a centralized repository where we can store and retrieve docker images
Dockerfile contains set of instructions to build docker image
We will use Docker Domain Specific keywords to write Dockerfile
FROM
MAINTAINER
COPY
ADD
RUN
CMD
ENTRYPONT
ENV
LABEL
USER
WORKDIR
EXPOSE
VOLUME
FROM Keyword
FROM keyword is used to specify base image to create our project image
To create docker image for Java application we will use 'java' as base image
To create docker image for python application we will use 'python' as base image
FROM openjdk:17-jdk-slim-buster
FROM tomcat:9.2
From mysql
MAINTAINER
It represents author of Dockerfile
MAINTAINER Asif <asifraza7857@gmail.com>
COPY Keyword
It is used to copy files/folders while creating image
Ex : COPY
Ex: COPY target/maven-web-app.war /opt/tomcat/webapps/maven-web-app.war
ADD Keyword
It is also used to copy the files while creating image
ADD keyword can download files also from remote location using http
EX : ADD
RUN Keyword
It is used to execute commands on top of our base image
RUN command instructions will execute while creating our image
We can write multiple RUN instructions in Dockerfile (they will execute in order top to bottom)
Ex: RUN mkdir demo
RUN yum install git
RUN yum install maven
CMD Keyword CMD keyword is used to execute commands
CMD commands will execute while creating container EX: CMD sudo start tomcat
We can write multiple CMD instructions in Dockerfile but Docker will process only the last CMD instruction
Note: There is no use of writing multiple CMD instructions
Sample Dockerfile
MAINTAINER Asif <asifraza7857@gmail.com>
RUN echo "run instruction-1"
RUN echo "run instruction-2"
CMD echo "cmd instruction-1"
CMD echo "cmd instruction-2"
RUN echo "run instruction-3"
Build docker image using Dockerfile
$ docker build -t <image-name>
.
Check docker images
$ docker images
Run Docker image
Creating docker image using customer dockerfile
$ docker build -f <file-name> -t <image-name> .
store docker image into docker hub
$ docker login
$ docker tag <image-name> <tag-name>
$ docker push <tag-name>
ENTRYPOINT Keyword
It is used to execute instructions while creating container
NOTE: CMD instructions we can override in runtime where as ENTRYPOINT instructions we can't override
EX:
ENTRYPOINT [ "echo", "Welcome to Fission Labs" ]
ENTRYPOINT [ "java" , "-jar", "target/spring-boot-app.jar" ]
WORKDIR Keyword It is used to set working directory for a image / container
Ex:
WORKDIR <dir-path>
The instructions which are available after WORKDIR those instructions will be processed from given working directory
ENV Keyword
It is used to set Environment variables
EX:
ENV <key> <value>
LABEL Keyword
It is used to represent data in key-value format It is used to write METADATA in docker file
Ex:
LABEL branchName release
ARG Keyword
It is used to avoid hard coded values in docker file
Ex:
ARG branch=develop
LABEL branch $branch
Note: We can pass ARG value in runtime (while creating docker image)
$ docker build -t <image-name> --build-arg branch=release
USER Keyword
It is used to set USER for image or conatiner to execute further instructions
Note: After USER instruction, remaining instructions will be processed with given USER
EXPOSE Keyword
It is used to specify on which PORT number our container is running
Note: It is just for documentation to understand Container PORT Number
EX:
EXPOSE 8080
VOLUME Keyword
It is used for data storage
Dockerizing Java Web App
- Install git client
- Install maven
$ sudo yum install maven
- clone git hub project
$ git clone <repo-url>
- Navigate into project directory and Build Maven project using maven goals
$ mvn clean package
- Create Dockerfile
$ vi Dockerfile
FROM tomcat:8.5.4-jre8
MAINTAINER Asif <asifraza7857@gmail.com>
COPY target/java-web-app-1.0.war /usr/local/tomcat/webapps/java-web-app.war
EXPOSE 8080
- Build Docker image
$ docker build -t <image-name> .
- Check docker image
$ docker images
- Run docker image
$ docker run -d -p 8080:8080 <image-name>
Dockerize Spring Boot Application
Java web applications will be deployed as 'war' file
Java Spring Boot applications will be deployed as 'jar' file
FROM java:8-jdk-alpine
COPY ./target/spring-boot-docker-app.jar /usr/app/
WORKDIR /usr/app
ENTRYPOINT [ "java", "-jar", "spring-boot-docker-app.jar" ]
Docker image commands
Pulling docker image
$ docker pull <image-name>
Display docker images
$ docker images
Delete Docker image
$ docker rmi <image-name>
Building Docker image
$ docker build -t <image-name> .
Tagging docker image
$ docker tag <image-name> <tag-name>
Delete all images which are not attached to containers
$ docker system prune -a
Docker Container Commands
Display list of running containers
$ docker container ls
To see list of running and stopped containers
$ docker ps -a
Start the container
$ docker start <container-name/container-id>
Stop the container
$ docker stop <container-name / container-id>
Delete Stopped Container
$ docker rm <container-name / container-id>
Delete running container
$ docker rm -f <container-name/container-id>
Stop all running containers
$ docker stop $(docker ps -aq)
Restart all containers
$ docker restart $(docker ps -aq)
Remove all containers (running & stopped)
$ docker rm -f $(docker ps -aq)
Docker Networking
Networking is all about communication among the processes
Docker network is used to provide isolation for Docker containers
Note: One Docker container can be linked to any no.of networks
Advantages of Docker Networking
-
We can maintain isolated environment
-
Fast Delivery
-
Application Portability
When docker s/w installed we will get below 3 default networks
1) host
2) bridge
3) none
We can check docker networks using below command
$ docker network ls
Network Drivers
- Bridge
- Host
- None
- Overlay
- Macvlan
Bridge is the default driver created in docker machine
When application running on standalone conatiner then Bridge driver is very userful
Host driver is also used for Standalone containers
None means containers will not have access to externa networks
If we want to disable networking for a container then we will use None driver
Overlay driver is used for Cluster Environment
Macvlan driver lets you assign a MAC address to a container
Docker Networking Commands
List docker networks
$docker network ls
Create docker network
$ docker network create --driver bridge asif-nw
Note: If we don't specify the driver then it will take 'bridge' driver by default
Running container on user-defined network
$ docker run --name nginx -d --network asif-nw -p 80:80 nginx
inspect the network
$ docker network inspect asif-nw
Container Communication
Run first container
$ docker run --name nginx1 -d --network asif-nw -p 9090:80 nginx
Run second container
$ docker run --name nginx2 -d --network asif-nw -p 9091:80 nginx
You can get docker container ip address with below command
$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_id>
get into nginx1 container
$ docker exec -it nginx1 /bin/bash
$ apt-get update
$ apt-get install iputils-ping
$ ping <nginx2-ip>
get into nginx2 container
$ docker exec -it nginx2 /bin/bash
$ apt-get update
$ apt-get install iputils-ping
$ ping <nginx1-ip>
Note: If we are able to ping from container to another container that means our networkg is working perfectley
Docker Volumes
Docker containers are stateless by default
Once we delete the docker container we will loose the data that got generated by docker container
We can make Docker containers statefull using 'Docker Volumes' concept
Note: Statefull means saving the data permanentley which got generated by container
Usecase: If we delete mysql container we should not loose the customers data which is stored in the mysql database
Display docker volumes
$ docker volume ls
Create docker volume
$ docker volume create <volume-name>
Remove Docker Volume
$ docker volume rm <volume-name>
Remove All Docker Volumes
$ docker volume prune