如果你是用windwos做服务器的话,那就不要用docker了
- docker主要为windows10用户使用。而服务器一般都是windows server
- docker toolbox使用了VirtualBox做虚拟机,导致ip固定是192.168.99.100,而这个ip局域网是访问不了的,我没找到好的方法修改ip。
所以,我不建议windows上使用docker
目的:创建一个mysql容器,设置root密码,创建一个名为cy的新用户,映射3306端口,挂载配置文件和数据目录到本地系统里
本地系统创建目录
cd /home
mkdir docker
cd docker
mkdir mysql
cd mysql
mkdir conf data logs
在conf目录下创建一个my.cnf文件,内容如下
[mysqld]
user=mysql
character-set-server=utf8
collation-server=utf8_general_ci
default_authentication_plugin=mysql_native_password
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
启动命令start.sh
docker run -d -p 3307:3306 --restart=always --privileged=true -e MYSQL_USER="cy" -e MYSQL_PASSWORD="root" -e MYSQL_ROOT_PASSWORD="root" --name mysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/data:/var/lib/mysql -v $PWD/logs:/logs mysql:5.7
上面即可实现我们的目的
进入容器:docker exec -it mysql /bin/bash
执行命令:env
输出如下:
root@81856089f317:/etc/mysql# env
MYSQL_PASSWORD=root
HOSTNAME=81856089f317
MYSQL_ROOT_PASSWORD=root
PWD=/etc/mysql
HOME=/root
MYSQL_MAJOR=5.7
GOSU_VERSION=1.7
MYSQL_USER=cy
MYSQL_VERSION=5.7.25-1debian9
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
OLDPWD=/etc
_=/usr/bin/env
目的:创建redis5.0版本,配置文件和数据目标挂载到本地机器上,外网可以访问
官方dockerfile
cd /home
mkdir docker
cd docker
mkdir redis
cd redis
mkdir conf data
拷贝一下redis.conf(可以下载一个redis.tar,得到redis.conf)到在conf下目录下:
如下做些修改,其它保持不变
protected-mode no
bind 0.0.0.0
requirepass root
appendonly yes
redis.conf5.0全部配置,拷贝即可
bind 0.0.0.0
protected-mode no
requirepass root
appendonly yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendfilename "appendonly.aof"
# appendfsync always
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
启动命令start.sh
docker run -d -p 6379:6379 --restart=always -v $PWD/conf/redis.conf:/etc/redis/redis.conf -v $PWD/data:/data --name redis redis:5.0 redis-server /etc/redis/redis.conf --appendonly yes
若报错,可以加个参数–privileged=true
目的:创建nginx1.15,挂载本地配置,数据和日志
cd /home
mkdir docker
cd docker
mkdir nginx
cd nginx
mkdir conf html logs
cd conf
mkdir conf.d
在conf目录下创建一个nginx.conf文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html/index;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include conf.d/*conf;
}
注意配置
location / {
root /usr/share/nginx/html/index;
index index.html index.htm;
}
这里的路径必需填写绝对路径,因为配置文件和资源路径不是在同一个目录下,无法使用相对路径。
如果需要添加配置,可以在conf.d目录下创建新的server即可
启动命令start.sh
docker run -d -p 8080:80 --restart=always -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v $PWD/html:/usr/share/nginx/html -v $PWD/logs:/var/log/nginx --name nginx nginx:1.15
前提条件:
windows10系统教育版,使用dockerToolbox做docker
准备:
目标:创建一个mysql容器,设置root密码,创建一个名为cy的新用户,映射3306端口,挂载配置文件和数据目录到本地系统里
本地系统创建目录。数据目录和配置在本地磁盘中,想恢复数据,直接运行即可。docker不会自动删除数据库数据。
按如下创建目录结构
my.cnf文件在conf目录下
[mysqld]
user=mysql
character-set-server=utf8
collation-server=utf8_general_ci
default_authentication_plugin=mysql_native_password
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
启动命令start.sh
"C:/Program Files/Docker Toolbox/docker.exe" run -d -p 3307:3306 --privileged=true -e MYSQL_USER="cy" -e MYSQL_PASSWORD="root" -e MYSQL_ROOT_PASSWORD="root" --name mysql -v /c/Users/k/docker/testMysql/data:/var/lib/mysql -v /c/Users/k/docker/testMysql/conf:/etc/mysql/conf.d -v /c/Users/k/docker/testMysql/logs:/logs mysql:5.7 --character-set-server=utf8 --collation-server=utf8_general_ci --explicit_defaults_for_timestamp=true --innodb_use_native_aio=0
这里我成功的脚本,其中
--character-set-server=utf8 --collation-server=utf8_general_ci --explicit_defaults_for_timestamp=true --innodb_use_native_aio=0必需放在镜像后面方可执行成功
和在linux上创建过程完全一样,唯一区别是挂载的目录写法
和在linux上创建过程完全一样,唯一区别是挂载的目录写法
从镜像搜索,里搜索镜像,下面有使用方法。