
Docker 示例
常用示例
Apache
2.4
# 在 8088 端口,以 Daemon 方式运行 Apache 2.4 容器
docker run -d \
--restart=always \
-p 8088:80 \
--name apache24 \
-v <宿主机应用根目录>:/usr/local/apache2/htdocs \
httpd:2.4
Jenkins
docker run --rm -it \
--name jenkins \
-u root \
-p 8080:8080 \
-p 50000:50000 \
-v $HOME:/root \
-v $(pwd)/data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
jenkins/jenkins:lts
Kubectl
docker run --rm -it \
-v ~/.kube/config:/.kube/config \
--user root --entrypoint /bin/bash bitnami/kubectl:latest
MariaDB
挂入命名卷
docker volume create mariadb-10.4
docker container run --name mariadb -v mariadb-10.4:/var/lib/mysql mariadb-10.4
MongoDB
docker run --name mongo -d mongo
.net
nginx
# 自动绑定宿主机空闲端口
-P
Node
考虑先放入 package.json,执行 npm install 后再部署代码。
# 挂载目录,启动 docker 后,在容器的 /var/www 目录下执行 npm start。
docker run -d -p 3000:3000 --name node_express --restart=always -v <宿主机源码存放目录>:/var/www -w /var/www node:9.0 npm start
Perl
# 该容器关闭后自动删除
docker run -it --rm -v ~/:/root -w /root perl:5 /bin/bash
PHP
错误 File not found
nginx + php:fpm 构建的系统,访问 PHP 资源,返回 File not found.
错误。Nginx Docker 镜像中的 default.conf 默认配置的脚本路径并不存在:
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
需要修复为:
# 如果相关 server 或 location 的配置里定义了 root,则使用 $document_root 变量
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 否则使用绝对路径,需要确保 FastCGI 服务可以访问到相关路径下的脚本文件
# 如果 FastCGI 服务也是运行在 Docker 中,需要把相关路径挂入 Docker
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
Python
FROM python:3.10
# Display the Python output through the terminal
ENV PYTHONUNBUFFERED: 1
# Set work directory
WORKDIR /usr/src/app
# Add Python dependencies
## Update pip
RUN pip3 install --upgrade pip
## Copy requirements
COPY requirements.txt ./requirements.txt
## Install requirements
RUN pip3 install -r requirements.txt
PostgreSQL
常用目录
环境变量
开发环境
# PostgreSQL 11
docker run -d --rm\
--name postgres-11 \
-p 5432:5432 \
-v $(pwd)/postgres:/var/lib/postgresql/data \
-e POSTGRES_USER=root \
-e POSTGRES_PASSWORD=root \
postgres:11
PowerShell
命令行
# 该容器关闭后自动删除
docker run -it --rm -v ~/:/root -w /root mcr.microsoft.com/powershell
Ubuntu
挂载用户目录
# 该容器关闭后自动删除
docker run -it --rm -v ~/:/root -w /root ubuntu
# 以只读方式挂载
docker run -it --rm -v ~/:/root:ro ubuntu
Ubuntu 18.04
#
docker run -it --name=ubuntu-18 --net bridge -v ~/:/root -w /root ubuntu:18.04
必装工具
apt install apt-file net-tools
VS Code
扩展安装及使用
code --install-extension ms-vscode-remote.vscode-remote-extensionpack
常见示例错误
GO 模块无法加载
Kuard 生成镜像时需要加载 golang.org 的模块,国内默认无法访问:
go: golang.org/x/net@v0.0.0-20181201002055-351d144fa1fc: unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:443: connect: connection refused)
go: golang.org/x/sync@v0.0.0-20181108010431-42b317875d0f: unrecognized import path "golang.org/x/sync" (https fetch: Get https://golang.org/x/sync?go-get=1: dial tcp 216.239.37.1:443: connect: connection refused)
go: error loading module requirements
解决方案
镜像生成时传入代理配置:
docker build -t kuard . --build-arg http_proxy=http://192.168.1.111:8118/ --build-arg https_proxy=http://192.168.1.111:8118/
用户文件夹挂载失败
macOS 下挂载用户文件夹失败,在 File Sharing 中设置后问题依旧:
docker: Error response from daemon: Mounts denied:
The path ~/
is not shared from OS X and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> File Sharing.
解决方案
需要开启 macOS 系统文件共享功能方可挂载文件夹进容器: