title: More about Docker
tags: Docker

All in one docker-compose.yml

Introduction

It's so annoying and mentally exhausted to install tons of software just to a new computer/new device, hence we can write a docker-compose.yml to handle all of these for us!

docker-compose.yml

MySQL
WordPress
Postgres
Redis
Jenkins
SonarQube
Yapi
Swagger
Nginx
PHP

docker-compose.yml

version: '3.8' services: db-mysql: image: mysql container_name: 'DevOps_mysql' ports: - '3306:3306' environment: MYSQL_ROOT_USER: root MYSQL_ROOT_PASSWORD: 123456 MYSQL_DATABASE: wordpress MYSQL_USER: root MYSQL_PASSWORD: 123456 volumes: - ./mysql:/var/lib/mysql wordpress: depends_on: - db-mysql image: wordpress:latest container_name: 'DevOps_wordpress' ports: - "18000:80" restart: always environment: WORDPRESS_DB_HOST: db-mysql:3306 WORDPRESS_DB_USER: root WORDPRESS_DB_PASSWORD: 123456 WORDPRESS_DB_NAME: wordpress volumes: - ./wordpress:/var/www/html db-postgres: image: postgres:12.7 container_name: 'DevOps_postgres' ports: - '5432:5432' environment: POSTGRES_USER: wolfzxcv POSTGRES_PASSWORD: 123456 volumes: - postgresql:/var/lib/postgresql # This needs explicit mapping due to https://github.com/docker-library/postgres/blob/4e48e3228a30763913ece952c611e5e9b95c8759/Dockerfile.template#L52 - ./postgresql/data:/var/lib/postgresql/data sonarqube: image: sonarqube:lts container_name: 'DevOps_sonarqube' depends_on: - db-postgres ports: - '9000:9000' environment: SONAR_JDBC_URL: jdbc:postgresql://db-postgres:5432/sonar SONAR_JDBC_USERNAME: wolfzxcv SONAR_JDBC_PASSWORD: 123456 volumes: - sonarqube_conf:/opt/sonarqube/conf - sonarqube_data:/opt/sonarqube/data - sonarqube_extensions:/opt/sonarqube/extensions - sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins db-mongo: image: mongo:latest container_name: 'DevOps_mongo' ports: - '27017:27017' volumes: - ./mongo/data/db:/data/db yapi: image: jayfong/yapi:latest container_name: 'DevOps_yapi' ports: - '40001:3000' depends_on: - db-mongo environment: - YAPI_ADMIN_ACCOUNT=admin@docker.yapi - YAPI_ADMIN_PASSWORD=123456 - YAPI_CLOSE_REGISTER=true - YAPI_DB_SERVERNAME=db-mongo - YAPI_DB_PORT=27017 - YAPI_DB_DATABASE=yapi - YAPI_MAIL_ENABLE=false - YAPI_LDAP_LOGIN_ENABLE=false - YAPI_PLUGINS=[] swagger: image: swaggerapi/swagger-ui container_name: 'DevOps_swagger' ports: - '38080:8080' environment: - SWAGGER_JSON=/swagger/api.json volumes: - ./swagger:/swagger jenkins: image: jenkins/jenkins:lts container_name: 'DevOps_jenkins' ports: - '8081:8080' volumes: - ./jenkins_home:/var/jenkins_home nginx: restart: unless-stopped image: nginx:latest container_name: 'DevOps_nginx' ports: - '80:80' links: - php volumes: - ./nginx/log:/var/log/nginx - ./nginx/conf:/etc/nginx/conf.d - ./nginx/projects:/projects php: build: ./php restart: unless-stopped container_name: 'DevOps_php' volumes: - ./nginx/projects:/projects redis: image: redis:4.0 container_name: 'DevOps_redis' ports: - '6379:6379' links: - php volumes: postgresql: sonarqube_conf: sonarqube_data: sonarqube_extensions: sonarqube_bundled-plugins:

PHP Dockerfile

From php:7.4.3-fpm RUN docker-php-ext-install pdo pdo_mysql mysqli \ && pecl install -o -f redis && docker-php-ext-enable redis

Nginx PHP config

server
{
  listen 80;
  #listen       443 ssl;
  #ssl_certificate /etc/letsencrypt/live/xyz.live/xyz.live.crt;
  #ssl_certificate_key /etc/letsencrypt/live/xyz.live/xyz.live.pem;

  #ssl_session_timeout 10m;
  #ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  #ssl_prefer_server_ciphers on;

  server_name 192.168.1.38;
  error_log /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;
  root /projects/wc-xyz/public/;
  index index.php index.html;

  location ~ /(index|router|xxxyyyzzzttt/.*)\.php
  {
    try_files $uri =404;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }


  location ~* /.*\.php$
  {
    return 404;
  }
  location ~ [^/]\.php(/|$)
  {
    try_files $uri =404;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }


  location /
  {

    if (!-e $request_filename)
    {
      rewrite ^(.*)$ /index.php?s=$1 last;
      break;
    }
  }

  location ~ \.php
  {
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    set $real_script_name $fastcgi_script_name;
    if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$")
    {
      set $real_script_name $1;
    }
    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    fastcgi_param SCRIPT_NAME $real_script_name;
  }


  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$
  {
    expires 30d;
    access_log off;
  }
  location ~ .*\.(js|css)?$
  {
    expires 7d;
    access_log off;
  }
}

Nginx default config

server {
    index index.php index.html index.htm;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root  /projects/wc-xyz/public/;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /projects/;
    }


    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~* \.(css|js|jpeg|jpg|gif|png|ico|xml) {
    access_log off;
    expires 30d;
    }

    location / {
    try_files $uri $uri/ /index.php;
    }

    location ~ /\.ht {
    deny all;
    }
}

Looking



Nuxt Dockerfile

FROM node:11.13.0-alpine ENV APP_ROOT /app/ RUN mkdir -p ${APP_ROOT} WORKDIR ${APP_ROOT} COPY package*.json ${APP_ROOT} RUN npm install COPY . ${APP_ROOT} RUN npm run build CMD [ "npm", "start" ]

Express TypeScript Dockerfile

FROM node:14.15.4-alpine3.10 ENV APP_ROOT /app/ # Create Directory for the Container WORKDIR /usr/src/app # Only copy the package.json file to work directory COPY package.json . # Install all Packages RUN npm install # Copy all other source code to work directory ADD . /usr/src/app # TypeScript RUN npm run build # Start CMD [ "npm", "start" ] # EXPOSE 7001
"scripts": { "build": "tsc -p tsconfig.json" }

Run PostgreSQL

  • docker-compose.yml
version: "3.8" services: # Create a service named db. db: # Use the Docker Image postgres. This will pull the newest release. image: "postgres" # Give the container the name my_postgres. You can changes to something else. container_name: "postgres" # Setup the username, password. You can changes these values. environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=11111111 # Maps port 5432 (localhost) to port 5432 on the container. You can change the ports to fix your needs. ports: - "5432:5432" # Set a volume some that database is not lost after shutting down the container. # I used the name postgres-data but you can changed it to something else. volumes: - postgresql-volume:/var/lib/postgresql/data volumes: postgresql-volume: external: true
  • Run this first before docker-compose up -d
    docker volume create --name=postgresql-volume