Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Docker Fundamentals for Microservices Deployment

Notes Nov 9 23

When deploying microservices, teams often manage dozens or even thousands of service instances. A major challenge arises from environment inconsistencies across servers—scripts that work on one machine may fail on another due to differences in OS versions, dependencies, or configurations. Docker addresses this by packaging applications and their dependenices into standardized, isolated units called containers, ensuring consistent behavior across environments.

Docker runs as a background daemon. Users issue commands to this daemon to pull images (prebuilt application templates), then launch containers from those images. This simplifies deployment significantly—even users unfamiliar with Linux can reliably run complex software stacks.

Core Docker Commands

Key operations involve managing images and containers:

# Pull an Nginx image
docker pull nginx

# List local images
docker images

# Run a container in detached mode, mapping port 80
docker run -d --name web-server -p 80:80 nginx

# View running containers; add '-a' for all
docker ps
docker ps -a

# Stop and restart a container
docker stop web-server
docker start web-server

# Access the container's shell
docker exec -it web-server bash
# Exit with 'exit'

# Remove a stopped container; force-remove with '-f'
docker rm db-container
docker rm -f db-container

Volumes for Persistent Data

Volumes provide persistent storage independent of container lifecycle. They map directories between the host and container:

# Create and manage volumes
docker volume create app-data
docker volume ls
docker volume inspect app-data
docker volume rm app-data
docker volume prune  # Remove unused volumes

Bind Mounts for Direct Host Integration

For simpler host-path access, use bind mounts instead of named volumes:

# Syntax: -v <host-path>:<container-path>
-v /host/config:/app/config
-v /host/script.sh:/app/start.sh

Verify mounts using:

docker inspect web-server

Look under the Mounts section in the output.

Example: Deploy MySQL with custom configuration and initialization scripts:

  1. Create directories on the host: /root/mysql/{data,init,conf}
  2. Place hm.cnf (setting utf8mb4 charset) in conf/
  3. Place hmall.sql (initialization script) in init/
  4. Run the container with bind mounts:
docker run -d \
  --name mysql-db \
  -p 3306:3306 \
  -e TZ=Asia/Shanghai \
  -e MYSQL_ROOT_PASSWORD=123 \
  -v /root/mysql/data:/var/lib/mysql \
  -v /root/mysql/init:/docker-entrypoint-initdb.d \
  -v /root/mysql/conf:/etc/mysql/conf.d \
  mysql

Building Custom Images with Dockerfile

Images bundle application code, runtime, libraries, and configs. Define them via a Dockerfile:

# Option 1: Start from Ubuntu and install JDK manually
FROM ubuntu:16.04
ENV JAVA_DIR=/usr/local
COPY jdk8.tar.gz $JAVA_DIR/
COPY app.jar /tmp/app.jar
RUN cd $JAVA_DIR && tar -xf jdk8.tar.gz && mv jdk1.8.0_144 java8
ENV JAVA_HOME=$JAVA_DIR/java8
ENV PATH=$PATH:$JAVA_HOME/bin
ENTRYPOINT ["java", "-jar", "/tmp/app.jar"]

# Option 2: Use official OpenJDK image (recommended)
FROM openjdk:11.0-jre-buster
COPY app.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

Container Networking

By default, containers connect to Docker’s default bridge network. To enable communication via container names, create a custom network:

docker network create my-net
docker run --network my-net --name service-a ...
docker run --network my-net --name service-b ...

Now service-a can reach service-b using its name as a hostname.

Command Aliases for Efficiency

Simplify frequent commands by adding aliases to ~/.bashrc:

alias dps='docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"'
alias dis='docker images'

Apply changes immediately with:

source ~/.bashrc

Tags: docker

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.