Docker Cheat Sheet for Spring Developers

Docker Cheat Sheet for Spring Developers

I’ve been playing with Docker a lot recently to deploy Spring Boot applications. Docker is very cool. I’ve been learning a lot about it.

This is my unofficial Docker Cheat sheet. Use with caution!

Got any tips and tricks? Comment below, and I’ll try to update this.

List all Docker Images

1

docker images-a

List All Running Docker Containers

1

docker ps

List All Docker Containers

1

docker ps-a

Start a Docker Container

1

docker start<container name>

Stop a Docker Container

1

docker stop<container name>

Kill All Running Containers

1

docker kill$(docker ps-q)

View the logs of a Running Docker Container

1

docker logs<container name>

Delete All Stopped Docker Containers

Use -f option to nuke the running containers too.

1

docker rm$(docker ps-a-q)

Remove a Docker Image

1

docker rmi<image name>

Delete All Docker Images

1

docker rmi$(docker images-q)

Delete All Untagged (dangling) Docker Images

1

docker rmi$(docker images-q-fdangling=true)

Delete All Images

1

docker rmi$(docker images-q)

Remove Dangling Volumes

1

docker volume rm-f$(docker volume ls-fdangling=true-q)

SSH Into a Running Docker Container

Okay not technically SSH, but this will give you a bash shell in the container.

1

sudo docker exec-it<container name>bash

Use Docker Compose to Build Containers

Run from directory of your docker-compose.yml file.

1

docker-compose build

Use Docker Compose to Start a Group of Containers

Use this command from directory of your docker-compose.yml file.

1

docker-compose up-d

This will tell Docker to fetch the latest version of the container from the repo, and not use the local cache.

1

docker-compose up-d--force-recreate

This can be problematic if you’re doing CI builds with Jenkins and pushing Docker images to another host, or using for CI testing. I was deploying a Spring Boot Web Application from Jekins, and found the docker container was not getting refreshed with the latest Spring Boot artifact.

1

2

3

4

5

6

#stop docker containers, and rebuild

docker-compose stop-t1

docker-compose rm-f

docker-compose pull

docker-compose build

docker-compose up-d

Follow the Logs of Running Docker Containers With Docker Compose

1

docker-compose logs-f

Save a Running Docker Container as an Image

1

docker commit<image name><name forimage>

Follow the logs of one container running under Docker Compose

1

docker-compose logs pump<name>

Checkout my Free Introduction to Docker Course!

Dockerfile Hints for Spring Boot Developers

Add Oracle Java to an Image

For CentOS/ RHEL

1

2

3

4

5

6

7

8

9

10

11

12

13

ENV JAVA_VERSION8u31

ENV BUILD_VERSION b13

# Upgrading system

RUN yum-yupgrade

RUN yum-yinstall wget

# Downloading & Config Java 8

RUN wget--no-cookies--no-check-certificate--header"Cookie: oraclelicense=accept-securebackup-cookie""http://download.oracle.com/otn-pub/java/jdk/$JAVA_VERSION-$BUILD_VERSION/jdk-$JAVA_VERSION-linux-x64.rpm"-O/tmp/jdk-8-linux-x64.rpm

RUN yum-yinstall/tmp/jdk-8-linux-x64.rpm

RUN alternatives--install/usr/bin/java jar/usr/java/latest/bin/java200000

RUN alternatives--install/usr/bin/javaws javaws/usr/java/latest/bin/javaws200000

RUN alternatives--install/usr/bin/javac javac/usr/java/latest/bin/javac200000