使用Dockerfile安装软件,docker安装不同软件
部署java项目 创建Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 # 基础镜像是docker仓库的java:8(也就是jdk8的意思) FROM java:8 # 作者签名 MAINTAINER xiaoxiang # 简化 jar 的名字路径 (左边是服务器中jar包的路径,因为当前文件是和jar包同级,所以直接写名字即可,右边是镜像中要存放jar包的路径) COPY test.jar /test.jar # 设置时间 RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone # 执行 java -jar 命令 (CMD:在启动容器时才执行此行。RUN:构建镜像时就此行,后面的jar包路径就是上面要设置的jar包路径) CMD java -jar /test.jar # 设置对外端口为 8080 EXPOSE 8080
创建镜像
1 2 # 后面有个点,指定路径为当前路径的Dockerfile文件 docker build -t test .
运行jar包
-d代表后台运行
-p 8082:8082 端口映射,后一个8082是镜像的端口
-v 代表挂载一个宿主机目录到容器目录中,第一个是宿主机目录,第二个是容器目录
第一个test/java代表是给容器的名字
第二个test是上面创建镜像的名字
1 docker run -d -p 8080:8080 -v /opt/java:/opt/java --name test/java test
查看容器
安装Mysql 1 mkdir -p /opt/data/mysql/data
1 mkdir -p /opt/data/mysql/conf.d
1 docker run -di -p 3307:3306 -v /opt/data/mysql/conf.d:/etc/mysql/conf.d -v /opt/data/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=密码 --name mysql mysql
安装Redis 1 mkdir -p /opt/data/redis/data
1 mkdir -p /opt/data/redis/conf
1 vim /opt/data/redis/conf/redis.conf
1 docker run -d -p 6379:6379 --name redis -v /opt/data/redis/redis.conf:/etc/redis/redis.conf -v /opt/data/redis/data:/data redis redis-server /etc/redis/redis.conf --requirepass "密码" --appendonly yes
redis.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 protected-mode yes port 6379 tcp-backlog 511 timeout 0 daemonize yes supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/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 /var/lib/redis 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 requirepass 密码 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 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
安装布隆过滤器Redis 其他配置同上
1 2 3 4 5 6 docker run -p 6379:6379 \ --name redis-bloom \ -v /opt/data/redis/redis.conf:/etc/redis/redis.conf -v /opt/data/redis/data:/data \ redislabs/rebloom:2.2.2 \ /etc/redis/redis.conf --requirepass "密码" --appendonly yes \ --loadmodule "/usr/lib/redis/modules/redisbloom.so"
安装nginx 1 mkdir -p /opt/data/nginx/{conf,html,logs}
1 vim /usr/nginx/conf/nginx.conf
1 docker run --name nginx -d -p 80:80 -p 443:443 -v /opt/data/nginx/html:/usr/share/nginx/html:ro -v /opt/data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /opt/data/nginx/conf.d:/etc/nginx/conf.d:ro -v /opt/data/nginx/logs:/var/log/nginx nginx
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # location / { # } # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } }
安装nacos 使用挂载文件的方式
1 docker run -d --name nacos -v /opt/nacos/conf/application.properties:/home/nacos/conf/application.properties -p 8848:8848 nacos/nacos-server
application.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 server.servlet.contextPath =/nacos server.port =8848 nacos.naming.empty-service.auto-clean =false nacos.naming.empty-service.clean.initial-delay-ms =60000 nacos.naming.empty-service.clean.period-time-ms =20000 management.metrics.export.elastic.enabled =false management.metrics.export.influx.enabled =false server.tomcat.accesslog.enabled =true server.tomcat.accesslog.pattern =%h %l %u %t "%r" %s %b %D %{User-Agent}i server.tomcat.basedir =nacos.security.ignore.urls =/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/** nacos.core.auth.system.type =nacos nacos.core.auth.enabled =false nacos.core.auth.default.token.expire.seconds =18000 nacos.core.auth.default.token.secret.key =SecretKey012345678901234567890123456789012345678901234567890123456789 nacos.core.auth.caching.enabled =false nacos.istio.mcp.server.enabled =false spring.datasource.platform =mysql db.num =1 db.url.0 =jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user =root db.password =123456
安装elasticSearch 安装
1 docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.6.2
配置跨域和密码
1 2 # 进入容器 docker exec -it es /bin/bash
1 2 # 编辑文件 vi config/elasticsearch.yml
1 2 3 4 5 6 7 8 9 10 11 # 名称,保证唯一 cluster.name: es-1# 为本机ip地址,docker中为0.0.0.0 network.host: 0.0.0.0# 打开跨域,选填 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-headers: Authorization# 开启安全认证,选填 xpack.security.enabled: true xpack.security.transport.ssl.enabled: true
1 2 # 重启镜像 docker restart es
1 2 3 # 设置密码,进入bin目录 # 需要为4个用户分别设置密码:elastic、kibana、logstash_system、beats_system cd /usr/local/kibana-7.6.2-linux-x86_64/bin && ./elasticsearch-setup-passwords interactive
1 2 # 浏览器查看 192.168.0.18:9200
安装中文分词 GitHub下载
1 2 3 # 下载后解压移动到/usr/share/elasticsearch/plugins,重启 docker cp elasticsearch-analysis-ik-7.8.0/ es:/usr/share/elasticsearch/plugins docker restart es
安装Kibana 创建配置文件
1 2 3 4 5 6 7 8 9 server.name: kibana server.host: "0" elasticsearch.hosts: [ "http://127.0.0.1:9200" ]xpack.monitoring.ui.container.elasticsearch.enabled: true elasticsearch.username: "elastic" elasticsearch.password: "xxx"
安装
1 2 3 4 docker run -d --log-driver json-file --log-opt max-size=100m --log-opt max-file=2 --name kibana -p 5601:5601 -v /opt/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml kibana:7.4.1# 浏览器查看 127.0.0.1:5601
安装Oracle11g 下载Oracle 11g R2安装包
1 2 3 4 5 6 # 创建目录 mkdir -p /opt/oracle/install/database# 解压下载的文件 unzip -d linux.x64_11gR2_database_1of2.zip /opt/oracle/install/database unzip -d linux.x64_11gR2_database_2of2.zip /opt/oracle/install/database
1 2 # 安装,安装过程很久,可能会卡在终端,可以新开一个终端查看是否安装完成 docker run --privileged --name oracle -p 1521:1521 -v /opt/oracle/install/database:/install docker.io/jaspeen/oracle-11g
1 2 3 4 5 6 # 进入容器 docker exec -it oracle11g /bin/bash# 切换到oracle用户 su - oracle# 进入数据库 sqlplus / as sysdba
1 2 3 4 5 create user username identified by password;grant create session,create table ,create sequence,unlimited tablespace to username;
出现数据库不能连接错误
拷贝容器中的timezone文件到本地
1 2 # 第一个名字是容器和路径,第二个点代表拷贝到当前目录下 docker cp test/java:/etc/timezone .
修改
放回原位
1 2 # 在文件所在的目录下拷贝到容器的 /etc/下 docker cp timezone 容器名称:/etc/
Docker访问宿主机 1 vim /etc/firewalld/zones/public.xml
添加
1 2 3 4 <rule family ="ipv4" > <source address ="172.17.0.0/16" /> <accept /> </rule >
重启防火墙
1 systemctl restart firewalld
Docker命令自动补全 1 2 3 4 5 yum install -y bash-completion source /usr/share/bash-completion/bash_completion source /usr/share/bash-completion/completions/docker
设置自启 设置自启动
1 docker update --restart=always 容器id
关闭自启动
1 docker update --restart=no 容器id
关闭所有自启动
1 docker update --restart=no $(docker ps -q)
相关文章
Docker
Docker高级
Docker安装Alist+Aria2+Qbittorrentee
Docker设置代理
国内可用Docker镜像仓库
Docker提示没权限