uniquejava / blog

My notes regarding the vibrating frontend :boom and the plain old java :rofl.
Creative Commons Zero v1.0 Universal
11 stars 5 forks source link

docker #211

Open uniquejava opened 6 years ago

uniquejava commented 6 years ago

设置代理

在~/.docker/daemon.json或/etc/docker/daemon.json 文件并添加上 registry-mirrors 键值然后重启。

 {
   "experimental" : false,
   "debug" : true,
   "registry-mirrors": [
-      "https://registry.docker-cn.com",
+      "https://8nzelxxxx.mirror.aliyuncs.com"
    ]
 }

Dockerfile build==> Image run==> Container

清理没有tag的image

According to the docker documentation you can list only untagged (dangling) images with

$ docker images -f "dangling=true"

and redirect them to docker rmi command like that:

$ docker rmi $(docker images -f "dangling=true" -q) --force

Notice -q param thats only show numeric IDs of containers.

镜像CRUD操作

查找镜像: docker search tomcat
本地查找: docker images|grep tomcat
拉取镜像: docker pull tomcat
创建镜像 docker build -t hello-world .
基于运行中的container创建新镜像
docker commit -a "xxx.com" -m "my image" container_id tomcat:v1
检查镜像 docker images tomcat:v1
删除镜像 docker rmi
备份/导出镜像 docker save tomcat:v1 > /Users/xxx/xxx/tomcat_v1.tar
还原镜像 docker load < /Users/xxx/xxx/tomcat_v1.tar

文件拷贝 (不用启动container、爽得1p)

实例 将主机/www/runoob目录拷贝到容器96f7f14e99ab的/www目录下。 docker cp /www/runoob 96f7f14e99ab:/www/

将主机/www/runoob目录拷贝到容器96f7f14e99ab中,目录重命名为www。 docker cp /www/runoob 96f7f14e99ab:/www

将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中。

docker cp 96f7f14e99ab:/www /tmp/

容器CRUD操作

创建容器: docker create 
创建并运行容器: docker run --name container1  image_name
创建nginx容器: 
docker run --name nginx1 -p 80:80 -v $PWD/test:/usr/share/nginx/html/test -d nginx:1.12.2

创建tomcat容器
docker run --name tomcat1 -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat
docker run --name tomcat2 -p 8090:8090 -v $PWD/test1:/usr/local/tomcat/webapps/test1 -d tomcat

其它: docker container
显示运行中的容器: ps
显示全部容器: ps -a 或 ls -a
删除容器 rm tomcat1
启动容器 start tomcat1
停止容器 stop tomcat1
深入容器 exec -it tomcat1 /bin/bash
杀死所有正在运行的容器 alias dockerkill='docker kill $(docker ps -q)'
删除所有已经停止的容器 alias dockercleanc='rm $(docker ps -a -q)'
  # -q, --quiet  Only display numeric IDs
删除所有未打标签的镜像:
alias dockercleani='docker rmi $(docker images -q -f dangling=true)'
删除所有未运行的docker容器
docker rm $(docker ps -a -q)
删除所有未打tag的镜像
docker rmi $(docker images -q|awk '/^<none>/ {print $3}')
删除所有镜像
docker rmi $(docker images -q)

根据格式删除所有镜像
docker rm $(docker ps -qf status=exited)

综合实例, 拉建跑(RHEL MongoDB)

docker pull registry.access.redhat.com/rhscl/mongodb-34-rhel7

docker create --name mongodb \
-e MONGODB_ADMIN_PASSWORD=xxxxxxx \
-e MONGODB_USER=xxx \
-e MONGODB_PASSWORD=xxx \
-e MONGODB_DATABASE=xxxxxxx \
-p 27017:27017 \
-v /var/lib/mongodb/data \
registry.access.redhat.com/rhscl/mongodb-34-rhel7

docker start mongodb
firewall-cmd --permanent --zone=ibm --add-port=27017/tcp
systemctl stop firewalld.service
systemctl start firewalld.service

Learn Docker in 12 Minutes Get started with Docker for Mac How to Dockerize a Node.js application

imooc免费课程

src/index.php

<?php
echo "hello word"

Dockerfile (Writing a Dockerfile)

FROM php:7.0-apache COPY src/ /var/www/html/ EXPOSE 80

# Building an image
docker build -t hello-world .
# Running a container
docker run -p 80:80 hello-world
curl http://localhost

# Mounting volumes (from host to container)
docker run -p 80:80 -v /Users/cyper/Desktop/docker/src/:/var/www/html/ hello-world

. 命令行登录报错

docker login Unable to docker login through CLI - unauthorized: incorrect username or password 解决: docker id不要使用email, 用登录docker.com后右上角显示的用户名。

docker run --name wex1 -v /Users/cyper/esadmin/data:/data -p 8390:8390 -p 8393:8393 -td wex_dae_ac

uniquejava commented 6 years ago

关于Docker目录挂载的总结

http://www.cnblogs.com/ivictor/p/4834864.html (总结的真好!)

我的经验, 当container中的某个目录比如/data存在很多文件的时候(APP) 第一次RUN时不要使用-v参数, 使用不带-v参数的RUN命令跑成功后, 将那个/data目录通过docker cp container_name:/data/ /Users/cyper/xx_data命令拷贝到本地. 然后删除这个container, 最后加上-v /Users/cyper/xx_data:/data 参数重新RUN这个image.

How to get into a docker container?

docker exec -it container_id_or_name command 这里的command可以是/bin/bash, 可以是whoamienv等等任意可以在container的terminal中执行的命令。 例: docker exec -it wex1 ls -a /root 见: https://stackoverflow.com/questions/30172605/how-to-get-into-a-docker-container

查看容器启动日志

docker logs -f tomcat1
docker logs --tail 50 --follow --timestamps tomcat1

查看容器IP地址信息

docker inspect <container id> | grep "IPAddress"
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host

Run docker container with non-root user

https://medium.com/@mccode/processes-in-containers-should-not-run-as-root-2feae3f0df3b

How to synchronize host folder in container folder with Docker

http://docker-sync.io/

Docker: How to live sync host folder with container folder?

uniquejava commented 6 years ago

WEX 12的docker image

docker load < WEX_DAE_AC_V12.0.0_LNX_ML.DockerImage.tar
docker images
docker run --name wex1 -p 8390:8390 -p 8393:8393 -td wex_dae_ac
docker logs wex1 
# 文件copy
docker cp wex1:/data /Users/cyper/esadmin/data
# 直接进bash内部
docker exec -it wex1 /bin/bash
docker container rm/start/stop wex1

## docker compose
docker-compose up

docker-compose.yml

```yml
version: '3'
services:
  mysqldb:
    image: mysql:5.7
    container_name: mysqldb
    ports:
      - "3306:3306"
    environment:
     MYSQL_ROOT_PASSWORD: admin
     MYSQL_DATABASE: catalog

MySQL错误: https://github.com/passbolt/passbolt_docker/issues/103

you have to remove the volumes before changing the mysql versions.

docker-compose rm
docker volume rm list_of_your_volumes
docker-compose up
uniquejava commented 5 years ago

安装 docker

Install from Docker (Official)

# 删除旧版本
$ yum  -y remove  docker-common docker container-selinux docker-selinux docker-engine
$ yum -y install  wget
$ wget https://download.docker.com/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

$ yum -y install docker-ce
$ systemctl start docker

详见: https://www.itzgeek.com/how-tos/linux/centos-how-tos/installing-docker-on-centos-7-rhel-7-fedora-21.html

不用 sudo即可使用docker命令的办法, 将当前用户加入docker组.

Run this command in your favourite shell and then completely log out of your account and log back in (if in doubt, reboot!):

sudo usermod -a -G docker $USER

什么是docker-proxy

https://windsock.io/the-docker-proxy/

uniquejava commented 4 years ago

修改docker disk image location

https://stackoverflow.com/questions/24309526/how-to-change-the-docker-image-installation-directory

指定docker volume create的路径

https://stackoverflow.com/questions/31747061/creating-a-docker-volume-at-a-specific-location

编写dockerfile的最佳实践

https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

Use multi-stage builds

https://docs.docker.com/develop/develop-images/multistage-build/

uniquejava commented 4 years ago

Best Practice

## 只用lts版本(偶数版本, 不要用latest TAG)
## 尽量用alpine (少包含很多linux命令, 体积小很多),不要用slim
## Alpine用的apk, slim用的apt作为包管理工具
## Alpine CVE scanning fails?
FROM node:10-alpine

EXPOSE 3000

RUN apk add --update curl

# WORKDIR能自动创建dir并CD, 不要使用RUN mkdir
WORKDIR /usr/src/app 

# 不要用ADD (除非你知道为什么要用 -- ADD可以自动下载, 解压, 大多数情况下COPY就够了)
COPY package.json package-lock.json* ./

RUN npm install && npm cache clean --force

COPY . .

CMD [ "node", "./bin/www" ]