查看原文
其他

如何使用docker?

半夏透心凉 云原生技术爱好者社区 2022-04-07


1. 基础环境

Centos:7.0/7.4Docker:[docker-18.09.6](https://download.docker.com/linux/static/stable/x86_64/)

2. docker在线和离线安装


2.1 docker在线安装


1、移除旧的版本:

$ 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


2、安装一些必要的系统工具:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2



3、添加软件源信息:

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



4、更新 yum 缓存:

sudo yum makecache fast



5、安装 Docker-ce:

sudo yum -y install docker-ce



6. 启动 Docker 后台服务

systemctl start docker


2.2 Docker离线安装
1、解压

tar -xvf docker-18.09.6.tgz


2、将解压出来的docker文件内容移动到 /usr/bin/ 目录下

cp docker/* /usr/bin/


3、将docker注册为service

vim /etc/systemd/system/docker.service


将下列配置加到docker.service中并保存

[Unit]Description=Docker Application Container EngineDocumentation=https://docs.docker.comAfter=network-online.target firewalld.serviceWants=network-online.target[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 dockerExecStart=/usr/bin/dockerdExecReload=/bin/kill -s HUP $MAINPID# 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=infinityLimitNPROC=infinityLimitCORE=infinity# Uncomment TasksMax if your systemd version supports it.# Only systemd 226 and above support this version.#TasksMax=infinityTimeoutStartSec=0# set delegate yes so that systemd does not reset the cgroups of docker containersDelegate=yes# kill only the docker process, not all processes in the cgroupKillMode=process# restart the docker process if it exits prematurelyRestart=on-failureStartLimitBurst=3StartLimitInterval=60s[Install]WantedBy=multi-user.target




4、启动

#添加文件权限并启动dockerchmod +x /etc/systemd/system/docker.service#重载unit配置文件systemctl daemon-reload#启动Dockersystemctl start docker#设置开机自启systemctl enable docker.service



5、验证

#查看Docker状态systemctl status docker#查看Docker版本docker -v


 2.3 公司内网harbor配置

1、 登录公司harbor

docker login docker.shj.com

2、拉取必要镜像

[root@localhost docker]# docker pull docker.shj.com/shj/mysql:5.7.235.7.23: Pulling from shj/mysqlbe8881be8156: Pull completec3995dabd1d7: Pull complete9931fdda3586: Pull completebb1b6b6eff6a: Pull completea65f125fa718: Pull complete2d9f8dd09be2: Pull complete37b912cb2afe: Pull completefaf9da46e0cf: Pull completeffcedc9e8600: Pull complete6e11f2447e86: Pull complete02243b284270: Pull completeDigest: sha256:97fca0a7d9c7904130c43a75d65a4a5b20eb9cc2a48fe9cdf5a94cb54935832fStatus: Downloaded newer image for docker.shj.com/shj/mysql:5.7.23



 2  使用操作实例(仅供学习参考)

1、基于docker.shj.com/shj/alpine:3.8制作tomcat镜像(示例);
前提基础条件:当前文件夹存在java.tar.gz和tomcat.tar.gz;
vim Dockerfile文件,如下:

FROM docker.shj.com/shj/alpine:3.8MAINTAINER shjWORKDIR /usr/local/ADD java.tar.gz /usr/local/ADD tomcat7.tar.gz /usr/local/
ENV JAVA_HOME /usr/local/javaENV JRE_HOME /usr/local/java/jreENV CLASSPATH .:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATHENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 8080CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]


2、执行构建操作

[root@localhost jdk-tomcat]# docker build -t tomcat7:v2 .Sending build context to Docker daemon 1.355GBStep 1/11 : FROM docker.shj.com/shj/alpine:3.8 ---> f1d0cd4bc04cStep 2/11 : MAINTAINER shj ---> Using cache ---> 3411ad3de9adStep 3/11 : WORKDIR /usr/local/ ---> Using cache ---> 7f85c345af60Step 4/11 : ADD java.tar.gz /usr/local/ ---> Using cache ---> 57f69804f319Step 5/11 : ADD tomcat7.tar.gz /usr/local/ ---> Using cache ---> 46d8a3ce7c36Step 6/11 : ENV JAVA_HOME /usr/local/java ---> Using cache ---> 42658b05ec9eStep 7/11 : ENV JRE_HOME /usr/local/java/jre ---> Using cache ---> 74d199e615abStep 8/11 : ENV CLASSPATH .:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH ---> Using cache ---> 4044d8ef4e86Step 9/11 : ENV PATH $JAVA_HOME/bin:$PATH ---> Using cache ---> 4fa4fef3940eStep 10/11 : EXPOSE 8080 ---> Using cache ---> e08ec786a08bStep 11/11 : CMD ["/usr/local/tomcat/bin/catalina.sh", "run"] ---> Using cache ---> 6755d3e6c0e9Successfully built 6755d3e6c0e9Successfully tagged tomcat7:v2

3、启动镜像,根据映射端口8999,可以正常访问.

[root@localhost jdk-tomcat]# docker run --rm -it -p 8999:8080 tomcat7:v2/usr/local/tomcat/bin/catalina.sh: /usr/local/tomcat/conf/shj.conf: line 5: /usr/bin/grep: not found/usr/local/tomcat/bin/catalina.sh: /usr/local/tomcat/conf/shj.conf: line 5: /usr/bin/sed: not found/usr/local/tomcat/bin/catalina.sh: /usr/local/tomcat/conf/shj.conf: line 5: /usr/bin/grep: not foundUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/local/java/jreUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jarUsing CATALINA_PID: /usr/local/tomcat/logs/tomcat.pidJava HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=500M; support was removed in 8.0Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=500M; support was removed in 8.0Java HotSpot(TM) 64-Bit Server VM warning: UseCMSCompactAtFullCollection is deprecated and will likely be removed in a future release.Java HotSpot(TM) 64-Bit Server VM warning: CMSFullGCsBeforeCompaction is deprecated and will likely be removed in a future release.Aug 08, 2019 5:34:56 PM org.apache.catalina.startup.SetAllPropertiesRule beginWARNING: [SetAllPropertiesRule]{Server/Service/Connector} Setting property 'maxKeepAliveRequest' to '200' did not find a matching property.Aug 08, 2019 5:34:56 PM org.apache.catalina.startup.SetAllPropertiesRule beginWARNING: [SetAllPropertiesRule]{Server/Service/Connector} Setting property 'maxSpareThreads' to '100' did not find a matching property.Aug 08, 2019 5:34:56 PM org.apache.tomcat.util.digester.SetPropertiesRule beginWARNING: [SetPropertiesRule]{Server/Service/Engine/Host} Setting property 'xmlValidation' to 'false' did not find a matching property.Aug 08, 2019 5:34:56 PM org.apache.tomcat.util.digester.SetPropertiesRule beginWARNING: [SetPropertiesRule]{Server/Service/Engine/Host} Setting property 'xmlNamespaceAware' to 'false' did not find a matching property.Aug 08, 2019 5:34:56 PM org.apache.catalina.core.AprLifecycleListener lifecycleEventINFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/libAug 08, 2019 5:34:56 PM org.apache.coyote.AbstractProtocol initINFO: Initializing ProtocolHandler ["http-bio-8080"]Aug 08, 2019 5:34:56 PM org.apache.catalina.startup.Catalina loadINFO: Initialization processed in 399 msAug 08, 2019 5:34:56 PM org.apache.catalina.core.StandardService startInternalINFO: Starting service CatalinaAug 08, 2019 5:34:56 PM org.apache.catalina.core.StandardEngine startInternalINFO: Starting Servlet Engine: Apache Tomcat/1.2Aug 08, 2019 5:34:56 PM org.apache.coyote.AbstractProtocol startINFO: Starting ProtocolHandler ["http-bio-8080"]Aug 08, 2019 5:34:56 PM org.apache.catalina.startup.Catalina startINFO: Server startup in 29 ms

4、基于上面镜像构建java管理系统镜像

FROM tomcat:v2MAINTAINER shjWORKDIR /home/shj/appsADD clq.war /home/shj/appsADD clq.tar.gz /home/shj/conf/EXPOSE 8080CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]




5、类似与上面步骤2,先构建、运行;在浏览器可以通过8990端口访问服务;并且日志已经挂载到宿主机。

docker run -d -p 8990:8080 -v /home/logs:/usr/local/tomcat/logs -v /home/logs:/server/tomcat6-8080/logs/clq clq:v1




6、进入容器进行必要配置修改

docker exec -it 46af96fc40b9  /bin/bash

7、 标记本地镜像并归入仓库

[root@localhost jdk-tomcat]# docker tag clq:v1 docker.shj.com/clq/clq:v1


[root@localhost jdk-tomcat]# docker push docker.shj.com/clq/clq:v1The push refers to repository [docker.shj.com/clq/clq]030a3cd045b4: Pushed5ea11432f29c: Pushed8efc68fa427d: Pushed6def8f5c34a2: Pushed7fe78f594dc6: Mounted from clq/tomcat7adb1e36c47d3: Mounted from clq/tomcat702c88841675d: Mounted from clq/tomcat7df64d3292fd6: Mounted from clq/tomcat7v1: digest: sha256:ae79c893a4f94d67b605c82bc0d6c5cb493f1f60e34be507127474fce7afab15 size: 1998

 3. 总结

   Docker是一个开源的容器引擎,利用docker的沙箱机制,能够提高部署效率,在市面使用上已经非常成熟,掌握基本常用命令即可使用。

 关注【被讨厌的勇气】

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存