docker安装常用软件
基础知识
创建bridge网络
docker network create web
查看数据卷
docker volume ls # 查看所有数据卷
docker volume inspect my-conf # 查看指定数据卷内容
/var/lib/docker/volumes/my-conf/_data/ # 默认数据卷位置
指定镜像,数据卷位置
vi /etc/docker/daemon.json # 重启后数据卷默认位置就变成/mnt/docker-data了
{
"registry-mirrors": ["https://6gyvb655.mirror.aliyuncs.com"],
"data-root": "/mnt/docker-data"
}
或者
vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --data-root='/home/docker'
# 修改镜像 把原镜像中的内容拷贝过来
mv /var/lib/docker/image/* /mnt/docker-data/image
docker安装php
介绍
php站点目录 /root/web/html # 站点目录要跟nginx一致
php:版本要选带fpm的
mkdir -p /root/web/{html,conf}
docker run --name myphp7 --restart=always --network web -p9000:9000 -v /root/web/html:/www -d php:7.1.30-fpm
docker安装nginx
介绍
nginx站点目录 /root/web/html # 站点目录要跟php一致
nginx配置文件目录 /root/web/conf # 可以配置多个文件
mkdir -p /root/web/{html,conf}
docker run --name nginx --restart=always --network web -p 80:80 -v /root/web/html:/usr/share/nginx/html -v /root/web/conf:/etc/nginx/conf.d -d nginx
vim /root/web/conf/default.conf
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass myphp7:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
vim /root/web/html/index.php
<?php
phpinfo();
?>
访问
10.0.0.13:80/index.php # nginx站点目录没index.php;时候
10.0.0.13:80 # nginx站点目录有index.php;时候
docker安装mysql
介绍
-e MYSQL_ROOT_PASSWORD=123456 # 指定root的密码
/data/mysql/data/ # 数据目录
/etc/mysql/my.cnf # 配置文件 # 文件my.cnf 注意这里映射到 /etc/mysql/conf.d/mysqld.cnf 不是/etc/my.cnf
准备工作
docker pull mysql
创建目录
mkdir -p /data/mysql/data /data/mysql/conf
配置文件
vim /data/mysql/conf/my.cnf
[mysqld]
# innodb_buffer_pool_size = 128M
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
default-authentication-plugin=mysql_native_password
skip-host-cache
server_id=6
skip-name-resolve
port=3306
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
lower_case_table_names=1
sql_mode='NO_AUTO_VALUE_ON_ZERO'
[client]
socket=/var/run/mysqld/mysqld.sock
启动
docker run -d --restart=always --network web -e MYSQL_ROOT_PASSWORD=123456 --name mysql -v /data/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /data/mysql/data/:/var/lib/mysql -p 3306:3306 mysql
docker安装oracle 12c
挂载目录加权限
mkdir -p /root/data_oracle/
chmod -R 777 /root/data_oracle/
启动
docker run -d -p 8090:8080 -p 1521:1521 --name=oracle12c --restart=always --network web -v /root/data_oracle:/u01/app/oracle truevoly/oracle-12c
登录
sqlplus system/oracle@//119.45.16.143:1521/xe
sqlplus sys/oracle as sysdba;
修改密码:
alter user system identified by oracle;
alter user sys identified by sys;
ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;
创建用户
create user test identified by test;
并给用户赋予权限
grant connect,resource,dba to test;
docker安装redis
介绍
daemonize no # 在前台运行 docker本身需要后台运行,而这个配置选项也是以守护进程启动,两者会冲突 所以docker中必须是no
/root/redis/data/6379 # 数据目录
/root/redis/conf/6379 # 配置文件目录
appendonly no # 关闭AOF模式(默认关闭)
–appendonly yes 开启持久化
docker pull redis
mkdir /root/redis/data/6379/ /root/redis/conf/6379/ -p
配置文件
vi /root/redis/redis.conf
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /data/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
requirepass food
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
启动
docker run -p 6379:6379 --name redis -v /root/redis/data/6379:/data -v /root/redis/conf/6379/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --appendonly yes
docker安装FastDfs
准备工作
docker pull delron/fastdfs
mkdir -p /var/fdfs/tracker /var/fdfs/storage
tracker
docker run -d -p22122:22122 --restart=always --name tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
storage
docker run -d -p23000:23000 -p8888:8888 --restart=always --name storage --link tracker:tracker -e TRACKER_SERVER=tracker:22122 -v /var/fdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage
重启storage
rm -rf /var/lib/docker/volumes/storage/_data/data/fdfs_storaged.pid # 先删除PID文件
测试
cd /var/fdfs/storage # 宿主机的 /var/fdfs/storage映射到storage容器的 /var/fdfs中
# 上传一张照片cumt.png
docker exec -it storage /bin/bash # 进入storage容器
cd /var/fdfs
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf cumt.png
访问
http://120.48.58.217:8888/group1/M00/00/00/rBcAA2MOv62Af15cAADZcN96M7g358.jpg
docker安装jar包
docker run -d -p 8888:8888 --name my-container -v /path/to/jar:/app.jar openjdk:8-jdk-alpine java -jar -Dserver.port=8888 /app.jar
docker安装tomcat
介绍
镜像下载要找支持linux的
https://hub.docker.com/_/tomcat/tags?page=5&ordering=-last_updated
直接指定目录挂载会失败,先用数据卷挂上再把数据卷的东西拷贝出来在启动,或者直接修改数据卷目录成指定了路径,但是这样的话之前下载的镜像也会检测不到
准备工作
docker pull tomcat:9.0.0.M10
mkdir -p /usr/app/tomcat/{webapps,conf,logs}
启动
docker run -d -p 8080:8080 --name tomcat -v webapps:/usr/local/tomcat/webapps -v conf:/usr/local/tomcat/conf -v logs:/usr/local/tomcat/logs --restart=always tomcat:9.0.0.M10
docker run -d -p 8080:8080 --name tomcat -v /usr/app/tomcat/webapps:/usr/local/tomcat/webapps -v conf:/usr/local/tomcat/conf -v logs:/usr/local/tomcat/logs --restart=always tomcat:9.0.0.M10
docker安装python
介绍
镜像地址
https://hub.docker.com/_/python/tags?page=1&ordering=-last_updated
运行容器把 python脚本挂载进去,在容器中运行Python脚本或者 /entrypoint.sh 默认容器启动执行的脚本 ,最后commit保存安装的内容到镜像
下载镜像
docker pull python:3.4.1
mkdir -p /root/python/myapp # 这个目录挂载到容器的/www下
vi /root/python/myapp/text.py # python脚本
#!/usr/bin/python
print("Hello, World!")
vi /root/python/myapp/start.sh # 启动脚本
#!/bin/bash
python /www/text.py > /hello.log
tail -f /hello.log
chmod +x /root/python/myapp/start.sh # 脚本加上执行权限
运行python
docker run -p 8000:8000 -d -v /root/python/myapp:/www --name python python /www/start.sh # 直接就执行python脚本了
安装完软件保存镜像
docker run -p 8000:8000 -itd -v /root/python/myapp:/www --name python python /bin/bash # 原始的自己配置
# /entrypoint.sh 默认容器启动执行的脚本
echo '#!/bin/bash' > /entrypoint.sh
echo 'python /www/text.py > /hello.log' >> /entrypoint.sh
echo 'tail -f /hello.log' >> /entrypoint.sh
docker commit 4931cfd2b692 python:3.7.4 # 安装完软件保存镜像
docker安装rabbitmq
docker run -d -p 15672:15672 -p 5672:5672 -e RABBITMQ_DEFAULT_USER=root -e RABBITMQ_DEFAULT_PASS=root --restart=always --name rabbitmq rabbitmq:latest
docker 安装 Django
制作镜像
指定项目名mysite , 指定python版本python:3.7 ,指定 Django==3.2.12
django-admin startproject mysite #生成mysite目录
cd mysite # 进入目录
chmod +x init.sh
vi mysite/settings.py 修改 ALLOWED_HOSTS = ["*"]
vi dockerfile
FROM python:3.7 # 建立 python3.7 环境
MAINTAINER KXM # 镜像作者
ENV PYTHONUNBUFFERED 1 # 设置 python 环境变量
COPY pip.conf /root/.pip/pip.conf # 设置pip源为国内源
RUN mkdir -p /var/www/html/mysite # 在容器内创建mysite文件夹
WORKDIR /var/www/html/mysite # 设置容器内工作目录
ADD . /var/www/html/mysite # 将当前目录文件加入到容器工作目录中(. 表示当前宿主机目录)
RUN pip install -r requirements.txt # pip安装依赖
CMD ["/bin/bash","init.sh"]
vi init.sh # 容器启动运行的命令 CMD ["/bin/bash","init.sh"]
nohup python manage.py runserver 0.0.0.0:8000 &
vi pip.conf # 修改pip镜像源
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
vi requirements.txt # 需要安装django版本 pip install -r requirements.txt
Django==3.2.12
# 创建镜像
chmod +x init.sh
docker build -t django:v1 .
运行
conf里 init.sh 放启动命令 ,项目名定死了mysite, 没创建app ,其他的都在当前路径下 直接改就行了
# 启动
docker run -it -d -p 88:8000 --name mysite -v conf:/var/www/html/mysite django:v1
#或者下载我打包好的镜像
docker pull registry.cn-qingdao.aliyuncs.com/docker1314/django:v5
#访问网址
10.0.0.11:88
docker安装FTP
docker run --name nginx --restart=always -p 80:80 --user=root -v ftp1:/usr/share/nginx/html -v /root/web/conf:/etc/nginx/conf.d -d nginx:latest
docker run -d -v ftp1:/home/vsftpd/root/ -e FTP_USER=root -e FTP_PASS=123456 -e PASV_ADDRESS=172.16.155.232 -e PASV_MIN_PORT=21100 -e PASV_MAX_PORT=21110 -p 20:20 -p 21:21 -p 21100-21110:21100-21110 --name vsftpd fauria/vsftpd:latest
vi 80.conf
server {
listen 80;
server_name _;
client_max_body_size 1024M;
location / {
root /usr/share/nginx/html/; # 站点目录
autoindex on; # 显示目录
charset utf-8,gbk; #展示中文文件名
autoindex_exact_size off; # 显示文件大小M ,默认on 显示字节数
autoindex_localtime on; # 显示服务器时间
#autoindex_format json; # html、xml、json、jsonp 分别用这几个风格展示目录
}
}
END
docker compose
介绍
官方文档
https://docs.docker.com/compose/compose-file/
安装docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version
启动
docker-compose up # 前台启动
docker-compose up -d # 后台启动
docker-compose -f docker-compose.yml up -d # 指定yaml文件启动
docker-compose ps # 查看容器状态
docker-compose stop # 停止
docker-compose restart # 重启
docker-compose down # 强制删除创建的容器不管运行没运行 也会删除创建的网卡
docker-compose rmi
docker-compose logs
docker-compose安装常用软件
新建一个空白目录,例如wptest。新建一个docker-compose.yml
主意: 启动mysql和redis要先编写配置文件,nginx+php要配置nginx的配置文件,并且php的站点目录要和nginx一致
version: "3"
services:
mysql:
environment:
MYSQL_ROOT_PASSWORD: "root"
image: "mysql:5.7"
container_name: mysql
networks:
- somenetwork
volumes:
- "${PWD}/mysql/data:/var/lib/mysql"
- "${PWD}/mysql/conf/my.cnf:/etc/mysql/my.cnf"
- "/etc/localtime:/etc/localtime"
ports:
- "3306:3306"
rabbitmq:
image: "rabbitmq:latest"
container_name: rabbitmq
environment:
- RABBITMQ_DEFAULT_USER=root
- RABBITMQ_DEFAULT_PASS=root
restart: always
depends_on:
- mysql
networks:
- somenetwork
ports:
- "15672:15672"
- "5672:5672"
redis:
image: "redis:latest"
container_name: redis
restart: always
networks:
- somenetwork
ports:
- 6379:6379
volumes:
- ${PWD}/redis/conf/redis.conf:/etc/redis/redis.conf:rw
- ${PWD}/redis/data/:/data:rw
- "/etc/localtime:/etc/localtime"
command:
# 执行的命令
redis-server /etc/redis/redis.conf --appendonly yes
tomcat:
image: "tomcat:9.0.0.M10"
container_name: tomcat
restart: always
depends_on:
- mysql
volumes:
- "/home/tomcat/webapps:/usr/local/tomcat/webapps"
- "tomcat-conf:/usr/local/tomcat/conf"
- "/home/tomcat/logs:/usr/local/tomcat/logs"
- "/etc/localtime:/etc/localtime"
networks:
- somenetwork
ports:
- "8080:8080"
jar-bao:
image: registry.cn-qingdao.aliyuncs.com/docker1314/openjdk-8-ttf-dejavu:v1
container_name: jar-bao
restart: always
networks:
- somenetwork
volumes:
- ${PWD}/jar/jeecg-boot-module-system-3.1.0.jar:/app.jar
- ${PWD}/jar/logs:/logs
- "/etc/localtime:/etc/localtime"
- "${PWD}/jar/webapp:/app/lf_product_manage/data/webapp"
- "${PWD}/jar/upload:/app/lf_product_manage/data/upload"
environment:
- TZ=Asia/Shanghai
command: java -jar -Dserver.port=8888 /app.jar
# apk add --no-cache ttf-dejavu 添加字体
ports:
- "8080:8888"
nginx:
image: "nginx"
container_name: nginx
restart: always
volumes:
- "${PWD}/nginx/html:/usr/share/nginx/html"
- "${PWD}/nginx/ditu:/usr/share/nginx/ditu/ditu"
- "${PWD}/nginx/conf:/etc/nginx/conf.d"
- "${PWD}/nginx/logs:/var/log/nginx"
- "/etc/localtime:/etc/localtime"
networks:
- somenetwork
ports:
- "80:80"
php:
image: "php:7.1.30-fpm"
container_name: php
restart: always
volumes:
- "/home/nginx/html:/www"
- "/etc/localtime:/etc/localtime"
networks:
- somenetwork
ports:
- "9000:9000"
tracker:
image: "delron/fastdfs"
networks:
- somenetwork
container_name: tracker
volumes:
- "/home/fdfs/tracker:/var/fdfs"
- "/etc/localtime:/etc/localtime"
ports:
- "22122:22122"
command:
tracker
storage:
environment:
- TRACKER_SERVER=tracker:22122
- GROUP_NAME=group1
image: "delron/fastdfs"
networks:
- somenetwork
container_name: storage
volumes:
- "/home/fdfs/storage:/var/fdfs"
- "/etc/localtime:/etc/localtime"
ports:
- "23000:23000"
- "8888:8888"
command:
storage
networks:
somenetwork:
driver: bridge
volumes:
tomcat-conf:
# 启动docker-compose 不需要创建网络somenetwork会自动创建
# 容器内互联 nginx: 和container_name: web 都能当域名用指向同一个地址
# 容器时间
- "/etc/localtime:/etc/localtime" 设置时间和服务器同步
- TZ=Asia/Shanghai 设置时区
# 当前路径 ${PWD}
# 启动停止
docker-compose -f docker-compose.yaml up -d
docker-compose -f docker-compose.yaml down
/home/nginx/conf/80.conf
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
/home/redis/conf/redis.conf
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /data/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
requirepass food
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
/home/mysql/conf/my.cnf
# 建库
create database app;
# 创建业务用户
create user admin@"%" identified by '123456';
grant all on app.* to admin@"%";
# 登录
mysql -uadmin -p123456
vi my.cnf
[mysqld]
# innodb_buffer_pool_size = 128M
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
default-authentication-plugin=mysql_native_password
skip-host-cache
server_id=6
skip-name-resolve
port=3306
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
lower_case_table_names=1
sql_mode='NO_AUTO_VALUE_ON_ZERO'
[client]
socket=/var/run/mysqld/mysqld.sock
参数解析
image镜像
image: redis # 使用哪个镜像
container_name容器名
container_name: rabbitmq # --name rabbitmq
command执行命令
command: bundle exec thin -p 3000 # 启动命令
links别名
links:
- db
- db:mysql
- redis:redis
使用了别名将自动会在容器的/etc/hosts文件里创建相应记录
- redis:red # redis是 services:标签下的redis: ,red是它的别名
- db 默认 等于 - db:db
external_links别名同网段
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
链接搭配docker-compose.yml文件或者Compose之外定义的服务,通常是提供共享或公共服务。格式与links相似
注意,external_links链接的服务与当前服务必须是同一个网络环境。
ports容器外端口
ports:
- "3000"
- "8000:8000"
- "49100:22"
- "127.0.0.1:8001:8001"
expose容器内端口
expose:
- "3000"
- "8000"
expose提供container之间的端口访问,不会暴露给主机使用。同docker run --expose。
volumes挂载
volumes:
- /var/lib/mysql
- cache/:/tmp/cache
- ~/configs:/etc/configs/:ro
volumes_from
volumes_from:
- service_name
- service_name:ro
- container:container_name
- container:container_name:rw
挂载数据卷容器,挂载是容器。同docker run --volumes-from
container:container_name格式仅支持version 2。
environment环境变量
environment:
RACK_ENV: development
SESSION_SECRET:
environment:
- RACK_ENV=development
- SESSION_SECRET
添加环境变量。同docker run -e。可以是数组或者字典格式
depends_on优先启动
depends_on:
- elasticsearch
- kibana
- redis
- logstash
用于指定服务依赖,一般是mysql、redis等。写在mysql、redis服务里面
指定了依赖,将会优先于服务创建并启动依赖
extra_hosts主机名映射
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
添加主机名映射
networks网络
volumes: # 以上每个服务中挂载映射的目录都在这里写入一次,也叫作声明volume
test1:
test2:
test3:
networks: # 创建桥接网络 如果创建的网卡没使用则不会创建 如果docker-compose down也会删除创建的网卡
my_net:
driver: bridge # 指定网卡类型桥接
myapp_net:
driver: bridge
# 网卡类型
net: "bridge"
net: "none"
net: "container:[name or id]"
net: "host"
dns
dns: 8.8.8.8
dns:
- 8.8.8.8
- 9.9.9.9
自定义dns服务器
其他参数
options:
max-size: "1000k"
max-file: "20"
cpu_shares: 73 # 服务容器相对于其他容器的 CPU 权重。
cpu_quota: 50000
cpuset: 0,1
cpu_count定义服务容器的可用 CPU 数量
cpu_percent定义可用 CPU 的可用百分比。
user: postgresql
working_dir: /code
domainname: foo.com
hostname: foo
ipc: host
mac_address: 02:42:ac:11:65:43
mem_limit: 1000000000
mem_limit: 128M
memswap_limit: 2000000000
privileged: true
restart: always
read_only: true
shm_size: 64M
stdin_open: true # 类似于docker run -d
tty: true # 类似于docker run -t
批量删除容器
# 关闭所有正在运行容器
docker ps | awk '{print }' | xargs docker stop
# 删除所有容器应用
docker ps -a | awk '{print }' | xargs docker rm
# 或者
docker rm $(docker ps -a -q)
111
111
11
111
END