Practical Docker Deployment and Configuration Guide

Deploying GitLab

To set up a GitLab instance with persistent storage, first create the necessary directory structure on the host machine to hold configuration, logs, and data.

sudo mkdir -p /srv/gitlab/config
sudo mkdir -p /srv/gitlab/logs
sudo mkdir -p /srv/gitlab/data

Launch the GitLab container using the official Community Edition image, mapping the host directories to the container paths and setting the timezone.

docker run \
  -d \
  --hostname gitlab.example.com \
  --name gitlab-server \
  --publish 443:443 --publish 80:80 --publish 22:22 \
  --env TZ=Asia/Shanghai \
  --volume /srv/gitlab/config:/etc/gitlab \
  --volume /srv/gitlab/logs:/var/log/gitlab \
  --volume /srv/gitlab/data:/var/opt/gitlab \
  --restart always \
  gitlab/gitlab-ce:latest

To retrieve the default root password, access the container's filesystem and read the initialization file.

docker exec -it gitlab-server grep 'Password:' /etc/gitlab/initial_root_password

Database and Cache Services

MySQL Deployment

Create a dedicated directory for MySQL data persistence.

mkdir -p /srv/mysql/data

Run the MySQL container, specifying the root password and enabling remote access from any host.

docker run \
  -d \
  --name mysql-db \
  -p 3306:3306 \
  -e TZ=Asia/Shanghai \
  -e MYSQL_ROOT_PASSWORD=secure_password \
  -e MYSQL_ROOT_HOST="%" \
  -v /srv/mysql/data:/var/lib/mysql \
  --restart always \
  mysql:8.0

Redis Cache

Deploy a Redis instance with a custom password requirement.

docker run -d \
  --name redis-cache \
  -p 6379:6379 \
  --env REDIS_PASSWORD=my_redis_secret \
  redis:latest

SQL Server on Linux

Instantiate a Microsoft SQL Server container, accepting the EULA and setting the 'sa' user password.

docker run -e 'ACCEPT_EULA=Y' \
  -e 'SA_PASSWORD=YourStrong@Passw0rd' \
  -p 1433:1433 \
  -v /srv/mssql/data:/var/opt/mssql \
  --restart always \
  --name sqlserver-instance \
  -d mcr.microsoft.com/mssql/server:2022-latest

Application Deployment Workflow

The following sequence demonstrates a complete cycle of removing an existing container, rebuilding the image, and running a new container for a Java application.

# Remove existing container and image
docker rm -f inventory-service
docker rmi -f inventory-service:v1

# Build new image from Dockerfile
docker build -f ./Dockerfile -t inventory-service:v1 .

# Run the container
docker run -d \
  -p 8080:8080 \
  -e TZ=Asia/Shanghai \
  --restart always \
  -v /srv/app/logs:/application/logs \
  --name inventory-service \
  inventory-service:v1

System Monitoring and Maintenance

Analyze disk usage by directory to identify large space consumers.

du -h --max-depth=1 | sort -hr

Check overall filesystem disk space usage.

df -h

Monitor real-time resource usage of running containers.

docker stats --no-stream

Container Management

List all containers, including those that are stopped.

docker ps -a

Access an interactive shell inside a running container.

docker exec -it <container_id> /bin/bash

Managing Restart Policies

Configure a container to restart automatically unless manually stopped.

docker update --restart=always my_container

Disable automatic restart for a specific container.

docker update --restart=no my_container

Restart Policy Flags:

  • no: Do not automatically restart the container (default).
  • on-failure: Restart only if the container exits with a non-zero error code.
  • unless-stopped: Restart unless the container was explicitly stopped or the Docker daemon was stopped/restarted.
  • always: Always restart the container regardless of the exit status.

Volume and Image Management

Inspect a container to view detailed mount information.

docker inspect --format='{{ .Mounts}}' <container_id>

Understanding Bind Mounts:

A bind mount configuration typically looks like this:

{
  "Type": "bind",
  "Source": "/host/path/config.conf",
  "Destination": "/container/path/config.conf",
  "Mode": "rw",
  "RW": true,
  "Propagation": "rprivate"
}

This configuration maps a file or directory from the host machine (Source) directly into the container (Destination), with read-write access enabled.

Saving and Loading Images

Export an image to a tar archive for migration or backup.

docker save -o my_app_backup.tar my_app:latest

Load an image from a tar archive into the local Docker registry.

docker load -i my_app_backup.tar

Converting a Container to an Image

If you have made changes inside a container and want to save them as a new image:

docker commit <container_id> my_custom_image:v1.0

Containerized Java Applications

Sample Dockerfile

This Dockerfile uses a lightweight Java base image, sets the timezone, and prepares the application jar for execution.

FROM eclipse-temurin:17-jre-alpine

# Set working directory
VOLUME /tmp

# Set timezone to Asia/Shanghai
ENV TZ=Asia/Shanghai
RUN apk add --no-cache tzdata && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    echo $TZ > /etc/timezone

# Copy application jar
COPY target/my-application.jar /app.jar

# Define entrypoint
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]

Build the image using the context of the current directory.

docker build -f ./Dockerfile -t my-application:v1 .

Networking

Create a user-defined bridge network to allow containers to communicate by name.

docker network create app-network

Connect a running container to the specific network.

docker network connect app-network redis-cache

Cleanup Operations

To stop all running containers efficiently:

docker stop $(docker ps -a -q)

To remove all containers (stopped and running):

docker rm -f $(docker ps -a -q)

Tags: docker devops containerization System Administration java

Posted on Sun, 30 Aug 2026 16:15:52 +0000 by Ekate