容器制作

安装Docker

卸载旧的版本

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

安装工具

yum install -y yum-utils

设置镜像的仓库(国内阿里云镜像地址)

yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新yum软件包索引

yum makecache fast

安装Docker相关的内容 ce 社区版,ee 企业版

yum install docker-ce docker-ce-cli containerd.io

启动docker

systemctl start docker

docker随系统启动

systemctl enable docker

配置阿里云镜像加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://lyywjmta.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

安装Docker-compose

下载

curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

授权

chmod +x /usr/local/bin/docker-compose

查看是否安装成功

docker-compose version

安装 Project

创建docker-compse目录

mkdir /home/docker-compose-jully

创建数据卷目录

mkdir -p /home/jully-volumes/main/{logs,tomcat/logs} /home/jully-volumes/{attachments,sync}

下载docker-compose.yml文件

首先定位到 /home/docker-compose-jully 路径下

curl http://www.jullyupdate.cn/docker/compose/download>>docker-compose.yml

修改docker-compose.yml文件

设置客户ID参数

#1.检查配置是否正确
[root@localhost docker-compose-jully]# docker-compose config
#如果出现如下警告,则表明没有设置客户ID
WARNING: The JULLY_CUSTOMER_ID variable is not set. Defaulting to a blank string.
​
#2.指定客户ID,在profile的最后增加如下语句
[root@localhost docker-compose-jully]# vim /etc/profile
export JULLY_CUSTOMER_ID=DOCKER_01
​
#3.生效环境变量
[root@localhost docker-compose-jully]# source /etc/profile
​
#4.查看环境变量
[root@localhost docker-compose-jully]# echo $JULLY_CUSTOMER_ID
DOCKER_01

修改timezon

#设置timezone,(以Asia/Shanghai为例)
#1.先查看当前宿主机的timezone
[root@localhost jully-volumes]# cat /etc/timezone 
cat: /etc/timezon: No such file or directory
#2.如果该文件不存在,则需要写入
[root@localhost jully-volumes]# echo 'Asia/Shanghai' >/etc/timezone

修改yml文件

version : '3.8'
​
services:
  main:
     image: registry.cn-zhangjiakou.aliyuncs.com/jully/main
     restart: always
     depends_on:
       - database                               # 如果使用外部数据库,则需要删除该依赖
       - redis                                  # 必须项:docker一律使用redis
     environment:
       JULLY_CUSTOMER_ID: @{JULLY_CUSTOMER_ID}  # 必须项:客户端ID,可以通过export指定,也可以直接修改
       JULLY_DOCKER_MODE: 1                     # 必须项:固定1
# 指定外部数据库信息
#      JULLY_DATASOURCE_DRIVER_CLASS_NAME_jully: com.mysql.cj.jdbc.Driver
#      JULLY_DATASOURCE_URL_jully: jdbc:mysql://localhost:3306/jully?serverTimezone=GMT%2B8&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL
#      JULLY_DATASOURCE_USERNAME_jully: jully
#      JULLY_DATASOURCE_PASSWORD_jully: jully
#      JAVA_OPTS: "-Xms512m -Xmx2048m"
     volumes:
       - "/sys:/home/host/sys:ro"
       - "/etc/localtime:/etc/localtime:ro"
       - "/home/jully-volumes/main/logs:/home/jully/main/logs"
       - "/home/jully-volumes/main/tomcat/logs:/usr/local/tomcat/logs"
       - "/home/jully-volumes/attachments:/home/jully/attachments"
       - "/home/jully-volumes/sync:/home/jully/sync"
     ports:
       - 8080:8080
​
  cc:
     image: registry.cn-zhangjiakou.aliyuncs.com/jully/cc
#    restart: always
     depends_on:
       - database                               # 如果使用外部数据库,则需要删除该依赖
       - redis                                  # 必须项:docker一律使用redis
     environment:
       JULLY_CUSTOMER_ID: @{JULLY_CUSTOMER_ID}  # 必须项:客户端ID,可以通过export指定,也可以直接修改
       JULLY_DOCKER_MODE: 1                     # 必须项:固定1
# 指定外部数据库信息
#      JULLY_DATASOURCE_DRIVER_CLASS_NAME_jully: com.mysql.cj.jdbc.Driver
#      JULLY_DATASOURCE_URL_jully: jdbc:mysql://localhost:3306/jully?serverTimezone=GMT%2B8&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL
#      JULLY_DATASOURCE_USERNAME_jully: jully
#      JULLY_DATASOURCE_PASSWORD_jully: jully
     volumes:
       - "/etc/localtime:/etc/localtime:ro"
       - "/home/jully-volumes/cc/logs:/home/jully/cc/logs"
       - "/home/jully-volumes/sync:/home/jully/sync"
       - "/home/jully-volumes/main/logs:/home/jully/main/logs:ro"
       - "/home/jully-volumes/attachments:/home/jully/attachments"
     ports:
       - 8081:8081
​
  # 如果指定外部数据库,则删除如下service
  database:
     image: registry.cn-zhangjiakou.aliyuncs.com/jully/mysql
     restart: always     
     environment:
       MYSQL_DATABASE: 'jully'      
       MYSQL_ROOT_PASSWORD: jully
       MYSQL_USER: 'jully'
       MYSQL_PASSWORD: 'jully'
       MYSQL_ROOT_HOST: '%'
     command: 
       --default-authentication-plugin=mysql_native_password
       --character-set-server=utf8
       --collation-server=utf8_general_ci       
       --lower_case_table_names=1
     ports:
       - 3306:3306
     volumes:
       - "/etc/localtime:/etc/localtime:ro"
       - "/home/jully-volumes/var/lib/mysql:/var/lib/mysql"
​
  nginx:
     image: registry.cn-zhangjiakou.aliyuncs.com/jully/nginx
     restart: always
     ports:
       - 80:80
       - 443:443
     depends_on: 
       - main
       - cc
     volumes:
       - "/etc/localtime:/etc/localtime:ro"
​
  redis:
     image: registry.cn-zhangjiakou.aliyuncs.com/jully/redis
     restart: always
     ports:
       - 6379:6379
     volumes:
       - "/etc/localtime:/etc/localtime:ro"

登陆阿里云仓库

[root@localhost docker-compose-jully]# docker login --username=liyongnanjully registry.cn-zhangjiakou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

启动项目

第一次启动

[root@localhost docker-compose-jully]# docker-compose up -d
Creating network "docker-compose-jully_default" with the default driver
Pulling database (registry.cn-zhangjiakou.aliyuncs.com/jully/mysql:)...
latest: Pulling from jully/mysql
d121f8d1c412: Pull complete
f3cebc0b4691: Pull complete
1862755a0b37: Pull complete
489b44f3dbb4: Pull complete
690874f836db: Pull complete
baa8be383ffb: Pull complete
55356608b4ac: Pull complete
dd35ceccb6eb: Pull complete
429b35712b19: Pull complete
162d8291095c: Pull complete
5e500ef7181b: Pull complete
af7528e958b6: Pull complete
3688b6c14f1f: Pull complete
1328341a6017: Pull complete
Digest: sha256:9079ea754796ecebf509d75a4b6685434cc42c4281d900e7ad7ac17c643d7253
Status: Downloaded newer image for registry.cn-zhangjiakou.aliyuncs.com/jully/mysql:latest
Pulling redis (registry.cn-zhangjiakou.aliyuncs.com/jully/redis:)...
latest: Pulling from jully/redis
d121f8d1c412: Already exists
2f9874741855: Pull complete
d92da09ebfd4: Pull complete
bdfa64b72752: Pull complete
e748e6f663b9: Pull complete
eb1c8b66e2a1: Pull complete
Digest: sha256:e8186060cb7d34850022ea4419e8daf98c3eb74c9c4e1324522fe160b22ea661
Status: Downloaded newer image for registry.cn-zhangjiakou.aliyuncs.com/jully/redis:latest
Pulling cc (registry.cn-zhangjiakou.aliyuncs.com/jully/cc:)...
latest: Pulling from jully/cc
57df1a1f1ad8: Pull complete
71e126169501: Pull complete
1af28a55c3f3: Pull complete
03f1c9932170: Pull complete
881ad7aafb13: Pull complete
9c0ffd4062f3: Pull complete
bd62e479351a: Pull complete
540daed7dbae: Pull complete
Digest: sha256:f3606b422343c13de08282eb888a6681734db554e785791e4f4d8b9a7e43461c
Status: Downloaded newer image for registry.cn-zhangjiakou.aliyuncs.com/jully/cc:latest
Pulling main (registry.cn-zhangjiakou.aliyuncs.com/jully/main:)...
latest: Pulling from jully/main
57df1a1f1ad8: Already exists
71e126169501: Already exists
1af28a55c3f3: Already exists
03f1c9932170: Already exists
881ad7aafb13: Already exists
9c0ffd4062f3: Already exists
bd62e479351a: Already exists
48ee8bc64dbc: Pull complete
6daad3485ea7: Pull complete
bc07a0199230: Pull complete
8254386f43b7: Pull complete
Digest: sha256:19dfc696c848025496b5df7498108b399a33a18dc774ee850476dd0fd0d933a5
Status: Downloaded newer image for registry.cn-zhangjiakou.aliyuncs.com/jully/main:latest
Pulling nginx (registry.cn-zhangjiakou.aliyuncs.com/jully/nginx:)...
latest: Pulling from jully/nginx
d121f8d1c412: Already exists
ebd81fc8c071: Pull complete
655316c160af: Pull complete
d15953c0e0f8: Pull complete
2ee525c5c3cc: Pull complete
e803714b29ab: Pull complete
Digest: sha256:774748ad2e1fc7ff26048ad3fb660756f03f5a5b911db237e1e5fc7bcd8bf244
Status: Downloaded newer image for registry.cn-zhangjiakou.aliyuncs.com/jully/nginx:latest
Creating docker-compose-jully_redis_1    ... done
Creating docker-compose-jully_database_1 ... done
Creating docker-compose-jully_main_1     ... done
Creating docker-compose-jully_cc_1       ... done
Creating docker-compose-jully_nginx_1    ... done

第二次以后启动

[root@localhost docker-compose-jully]# docker-compose up -d
Creating network "docker-compose-jully_default" with the default driver
Creating docker-compose-jully_database_1 ... done
Creating docker-compose-jully_redis_1    ... done
Creating docker-compose-jully_cc_1       ... done
Creating docker-compose-jully_main_1     ... done
Creating docker-compose-jully_nginx_1    ... done

停止项目

[root@localhost docker-compose-jully]# docker-compose down
Stopping docker-compose-jully_nginx_1    ... done
Stopping docker-compose-jully_cc_1       ... done
Stopping docker-compose-jully_main_1     ... done
Stopping docker-compose-jully_redis_1    ... done
Stopping docker-compose-jully_database_1 ... done
Removing docker-compose-jully_nginx_1    ... done
Removing docker-compose-jully_cc_1       ... done
Removing docker-compose-jully_main_1     ... done
Removing docker-compose-jully_redis_1    ... done
Removing docker-compose-jully_database_1 ... done
Removing network docker-compose-jully_default

升级Project

此方法今后废弃

#1.停止项目
docker-compose down

#2.更新所有镜像,拉取最新的镜像
docker images --format "{{.Repository}}:{{.Tag}}" | grep ':latest' | xargs -L1 docker pull

#3.删除none镜像(由升级导致tag变成none的旧镜像)
docker rmi -f $(docker images -aq -f dangling=true)

#4.启动项目
docker-compose up -d

其他常用命令

#1.获取长容器ID
docker inspect --format="{{.Id}}" tomcat01
​
#2.容器外部复制到容器内部
docker cp /home/docker-compose-jully/jully d200f4e546ef14837b27d46ca6ab953ffee45ec8b6d18711b2333737eab25c57:/usr/local/tomcat/webapps
​
#3.删除所有镜像
docker rmi -f $(docker images -aq)
​
#4.删除所有容器
docker rm -f $(docker ps -aq)