Docker安装
# 清理:
sudo apt-get remove -y docker docker-engine docker.io
# 安装依赖:
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common
# 信任 Docker 的 GPG 公钥:
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
# 添加软件仓库:
sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian \
$(lsb_release -cs) \
stable"
# 安装
sudo apt-get update -y
sudo apt-get install -y docker-ce
# 设置加速镜像
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
"registry-mirrors": [
"https://ustc-edu-cn.mirror.aliyuncs.com/",
"https://hub-mirror.c.163.com/"
]
}
EOF
重启 docker 并设置开机自启
sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker
允许运行普通账号直接执行 docker 命令
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
容器自启:
docker ps -a
开启开机启动,不用考虑容器的状态是否为运行或者其他
docker update --restart=always 容器id
关闭开机启动,不用考虑容器的状态是否为运行或者其他
docker update --restart=no 容器id
容器ID:
docker inspect mysql | grep IPAddress #mysql是对应的容器名字
卸载旧版本:
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
CentOS 脚本安装:
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun
yum 安装:
安装依赖包:
$ sudo yum install -y yum-utils
添加 yum
软件源:
$ sudo yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
$ sudo sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo
# 官方源
# $ sudo yum-config-manager \
# --add-repo \
# https://download.docker.com/linux/centos/docker-ce.repo
安装 docker-ce:
$ sudo yum install docker-ce docker-ce-cli containerd.io
CentOS8 额外设置:
vi /etc/firewalld/firewalld.conf
# FirewallBackend=nftables
FirewallBackend=iptables
启动Docker:
$ sudo systemctl enable docker
$ sudo systemctl start docker
建立 docker 用户组:
$ sudo groupadd docker
将当前用户加入 docker
组:
$ sudo usermod -aG docker $USER
测试 Docker 是否安装正确:
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
镜像加速:
vi /etc/docker/daemon.json
{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
重新启动服务:
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
Windows 10:
对于使用 Windows 10
的用户,在任务栏托盘 Docker 图标内右键菜单选择 Settings
,打开配置窗口后在左侧导航菜单选择 Docker Engine
,在右侧像下边一样编辑 json 文件,之后点击 Apply & Restart
保存后 Docker 就会重启并应用配置的镜像地址了。
{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
macOS:
对于使用 macOS 的用户,在任务栏点击 Docker Desktop 应用图标 -> Perferences
,在左侧导航菜单选择 Docker Engine
,在右侧像下边一样编辑 json 文件。修改完成之后,点击 Apply & Restart
按钮,Docker 就会重启并应用配置的镜像地址了。
{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
检查加速器是否生效:
docker info
#出现,则成功
Registry Mirrors:
https://hub-mirror.c.163.com/
Mysql
访问 MySQL 镜像库地址:Docker Hub
查看可用版本:docker search mysql
默认是最新版本 mysql:latest
# docker 中下载 mysql
docker pull mysql:latest
#启动(选一种)
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
docker run -p 3306:3306 --name mysql5 \
-v /mydata/mysql5/log:/var/log/mysql \
-v /mydata/mysql5/data:/var/lib/mysql \
-v /mydata/mysql5/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7 --lower_case_table_names=1
参数说明:
- -p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
- MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码。
- -v 映射容器内部文件到本地
#进入容器
docker exec -it mysql bash
#登录mysql
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
#添加远程登录用户
CREATE USER 'cool'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'cool'@'%';
Redis
访问 Redis 镜像库地址: Docker Hub
查看可用版本:docker search redis
## 拉取
docker pull redis:latest
## 查看本地镜像
docker images
## 运行容器
docker run -itd --name redis-test -p 6379:6379 redis
## 安装成功
docker ps
## redis-cli 连接测试使用 redis 服务
docker exec -it redis-test /bin/bash
参数说明:
- -p 6379:6379:映射容器服务的 6379 端口到宿主机的 6379 端口。外部可以直接通过宿主机ip:6379 访问到 Redis 的服务。
###映射文件到本地
1.拉取镜像
docker pull redis
2.查看镜像
docker images
3.本地配置生成
mkdir -p /mydata/redis/conf
touch /mydata/redis/conf/redis.conf
echo "appendonly yes" >> /mydata/redis/conf/redis.conf
4.生成容器
docker run -p 6379:6379 --name redis -v /mydata/redis/data:/data -v/mydata/redis/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf
5.设置docker容器自启
docker update redis --restart=always
Nginx
访问 Nginx 镜像库地址: Docker Hub
docker search nginx 命令来查看可用版本
基础安装:
docker pull nginx:latest
docker images
docker run --name nginx-test -p 8080:80 -d nginx
docker ps -a
## IP:8080
参数说明:
- --name nginx-test:容器名称。
- -p 8080:80: 端口进行映射,将本地 8080 端口映射到容器内部的 80 端口。
- -d nginx: 设置容器在在后台一直运行。
本地映射安装:
##1.拉取镜像
docker pull ngnix
##2.查看镜像
docker images
##3.本地配置生成
mkdir /mydata/nginx/conf
docker run -p 80:80 --name nginx -d nginx
docker container cp nginx:/etc/nginx /mydata/nginx/conf
docker stop nginx
docker rm nginx
##4.生成容器
docker run -p 80:80 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf:/etc/nginx \
-d nginx
##5.设置docker容器自启
docker update nginx --restart=always
Node.js
访问 Node 镜像库地址: Docker Hub
docker pull node:latest
docker images
## name node-test:容器名称。
docker run -itd --name node-test node
docker exec -it node-test /bin/bash
node -v
CentOS
访问 CentOS 镜像库地址:Docker Hub
docker pull centos:latest
docker images
docker run -itd --name centos-test centos:centos
docker ps
Ubuntu
访问 Ubuntu 镜像库地址: Docker Hub
docker pull ubuntu:latest
docker images
docker run -itd --name ubuntu-test ubuntu
docker ps
Tomcat
查找 Docker Hub 上的 Tomcat 镜像:
docker search tomcat
docker pull tomcat
docker images|grep tomcat
docker run --name tomcat -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat
## 命令说明:
-p 8080:8080:将主机的 8080 端口映射到容器的 8080 端口。
-v $PWD/test:/usr/local/tomcat/webapps/test:将主机中当前目录下的 test 挂载到容器的 /test。
docker ps
## 浏览器访问: IP:8080
Apache
查找 Docker Hub 上的 httpd 镜像:
docker search httpd
docker pull httpd
docker images httpd
docker run -p 80:80 -v $PWD/www/:/usr/local/apache2/htdocs/ -v $PWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf -v $PWD/logs/:/usr/local/apache2/logs/ -d httpd
## 命令说明:
-p 80:80: 将容器的 80 端口映射到主机的 80 端口。
-v $PWD/www/:/usr/local/apache2/htdocs/: 将主机中当前目录下的 www 目录挂载到容器的 /usr/local/apache2/htdocs/。
-v $PWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf: 将主机中当前目录下的 conf/httpd.conf 文件挂载到容器的 /usr/local/apache2/conf/httpd.conf。
-v $PWD/logs/:/usr/local/apache2/logs/: 将主机中当前目录下的 logs 目录挂载到容器的 /usr/local/apache2/logs/。
docker ps
## 浏览器访问IP:80
MongoDB
访问 MongoDB 镜像库地址: https://hub.docker.com/_/mongo?tab=tags&page=1
docker search mongo
docker pull mongo:latest
docker images
docker run -itd --name mongo -p 27017:27017 mongo --auth
## 参数说明:
-p 27017:27017 :映射容器服务的 27017 端口到宿主机的 27017 端口。外部可以直接通过 宿主机 ip:27017 访问到 mongo 的服务。
--auth:需要密码才能访问容器服务。
docker ps
$ docker exec -it mongo mongo admin
# 创建一个名为 admin,密码为 123456 的用户。
> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
# 尝试使用上面创建的用户信息进行连接。
> db.auth('admin', '123456')
##本地映射
docker run -d --restart=always -p 27017:27017 --name mymongo -v /data/db:/data/db -d mongo
#进入容器
docker exec -it mymongo /bin/bash
#使用MongoDB客户端进行操作
mongo
#查询所有的数据库
show dbs
elasticsearch(适用版本7)
## 拉取镜像
docker pull elasticsearch:7.4.2
## 映射配置
mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data
echo "http.host: 0.0.0.0" > /mydata/elasticsearch/config/elasticsearch.yml
cat /mydata/elasticsearch/config/elasticsearch.yml
## 容器生成
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 --privileged=true -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms128m -Xms256m" -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins -d elasticsearch:7.4.2
## 可能遇到权限不够
chmod 777 /mydata/elasticsearch/data/
docker run -p 9200:9200 -p 9300:9300 --name es7.8.1 \
--privileged=true \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-v /dockerfile/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /dockerfile/elasticsearch/data:/usr/share/elasticsearch/data \
-v /dockerfile/elasticsearch/logs:/usr/share/elasticsearch/logs \
-d elasticsearch:7.8.1
Kibana
docker pull kibana:7.4.2
## 地址为elasticsearch地址
docker run --name kibana -e ELASTICSEARCH_HOSTS=http://172.16.3.108:9200 -p 5601:5601 -d kibana:7.4.2
Oracle11g
1.拉取镜像
#1》
docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
#2》
docker pull oracleinanutshell/oracle-xe-11g:latest
2.查看镜像
docker images
3.新建容器
#1.1》
docker run -it -d -p 1521:1521 -v /data/oracle:/data/oracle --name oracle11 registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
#1.2》
docker run --name oracle -d -v /mydata/oracle/oracle_data:/data/oracle_data -p 49160:22 -p 1521:1521 -e ORACLE_ALLOW_REMOTE=true -e ORACLE_DISABLE_ASYNCH_IO=true docker.io/oracleinanutshell/oracle-xe-11g
#2》
docker run --name oracle -d -v /home/docker/data/oracle_data:/data/oracle_data -p 49160:22 -p 1521:1521 -e ORACLE_ALLOW_REMOTE=true -e ORACLE_DISABLE_ASYNCH_IO=true docker.io/oracleinanutshell/oracle-xe-11g
4.运行容器
docker start oracle11g
5.进入容器
docker exec -it oracle11g bash
6.使用root用户
su root
7.修改文件,直接加末尾
export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_2
#1》
export ORACLE_SID=helowin
或
#2》
export ORACLE_SID=xe
export PATH=$ORACLE_HOME/bin:$PATH
8.刷新配置
source /etc/profile
9.软连接
ln -s $ORACLE_HOME/bin/sqlplus /usr/bin
10.切换oracle用户
su - oracle
11.登录
sqlplus /nolog
12.连接
conn /as sysdba
13.修改密码
alter user system identified by system;
alter user sys identified by sys;
14.创建用户
create user dev identified by dev;
// 赋予权限
grant connect,resource,dba to dev;
15.退出
exit;
nacos
1.拉取
docker pull nacos/nacos-server
2.查看镜像
docker images
3.查看docker mysql Ip
docker inspect mysql | grep IPAddress #mysql是对应的容器名字
4.本地创建映射文件
mkdir /mydata/nacos/logs -p
mkdir /mydata/nacos/conf -p
vim /mydata/nacos/conf/application.properties
5.配置文件内容/*application.properties 配置文件*/
/*application.properties 配置文件*/
#
# Copyright 1999-2018 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#*************** Spring Boot Related Configurations ***************#
### Default web context path:
server.servlet.contextPath=/nacos
### Default web server port:
server.port=8848
#*************** Network Related Configurations ***************#
### If prefer hostname over ip for Nacos server addresses in cluster.conf:
# nacos.inetutils.prefer-hostname-over-ip=false
### Specify local server's IP:
# nacos.inetutils.ip-address=
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
#连接mysql8.0数据库
### Connect URL of DB:
db.url.0=jdbc:mysql://172.17.0.3:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&useSSL=false
db.user.0=root
db.password.0=root
### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2
#*************** Naming Module Related Configurations ***************#
### Data dispatch task execution period in milliseconds:
# nacos.naming.distro.taskDispatchPeriod=200
### Data count of batch sync task:
# nacos.naming.distro.batchSyncKeyCount=1000
### Retry delay in milliseconds if sync task failed:
# nacos.naming.distro.syncRetryDelay=5000
### If enable data warmup. If set to false, the server would accept request without local data preparation:
# nacos.naming.data.warmup=true
### If enable the instance auto expiration, kind like of health check of instance:
# nacos.naming.expireInstance=true
nacos.naming.empty-service.auto-clean=true
nacos.naming.empty-service.clean.initial-delay-ms=50000
nacos.naming.empty-service.clean.period-time-ms=30000
#*************** CMDB Module Related Configurations ***************#
### The interval to dump external CMDB in seconds:
# nacos.cmdb.dumpTaskInterval=3600
### The interval of polling data change event in seconds:
# nacos.cmdb.eventTaskInterval=10
### The interval of loading labels in seconds:
# nacos.cmdb.labelTaskInterval=300
### If turn on data loading task:
# nacos.cmdb.loadDataAtStart=false
#*************** Metrics Related Configurations ***************#
### Metrics for prometheus
#management.endpoints.web.exposure.include=*
### Metrics for elastic search
management.metrics.export.elastic.enabled=false
#management.metrics.export.elastic.host=http://localhost:9200
### Metrics for influx
management.metrics.export.influx.enabled=false
#management.metrics.export.influx.db=springboot
#management.metrics.export.influx.uri=http://localhost:8086
#management.metrics.export.influx.auto-create-db=true
#management.metrics.export.influx.consistency=one
#management.metrics.export.influx.compressed=true
#*************** Access Log Related Configurations ***************#
### If turn on the access log:
server.tomcat.accesslog.enabled=true
### The access log pattern:
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
### The directory of access log:
server.tomcat.basedir=
#*************** Access Control Related Configurations ***************#
### If enable spring security, this option is deprecated in 1.2.0:
#spring.security.enabled=false
### The ignore urls of auth, is deprecated in 1.2.0:
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-ui/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
### The auth system to use, currently only 'nacos' is supported:
nacos.core.auth.system.type=nacos
### If turn on auth system:
nacos.core.auth.enabled=false
### The token expiration in seconds:
nacos.core.auth.default.token.expire.seconds=18000
### The default token:
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=true
### Since 1.4.1, Turn on/off white auth for user-agent: nacos-server, only for upgrade from old version.
nacos.core.auth.enable.userAgentAuthWhite=true
### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=
nacos.core.auth.server.identity.value=
#*************** Istio Related Configurations ***************#
### If turn on the MCP server:
nacos.istio.mcp.server.enabled=false
###*************** Add from 1.3.0 ***************###
#*************** Core Related Configurations ***************#
### set the WorkerID manually
# nacos.core.snowflake.worker-id=
### Member-MetaData
# nacos.core.member.meta.site=
# nacos.core.member.meta.adweight=
# nacos.core.member.meta.weight=
### MemberLookup
### Addressing pattern category, If set, the priority is highest
# nacos.core.member.lookup.type=[file,address-server]
## Set the cluster list with a configuration file or command-line argument
# nacos.member.list=192.168.16.101:8847?raft_port=8807,192.168.16.101?raft_port=8808,192.168.16.101:8849?raft_port=8809
## for AddressServerMemberLookup
# Maximum number of retries to query the address server upon initialization
# nacos.core.address-server.retry=5
## Server domain name address of [address-server] mode
# address.server.domain=jmenv.tbsite.net
## Server port of [address-server] mode
# address.server.port=8080
## Request address of [address-server] mode
# address.server.url=/nacos/serverlist
#*************** JRaft Related Configurations ***************#
### Sets the Raft cluster election timeout, default value is 5 second
# nacos.core.protocol.raft.data.election_timeout_ms=5000
### Sets the amount of time the Raft snapshot will execute periodically, default is 30 minute
# nacos.core.protocol.raft.data.snapshot_interval_secs=30
### raft internal worker threads
# nacos.core.protocol.raft.data.core_thread_num=8
### Number of threads required for raft business request processing
# nacos.core.protocol.raft.data.cli_service_thread_num=4
### raft linear read strategy. Safe linear reads are used by default, that is, the Leader tenure is confirmed by heartbeat
# nacos.core.protocol.raft.data.read_index_type=ReadOnlySafe
### rpc request timeout, default 5 seconds
# nacos.core.protocol.raft.data.rpc_request_timeout_ms=5000
6.生成容器
docker run --name nacos -p 8848:8848 \
--privileged=true \
--restart=always \
-e JVM_XMS=128m \
-e JVM_XMX=128m \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-v /mydata/nacos/logs:/home/nacos/logs \
-v /mydata/nacos/conf/application.properties:/home/nacos/conf/application.properties \
-d nacos/nacos-server:latest
minio
docker run -d -p 9000:9000 -p 9001:9001 -v /data/minio/data:/data -v /data/minio/config:/root/.minio -e MINIO_ROOT_USER=admin -e MINIO_ROOT_PASSWORD=passwd minio/minio:latest server /data --console-address :9001
注意:所有命令请用root权限