docker基础
在线安装
1、docker安装
- centos7
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
- 安装yum工具包
yum install -y yum-utils
- 设置镜像仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo 这是国外的,安装国内的阿里云镜像
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
推荐使用用国内的
- 更新软件包索引
yum makecache fast
- 安装docker的相关内容
//docker-ce 社区 ee 企业版
yum install docker-ce docker-ce-cli containerd.io -y
//如果不想安装最新版本,可以安装指定版本
yum list docker-ce --showduplicates | sort -r
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
- 启动docker
systemctl start docker
//查看docker是否启动成功
docker version
2、阿里云的镜像加速
登录阿里云,找到镜像加速地址
阿里云镜像加速(阿里云 容器镜像服务)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://z8wfi803.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
3、卸载docker
- 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
- 删除资源
rm -rf /var/lib/docker # /var/lib/docker docker默认的工作路径
rm -rf /var/lib/containerd
离线安装
1、环境
-
CentenOS 7 内核3.1以上
内核查看命令: uname -a -
安装包:docker-19.03.8.tgz
下载地址:https://download.docker.com/linux/static/stable/x86_64/
2、安装
- 解压
tar -xvf docker-19.03.8.tgz
- 移动(/user/bin/目录下)
cp docker/* /usr/bin/
- 将docker注册为service
vim /etc/systemd/system/docker.service
#将下列配置加到docker.service中并保存
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
# BindsTo=containerd.service
# After=network-online.target firewalld.service containerd.service
After=network-online.target firewalld.service
Wants=network-online.target
# Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
# ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
# TasksMax=infinity
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
[Install]
WantedBy=multi-user.target
- 启动
chmod +x /etc/systemd/system/docker.service
systemctl daemon-reload
systemctl start docker
systemctl enable docker.service
- 验证
systemctl status docker
docker -v
修改docker默认存储目录
1、环境:
centos7.x系统,已经装好docker-ce服务包
2、查看当前docker的存储路径
[root@VM-docker ~]$ docker info |grep Dir
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
WARNING: the devicemapper storage-driver is deprecated, and will be removed in a future release.
WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use.
Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
Docker Root Dir: /var/lib/docker ## docker默认的存储路径
3、关闭docker服务
[root@VM-docker ~]$ systemctl stop docker ## 关闭docker服务
[root@VM-docker ~]$ systemctl status docker ## 查看docker服务状态
4、将原有数据迁移至新目录
[root@@VM-docker local]# mkdir /data/service/docker -p
[root@@VM-docker local]# mv /var/lib/docker/* /data/docker/
5、修改docker.service配置文件,使用 --graph 参数指定存储位置
[root@VM-docker local]# vim /etc/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --graph /data/docker
6、重新加载配置文件
[root@VM-docker local]# systemctl daemon-reload
7、启动docker服务
[root@VM-docker local]# systemctl start docker
[root@VM-docker local]# systemctl enable docker
[root@VM-docker local]# systemctl status docker
8、查看修改是否成功
[root@VM-docker ~]# docker info | grep Dir
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
WARNING: the devicemapper storage-driver is deprecated, and will be removed in a future release.
WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use.
Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
Docker Root Dir: /data/docker #查看修改成功
命令自动补全
Linux
sudo curl \
-L https://raw.githubusercontent.com/docker/compose/1.29.2/contrib/completion/bash/docker-compose \
-o /etc/bash_completion.d/docker-compose
source ~/.bashrc
参考文档:
https://docs.docker.com/compose/completion/
搭建本地仓库
1、搭建本地仓库
mkdir /data/docker/myregistry -p
docker run -d -p 5000:5000 -v /data/docker/myregistry:/var/lib/registry registry
2、上传镜像
docker tag httpd:v11 127.0.0.1:5000/michael/httpd:v11
docker push 127.0.0.1:5000/michael/httpd:v11
3、查看 Registry 中的镜像。
#查询registry中所有的镜像名称
curl -XGET http://192.168.2.80:5000/v2/_catalog
#依据镜像名称查询镜像版本
curl -XGET http://192.168.2.80:5000/v2/httpd/tags/list
4、从 Registry 下载镜像 michael/httpd:v11
vim /etc/docker/daemon.json
{
"insecure-registries":["192.168.2.80:5000"]
}
systemctl restart docker
docker pull 192.168.2.80:5000/michael/httpd:v11
docker中centos7中文支持
1、进入docker里配置
添加中文环境编码,安装两个包
# yum install kde-l10n-Chinese -y
# yum install glibc-common -y
转化语言环境和字符集
# localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
添加定义到系统环境变量
# vi /etc/profile
export LC_ALL=zh_CN.utf8
执行生效
# source /etc/profile
2、编写dockerfile文件
FROM centos
MAINTAINER djl
#设置系统编码
RUN yum install kde-l10n-Chinese -y
RUN yum install glibc-common -y
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
#RUN export LANG=zh_CN.UTF-8
#RUN echo "export LANG=zh_CN.UTF-8" >> /etc/locale.conf
#ENV LANG zh_CN.UTF-8
ENV LC_ALL zh_CN.UTF-8
参考文档:
https://www.lmlphp.com/user/16958/article/item/481698/
dockerfile
tomcat
#基于我们从阿里云下载下来的centos基础镜像
FROM centos
#定义维护者的信息
MAINTAINER kgf<[email protected]>
#把宿主机当前上下文的test1.txt文件拷贝到容器/usr/local/路径下
COPY readme.txt /usr/local/readme.txt
#把java与tomcat添加到容器中,使用ADD命令会自动帮我们解压
ADD jdk-8u191-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.12.tar.gz /usr/local/
#安装vim编辑器
RUN yum -y install vim
#设置工作访问时候的workdir路径,登录落脚点
ENV MY_PATH /usr/local
WORKDIR $MY_PATH
#配置java与tomcat环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_191
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.12
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.12
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#容器运行时监听的端口
EXPOSE 8080
#启动时运行tomcat,下面的三种方式随便一种都可以使用
#ENTRYPOINT ["/usr/local/apache-tomcat-9.0.12/bin/startup.sh"]
#CMD ["/usr/local/apache-tomcat-9.0.12/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-9.0.12/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.12/bin/logs/catalina.out
docker-compose
安装
- 下载
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
//国内的
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
- 修改权限
sudo chmod +x /usr/local/bin/docker-compose
- 补全命令
curl -L https://raw.githubusercontent.com/docker/compose/$(docker-compose version --short)/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
- 查看是否安装成功
docker-compose version
标签:bin,compose,入门,Kubernetes,宝典,yum,usr,docker,local
From: https://www.cnblogs.com/jluo/p/16790789.html