Essential Docker Commands and Configuration Guide

Essential Docker Commands and Configuration Guide

Recently, while deploying a Spring Boot project with separate front-end and back-end components, I found Docker to be extremely convenient. Here's a summary of essenttial Docker commands and configurations.

1. Recommended Resources

2. Core Docker Commands

2.1 Docker Service Management

  • Start Docker service ``` systemctl start docker
  • Reload daemon ``` sudo systemctl daemon-reload
  • Restart Docker service ``` systemctl restart docker
    
    
    sudo service docker restart
  • Stop Docker ``` service docker stop
    
    
    systemctl stop docker
    
    

2.2 Common Docker Commands

  • Check Docker version ``` docker version

  • Pull an image ``` docker pull :

    
    Where <image name> is the image name and <tag> is the version tag (optional, defaults to latest). Check the official repository for available tags.
    
    

    docker pull mysql

    
    

    docker pull mysql:8.0.31

  • List downloaded images ``` docker images

  • Remove an imageBefore removing an image, delete any containers that depend on it, even if they're not running.

    docker rmi <image name or image id>
    
  • Run a container ``` docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

    
    

    docker run -itd --name redis-test -p 6379:6379 redis

    
    

    docker run --name=mysql-test -it -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql

    
    **Note:** By default, Redis and MySQL store data within the container. This means data will be lost when the container is removed. To prevent this, store data on the host machine.
    
    

    docker run --name mysql-test -p 3306:3306 -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

  • List running containers ``` docker ps

    
    

    docker ps -a

  • Start a container ``` docker start

  • Stop a container ``` docker stop

    
    

    docker stop $(docker ps -a -q)

  • Remove a container ``` docker rm

    
    

    docker rm $(docker ps -a -q)

    
    

3. Writing Dockerfiles

To create a Docker image for your project, you need to write a Dockerfile that defines how to build the image.

FROM openjdk:8
EXPOSE 8080
ADD myproject-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java", "-jar", "/app.jar", "--spring.profiles.active=pro"]

This Dockerfile does the following:

  1. Uses the OpenJDK 8 base image
  2. Exposes port 8080
  3. Copies your application JAR file into the container and renames it to app.jar
  4. Ensures the JAR file is executable
  5. Runs the application with the production profile

4. Using docker-compose

For complex applications with multiple services (like Nginx, MySQL, Redis, and your Spring Boot application), docker-compose simplifies deployment.

version: "3"
services:
 webserver:
   image: nginx:latest
   ports:
     - 80:80
   volumes:
     - /host/path/nginx/html:/usr/share/nginx/html
     - /host/path/nginx/nginx.conf:/etc/nginx/nginx.conf
   privileged: true
 
 database:
   image: mysql:5.7.27
   ports:
     - 3306:3306
   environment:
     - MYSQL_ROOT_PASSWORD=admin
 
 cache:
   image: redis:latest
 
 application:
   build: .
   ports:
     - 8081:8081
   depends_on:
     - database
     - cache

This configuration defines four services: a web server (Nginx), a database (MySQL), a cache (Redis), and your application. The application service builds from the current directory's Dockerfile and depends on the database and cache services.

Tags: docker containerization docker-compose dockerfile Spring Boot

Posted on Wed, 08 Jul 2026 17:10:29 +0000 by a-scripts.com