Skip to content

Latest commit

 

History

History
 
 

chapter2

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Chapter 2

2.1 Docker CLI

Show Docker CLI commands

2.2 Run your first Docker container

docker container run -it jboss/wildfly
docker container run -d jboss/wildfly
docker container ls
docker container stop {name}
docker container rm {name}
docker container run -d --name web jboss/wildfly
docker container ls
docker container rm -f web
docker container run -it --name web jboss/wildfly bash

2.3 Run Docker container (ports & volumes)

Expose ports

docker container run -d --name web -P jboss/wildfly
docker container run ls
docker container logs web

Access WildFly home page in browser on the exposed port

Expose ports

docker container stop web
docker container rm web
docker container run -d --name web -p 8080:8080 jboss/wildfly
docker container logs web

Access WildFly home page in browser on 8080

Volume mapping

docker container rm -f web
docker container run -d --name web -p 8080:8080 -v `pwd`/webapp.war:/opt/jboss/wildfly/standalone/deployments/webapp.war jboss/wildfly
docker container logs web -f
curl http://localhost:8080/webapp/resources/persons

2.4 Create first Docker image

  1. Dockerfile syntax

  2. Create a new directory

  3. Explain build context

FROM ubuntu

CMD echo "Hello world"
  1. Build image: docker image build -t helloworld .

  2. Explain .dockerignore

  3. List image: docker image ls

  4. History of image: docker image history helloworld

  5. Run container: docker container run helloworld

  6. Explain --compress

  7. Explain --squash

2.5 Create first Java Docker image

FROM openjdk

CMD java -version
  1. Build image: docker image build -t hellojava .

  2. List image and show the size: docker image ls

  3. Run container: docker container run hellojava

FROM openjdk:jdk-alpine

CMD java -version
  1. Build image: docker image build -t hellojava:2 .

  2. Talk about image tagging

  3. List image and show/compare the size: docker image ls

  4. Run container: docker container run hellojava:2

2.6 Copy files in the Docker image

  1. Explain COPY vs ADD

FROM jboss/wildfly

COPY webapp.war /opt/jboss/wildfly/standalone/deployments/webapp.war
  1. Build image: docker image build -t helloweb .

  2. Run container: docker container run -p 8080:8080 -d helloweb

  3. Access application: curl http://localhost:8080/webapp/resources/persons

2.7 Run JAR files from the Docker image

FROM openjdk:jdk-alpine

COPY myapp/target/myapp-1.0-SNAPSHOT.jar /deployments/

CMD java -jar /deployments/myapp-1.0-SNAPSHOT.jar
  1. Create JAR file: mvn -f myapp/pom.xml clean package

  2. Build image: docker image build -t hellojava:3 .

  3. Run container: docker container run hellojava:3

  4. Change Java application (change "Hello" to "Howdy")

  5. Create new JAR file using mvn -f myapp/pom.xml clean package

  6. Build new Docker image: docker image build -t hellojava:4 .

  7. Run new Docker container: docker container run hellojava:4

  8. Show updated changes

2.8 Other Dockerfile instructions

Slides only

2.9 Docker and Maven

  1. Check out https://github.com/arun-gupta/docker-java-sample

  2. Run using CLI: mvn clean package exec:java

  3. Show pom.xml and explain DMP

  4. Build image: mvn package -Pdocker

  5. Show image: docker image ls

  6. Run container: mvn install -Pdocker

2.10 Docker and Gradle

  1. Check out https://github.com/arun-gupta/docker-java-sample

  2. Run using CLI: ./gradlew build run

  3. Show build.gradle and explain Docker Gradle Plugin

  4. Build image: ./gradlew dockerBuildImage

  5. Show image: docker image ls

  6. Run container: ./gradlew startContainer

2.11 Tag and Share Docker Image

  1. Show tags at https://hub.docker.com/_/openjdk/

  2. Docker image name format: <registry>/repo_name:tag

    1. Default value of <registry> is docker.io

Dockerfile

Use Dockerfile:

FROM ubuntu:latest

CMD echo "This is v1"

Remove all images and containers

  1. docker image rm -f $(docker image ls -qa)

  2. docker container rm -f $(docker container ls -aq)

Image with no name or tag

  1. Build image: docker image build .

  2. List image: Use docker image ls to show the list of images, particular <none>:<none> for this image

  3. Run container: docker container run <image-id>, have no name

Image with no name and default latest tag

  1. Build image: docker image build -t helloworld .

  2. List image: docker image ls, show default <none>:<none> is gone, latest tag is created

  3. Run container: docker container run helloworld and docker container run helloworld:latest

Image with name and explicit tag

  1. Remove all containers: docker container rm -f $(docker container ls -aq)

  2. Remove image with latest tag: docker image rm helloworld:latest

  3. Build image: docker image build -t helloworld:1 .

  4. List image: docker image ls, show no latest tag

  5. Run container: docker container run helloworld:1

  6. Run latest container: docker container run helloworld and docker container run helloworld:latest

Tag versioned image with latest tag

  1. Tag image: docker image tag helloworld:1 helloworld:latest

  2. List images: docker image ls, show both tags are available

  3. Run latest container (now works): docker container run helloworld and docker container run helloworld:latest

Is latest really latest?

Use Dockerfile:

FROM ubuntu:latest

CMD echo "This is v2"
  1. Build image: docker image build -t helloworld:2 .

  2. Run container: docker container run helloworld shows v1

  3. Tag v2 to latest: docker image tag helloworld:2 helloworld:latest

  4. Run container: docker container run helloworld shows v2 now

Push image to Docker Hub

  1. Push the image without namespace and show the error: docker image push helloworld:latest

  2. Tag image: docker image tag helloworld:2 arungupta/helloworld:latest

  3. Login to Docker: docker login

  4. Push to Docker Hub: docker image push arungupta/helloworld:latest

  5. Push to local registry

    1. Run registry: docker container run -d -p 5000:5000 --restart always --name registry registry:2.6.0

    2. Tag image for local registry: docker image tag helloworld:latest localhost:5000/arungupta/helloworld:latest

    3. Use docker image ls to show the list of images

    4. Push to local registry: docker image push localhost:5000/arungupta/helloworld:latest