Docker Cheat Sheet for Spring Developers

Docker Cheat Sheet for Spring Developers

4 Comments

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 Cheatsheet. Use with caution!

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

List all Docker Images

docker images -a

List All Running Docker Containers

docker ps

List All Docker Containers

docker ps -a

Start a Docker Container

docker start <container name>

Stop a Docker Container

docker stop <container name>

Kill All Running Containers

docker kill $(docker ps -q)

View the logs of a Running Docker Container

docker logs <container name>

Delete All Stopped Docker Containers

Use -f option to nuke the running containers too.

docker rm $(docker ps -a -q)

Remove a Docker Image

docker rmi <image name>

Delete All Docker Images

docker rmi $(docker images -q)

Delete All Untagged (dangling) Docker Images

docker rmi $(docker images -q -f dangling=true)

Delete All Images

docker rmi $(docker images -q)

Remove Dangling Volumes

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

SSH Into a Running Docker Container

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

sudo docker exec -it <container name> bash

Use Docker Compose to Build Containers

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

docker-compose build

Use Docker Compose to Start a Group of Containers

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

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.

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.

#stop docker containers, and rebuild
docker-compose stop -t 1
docker-compose rm -f
docker-compose pull
docker-compose build
docker-compose up -d

Follow the Logs of Running Docker Containers With Docker Compose

docker-compose logs -f

Save a Running Docker Container as an Image

docker commit <image name> <name for image>

Follow the logs of one container running under Docker Compose

docker-compose logs pump <name>
Introduction to Docker Course
Checkout my Free Introduction to Docker Course!

Dockerfile Hints for Spring Boot Developers

Add Oracle Java to an Image

For CentOS/ RHEL

ENV JAVA_VERSION 8u31
ENV BUILD_VERSION b13

# Upgrading system
RUN yum -y upgrade
RUN yum -y install 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 -y install /tmp/jdk-8-linux-x64.rpm
RUN alternatives --install /usr/bin/java jar /usr/java/latest/bin/java 200000
RUN alternatives --install /usr/bin/javaws javaws /usr/java/latest/bin/javaws 200000
RUN alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 200000

Add / Run a Spring Boot Executable Jar to a Docker Image

VOLUME /tmp
ADD /maven/myapp-0.0.1-SNAPSHOT.jar myapp.jar
RUN sh -c 'touch /myapp.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/myapp.jar"]

 

About jt

    You May Also Like

    4 comments on “Docker Cheat Sheet for Spring Developers

    1. May 13, 2017 at 3:56 pm

      Typo in URL: “devlopers”. Possibly intentional, but if not you might want to alias from the typo so as not to mess up those Googling it (how I found it). Taking your course now… great stuff!

      Reply
    2. June 10, 2017 at 9:44 am

      Thank you.

      Reply
    3. November 30, 2021 at 1:25 am

      Thank you for this post, I’ll just leave this reference for someone who might be looking for it.
      How to remove one or more containers

      docker rm [SHA256]

      Options:
      -f, –force Force the removal of a running container (uses SIGKILL)
      -l, –link Remove the specified link
      -v, –volumes Remove anonymous volumes associated with the container

      Reply

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.