What is docker ?
What can I use Docker for ?
Important Components of Docker
An image is the definition of a container. Container is the instance of an image
Containers Vs Virtual Machine
Getting an Image
docker image pull hello-world
Running Container from the image
docker container run hello-world
If you try to run an image which does not exists on your local then run command will pull that image for you and run it. Try to run the command below to test it.
docker container run virtualpairprogrammers/fleetman-webapp
mapping port
docker container run -dp 8080:8080 virtualpairprogrammers/fleetman-webapp
Listing the container
docker container ls
Stopping a container
docker container stop <initals of container Id>
Docker Hub
Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default.
Go to https://hub.docker.com/
and create your docker hub account
Log in to your docker hub account and look for ubuntu image
Look for the official images because they are well tested
classic vs modern command
docker pull ubuntu (Classic Command)
docker image pull ubuntu (Modern Command)
Use modern commands inspite of using classic command because they are more verbose
e.g
docker image ls
docker container ls
help command
docker --help
docker image --help : Image related commands
docker container --help : Container related commands
Try
docker container run ubuntu
to run container you will notice that it exits quickly.
This happens because ubuntu image uses the command "bin/bash" which expects to run a script.
In order to connect with the terminal use the command below:
docker container run -it ubuntu
Additional Commands for container
docker container ls -a : this command will show stop and running containers
docker container start <Initials-of-container-Id> : start a stopped container
docker container rm <Initials-of-container-Id> : Removing a stopped container. You cannot remove a running container. Stop the container before attempting remove it.
docker container prune : Remove all stopped containers
docker container logs <Initials-of-container-Id>
docker container logs -f <Initials-of-container-Id> : Follow the log further
docker container exec -it <Initials-of-container-Id> bash : connect to a running container's bash shell
Exercise
Building images with commit
Step 1 : docker container run -it ubuntu
Step 2 : apt-get update
Step 3 : apt-get install -y openjdk-8-jdk
Step 4 : javac (To confirm the installation of jdk)
Step 5: exit (Exit the container)
Step 6 : docker container ls -a (List the containers to find out the one which you have create recently with Java installation)
Step 7 : docker container commit -a "<Author-name>" <Initials-of-container-Id> myjdkimage
Step 8 : docker image ls (To view your the newly created image)
Step 9: docker container run -it myjdkimage (Run the image which you have created previously and check if the Java is installed in it)
Docker File
Dockerfile to create the image
FROM ubuntu:latest
MAINTAINER Pulkit Pushkarna "pulkit.pushkarna@gmail.com"
RUN apt-get update && apt-get install -y openjdk-8-jdk
CMD ["/bin/bash"]
Line 1 : Specify the basic image we need extend
Line 2 : Specifies the maintainer of the image
Line 3 : Run the commands inside the base image. && allow us to chain multiple commands
Line 4 : Command to be executed when container is run from the image
Steps to Create image from the docker file
docker image build -t jdk-image-from-docker-file <path-of-directory-which-contains-docker-file>
Copying files to images
FROM ubuntu:latest
MAINTAINER Pulkit Pushkarna "pulkit.pushkarna@gmail.com"
RUN apt-get update && apt-get install -y openjdk-8-jdk
WORKDIR /usr/local/bin
COPY test-program.jar .
CMD ["/bin/bash"]
Step 4 : Specified the Working Directory for the execution of any further commands
Step 5 : Copy the jar from the folder which contains Dockerfile to working directory
Please note that the files to be copied in the image should be present in working directory of Dockerfile or in its subfolders
FROM ubuntu:latest
MAINTAINER Pulkit Pushkarna "pulkit.pushkarna@gmail.com"
RUN apt-get update && apt-get install -y openjdk-8-jdk
WORKDIR /usr/local/bin
COPY test-program.jar .
CMD ["java","-jar","test-program.jar"]
Running the jar file
In step 6 you can see how to run jar file with the help of CMD command
Difference Between CMD and ENTRYPOINT
MAINTAINER vs LABEL
LABEL maintainer="pulkit.pushkarna@gmail.com"
FROM ubuntu:latest
LABEL maintainer="pulkit.pushkarna@gmail.com"
LABEL version="1.0"
RUN apt-get update && apt-get install -y openjdk-8-jdk
WORKDIR /usr/local/bin
COPY test-program.jar .
ENTRYPOINT ["java","-jar","test-program.jar"]
use docker image inspect <image-name> to get the details of docker image
Exercise 2
Lets Create a Simple Spring boot Application and create its build
Introduce a docker file in the project
FROM tomcat:8.5.50-jdk8-openjdk
RUN apt-get update
WORKDIR /usr/local/bin
COPY ./build/libs/basic-app-0.0.1-SNAPSHOT.jar .
EXPOSE 8080
CMD ["java","-jar","basic-app-0.0.1-SNAPSHOT.jar"]
Docker file to be introduced in Spring Boot App
Build docker image for Spring Boot Application
docker image build -t spring-boot-on-docker .
Run docker container for Spring boot application
docker container run -p 8080:8080 spring-boot-on-docker
Publish image to docker hub
In order to push your image to first we need to
create a tag for our image spring-boot-on-docker by prepending it with your username otherwise dockerhub will not allow you to publish the image.
In our case we need to create the image with the following name:
docker image tag <container-id-initials>
pulkitpushkarna/spring-boot-on-docker
Login to docker with username and password
docker login -u <username> -p <password>
Now push the image to the docker
docker push pulkitpushkarna/spring-boot-on-docker:latest
Exercise 3
Network
In order to connect containers from one another we need to use network.
We will now gonna create 2 containers.
Search for Mysql official image
Configure mysql container
Step 1 : docker image pull mysql:5
Step 2 : docker container run --name mysql-5-demo -e MYSQL_ROOT_PASSWORD=password -d mysql
Step 3 : docker container exec -it mysql-5-demo bash
Step 4 : mysql -u root -p
Step 5 : show databases
docker container run
--name mysql-5-demo
-e MYSQL_ROOT_PASSWORD=password -d
-e MYSQL_DATABASE=mydatabase mysql
Provide port mapping to mysql to connect with spring boot
docker container run --name mysql-5-demo
-e MYSQL_ROOT_PASSWORD=password -d -e MYSQL_DATABASE=mydatabase -p 3307:3306 mysql
Create a Spring boot app with mysql and JPA dependencies
spring.datasource.url=jdbc:mysql://localhost:3307/mydatabase
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create
application.properties
application-docker.properties
spring.datasource.url=jdbc:mysql://mysql-5-demo-container:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create
package com.sprintboot.dockermysqldemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import javax.sql.DataSource;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
@SpringBootApplication
@RestController
public class DockerMysqlDemoApplication {
@GetMapping("/")
public String index(){
return "Hello from Spring boot on Docker";
}
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(DockerMysqlDemoApplication.class, args);
DataSource dataSource = applicationContext.getBean(DataSource.class);
try {
DatabaseMetaData databaseMetaData = dataSource.getConnection().getMetaData();
System.out.println(databaseMetaData.getURL());
System.out.println(databaseMetaData.getUserName());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Main Class Code for Spring Boot
Appplication output
Creating network in docker
Step 1: docker network create my-network
Step 2 : docker container run --name mysql-5-demo-container -e MYSQL_ROOT_PASSWORD=password -d -e MYSQL_DATABASE=mydatabase --network my-network mysql
Step 3 : docker container exec -it mysql-5-demo-container
bash
Step 4 :
apt-get update apt-get install iputils-ping
Build the project
Dockerfile
FROM tomcat:8.5.50-jdk8-openjdk
RUN apt-get update
WORKDIR /usr/local/bin
COPY ./build/libs/docker-mysql-demo-0.0.1-SNAPSHOT.jar .
EXPOSE 8080
CMD ["java","-jar","docker-mysql-demo-0.0.1-SNAPSHOT.jar"]
Run Mysql Conatainer for Spring Boot App
docker container run --name mysql-5-demo-container -e MYSQL_ROOT_PASSWORD=password -d -e MYSQL_DATABASE=mydatabase --network my-network mysql
Build the image of the container
docker image build -t spring-boot-jpa-docker .
Run the Container for Spring Boot App
docker container run -it --network my-network -e "SPRING_PROFILES_ACTIVE=docker" --name spring-boot-jpa-docker spring-boot-jpa-docker
Exercise 4
Volumes
Check the volume persistense of mysql official image
We can see here that the volume is mounted to /var/lib/mysql
List all volumes
Run a container of mysql by running the command below :
docker container run
--name mysql-volume
-v mydata:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=password
-e MYSQL_DATABASE=dummy
-d
mysql:5
docker container exec -it mysql-volume bash
Mount the volume
docker container run
--name mysql-volume1
-v /Users/pulkitpushkarna/mydata1:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=password
-e MYSQL_DATABASE=dummy
-d
mysql:5
By mounting volume to a particular location you can keep the track of volume data in your system
Exercise 5
Docker Compose
version: "3"
services:
spring-boot-app:
image: spring-boot-jpa-docker
networks:
- another-network
ports:
- 8080:8080
environment:
- spring.profiles.active=docker
depends_on:
- mysql-5-demo-container
mysql-5-demo-container:
image: mysql:5
networks:
- another-network
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=mydatabase
networks:
another-network:
docker-compose.yaml
Running mysql and docker containers with docker compose:
Go to the folder which contains docker-compose.yaml and run the command
docker-compose up (If your application has some problems while resolving connection with each other due to race condition please run this command again)
To run the compose file in background
docker-compose up -d
Stopping the containers
docker-compose down
Running the services individually
docker-compose up -d mysql-5-demo-container
docker-compose up -d spring-boot-app
docker-compose up -d spring-boot-app
Exercise 6
Swarm
Play with docker
Start the new session and click on the add new instance
Run the node as Manager
Copy the docker swarm join command and paste it in another instance to make this instance manager and other worker
In the Manager node
Run another service on Manager
Click on 8080 link at the top to see your running app