Docker commands
- Recomendation is to use docker command line reference over here or a cheatsheet over here
- other option to read manual by using –help
Docker Registry
- Docker Hub is Default Registry Refer Here
- There are many private docker registries
- AWS Elastic Container Registry (ECR)
- Azure Container Registry (ACR)
- Aritifactory
- Docker registry (Docker Image) Refer Here
- and many more
Docker Images and how to create them
- Docker image is a packaging format to run an application on docker engine as a container
- What docker image has?
- includes everything to run an application
- code or binaries
- runtime
- dependencies
- other filesystem objects
- includes everything to run an application
- From one docker image we can create multiple containers
- How can a Docker image be created?
- Converting existing container into docker image
- Export VMs to Docker Image
- Create DockerImage using Dockerfile (Recommended approach)
Docker image building steps
- To build a docker image we should always take some existing docker image as a base
- Get the steps to configure your application on this docker image
- Now try to use Dockerfile Instructions to create Docker image
Lets try to understand how to build image for a java app (Spring pet clinic)
- Lets write the manual steps.
- Install java 8
- Download spring petclinic jar file from here
- Run the application
- Commands
# Install Java
sudo apt-get update && sudo apt-get install openjdk-8-jdk -y
# Download jar
wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
# Run the application
java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
- This application can be viewed on port 8080
Using Above Steps lets create Dockerfile
- Create some folder (hellodocker)
- Now create a file Dockerfile in this folder
- Find a base image
- Now lets experiment with docker base image ubuntu.
- Lets create a container with ubuntu image
docker container run -it ubuntu /bin/bash
- Now execute the following commands
apt-get update apt-get install openjdk-8-jdk wget -y wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar # Now to start application java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
- Since we need to install java and the rest why dont we take a base image with java already installed? so lets use openjdk:8 image
- Lets create a contianer with openjdk image
docker container run -it openjdk:8 /bin/bash
- Now execute the following commands
wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar # Now to start application java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
- Dockerfile for approach using ubuntu as base image
FROM ubuntu
RUN apt-get update && apt-get install openjdk-8-jdk wget -y
RUN wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
CMD java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
- Now build the docker image using docker image build command
cd ~/hellodocker
docker image build -t hellodocker .
docker image ls
6. Lets run the container using the image
docker container run -d -P hellodocker
- Exercise: Use this docker file to create a image called as hellodocker2 Dockerfile for approach using openjdk as base image
FROM openjdk:8
RUN wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]