Docker操作备忘
搭建私有仓库
$ docker pull registry:2.6.2
$ docker run -d --restart=always --privileged=true --name registry -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry:2.6.2
#安装仓库管理平台
$ docker run -d -e ENV_DOCKER_REGISTRY_HOST=127.0.0.1 -e ENV_DOCKER_REGISTRY_PORT=5000 -p 8080:80 konradkleine/docker-registry-frontend:v2
#初始化swarm
$ docker swarm init --advertise-addr 127.0.0.1
#强制退出swarm
$ docker swarm leave --force
$ docker node rm p8m9tlrjl1g52deopz1nq7efo
$ docker network create --driver overlay --subnet 10.10.10.0/24 cluster
$ docker network create --subnet 192.168.0.0/16 --gateway 192.168.0.1 --opt com.docker.network.bridge.name=docker_gwbridge --opt com.docker.network.bridge.enable_icc=false docker_gwbridge
#强制删除网络
$ docker network inspect docker_gwbridge
$ docker network disconnect -f docker_gwbridge gateway_ingress-sbox
$ docker network rm docker_gwbridge
常用镜像Push到本地仓库
# 1.拉取一个镜像并打tag
$ docker pull nginx:latest
$ docker tag nginx:latest 127.0.0.1:5000/nginx
# 2.提交tag镜像到自己的本地镜像仓库
$ docker push 127.0.0.1:5000/nginx
# 下载alpine-oraclejdk8
$ docker pull frolvlad/alpine-oraclejdk8:slim
$ docker tag frolvlad/alpine-oraclejdk8:slim 127.0.0.1:5000/frolvlad/alpine-oraclejdk8:slim
$ docker push 127.0.0.1:5000/frolvlad/alpine-oraclejdk8:slim
# 备注:frolvlad/alpine-oraclejdk8当前已无法下载,替换方案为frolvlad/alpine-oraclejre8
应用 Docker发布示例
后端Java应用发布
使用Dockerfile构建Docker镜像
在pom.xml所在目录创建Dockerfile文件
FROM 127.0.0.1:5000/frolvlad/alpine-oraclejdk8:slim
ADD ./myapp-api/target/myapp-api-*.jar myapp-api.jar
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/myapp-api.jar"]
EXPOSE 8080
Docker Build
$ docker build -t myapp-server -f ./myapp-api/Dockerfile .
$ docker tag myapp-server 127.0.0.1:5000/myapp-server
$ docker push 127.0.0.1:5000/myapp-server
发布服务
# 删除service
$ docker service rm myapp-server
#创建service
$ docker service create --detach=false --replicas 1 --limit-memory 1024M --name myapp-server --network=cluster --publish 16090:80 127.0.0.1:5000/myapp-server
# 查看日志
$ docker service logs myapp-server
前端Vue应用发布
使用Dockerfile构建Docker镜像
- 创建Dockerfile文件
from 127.0.0.1:5000/nginx:latest
COPY dist /usr/share/nginx/html/
ADD ./nginx.conf.template /etc/nginx/conf.d/nginx.conf.template
CMD envsubst < /etc/nginx/conf.d/nginx.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'
- nginx.conf.template
server {
listen 80;
server_name localhost;
client_max_body_size 40m;
client_body_buffer_size 4096k;
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
error_page 404 /index.html;
}
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
index index.html index.htm;
error_page 404 /index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
}
Docker build
$ docker build -t myapp-web .
$ docker tag myapp-web 127.0.0.1:5000/myapp-web
$ docker push 127.0.0.1:5000/myapp-web
发布服务
$ docker service rm myapp-web-test
$ docker service create --detach=false --replicas 1 --limit-memory 1024M --name myapp-web-test --network=cluster --publish 16080:80 127.0.0.1:5000/myapp-web
Docker Machine
$ docker-machine ls
$ docker-machine create -d virtualbox test
$ docker-machine create --driver hyperv --hyperv-virtual-switch=docker manager
其他
Windows下迁移目录
#创建链接
mklink /J "C:\Users\Public\Documents\Hyper-V\Virtual hard disks" D:\Hyper-V
常用命令
#节点
$ docker node ls
#网络
$ docker network ls
#容器
$ docker container ls
#镜像
$ docker image ls
# 强制删除镜像
$ docker image rmi dc5523692687 --force
#服务
$ docker service ls
$ docker service ps service_name
#进入容器
$ docker exec -it 69d1 bash
# 服务伸缩
$ docker service scale nginx=5
# 拷贝
docker cp RS-MapReduce 30026605dcfe:/home/cloudera
# 清理镜像
docker image prune
#登录阿里云Docker Registry
docker login --username=xxx registry.cn-shanghai.aliyuncs.com
用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码。
#从Registry中拉取镜像
docker pull registry.cn-shanghai.aliyuncs.com/xxx/alpine-oraclejdk8:slim
#将镜像推送到Registry
docker login --username=xxx registry.cn-shanghai.aliyuncs.com
docker tag frolvlad/alpine-oraclejdk8:slim registry.cn-shanghai.aliyuncs.com/xxx/alpine-oraclejdk8:slim
docker push registry.cn-shanghai.aliyuncs.com/xxx/alpine-oraclejdk8:slim