其他
如何使用docker?
1. 基础环境
Centos:7.0/7.4
Docker:[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 Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=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 docker
ExecStart=/usr/bin/dockerd
ExecReload=/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=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# 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
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
4、启动
#添加文件权限并启动docker
chmod +x /etc/systemd/system/docker.service
#重载unit配置文件
systemctl daemon-reload
#启动Docker
systemctl 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.23
5.7.23: Pulling from shj/mysql
be8881be8156: Pull complete
c3995dabd1d7: Pull complete
9931fdda3586: Pull complete
bb1b6b6eff6a: Pull complete
a65f125fa718: Pull complete
2d9f8dd09be2: Pull complete
37b912cb2afe: Pull complete
faf9da46e0cf: Pull complete
ffcedc9e8600: Pull complete
6e11f2447e86: Pull complete
02243b284270: Pull complete
Digest: sha256:97fca0a7d9c7904130c43a75d65a4a5b20eb9cc2a48fe9cdf5a94cb54935832f
Status: 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.8
MAINTAINER shj
WORKDIR /usr/local/
ADD java.tar.gz /usr/local/
ADD tomcat7.tar.gz /usr/local/
ENV JAVA_HOME /usr/local/java
ENV JRE_HOME /usr/local/java/jre
ENV CLASSPATH .:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
2、执行构建操作
[root@localhost jdk-tomcat]# docker build -t tomcat7:v2 .
Sending build context to Docker daemon 1.355GB
Step 1/11 : FROM docker.shj.com/shj/alpine:3.8
---> f1d0cd4bc04c
Step 2/11 : MAINTAINER shj
---> Using cache
---> 3411ad3de9ad
Step 3/11 : WORKDIR /usr/local/
---> Using cache
---> 7f85c345af60
Step 4/11 : ADD java.tar.gz /usr/local/
---> Using cache
---> 57f69804f319
Step 5/11 : ADD tomcat7.tar.gz /usr/local/
---> Using cache
---> 46d8a3ce7c36
Step 6/11 : ENV JAVA_HOME /usr/local/java
---> Using cache
---> 42658b05ec9e
Step 7/11 : ENV JRE_HOME /usr/local/java/jre
---> Using cache
---> 74d199e615ab
Step 8/11 : ENV CLASSPATH .:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
---> Using cache
---> 4044d8ef4e86
Step 9/11 : ENV PATH $JAVA_HOME/bin:$PATH
---> Using cache
---> 4fa4fef3940e
Step 10/11 : EXPOSE 8080
---> Using cache
---> e08ec786a08b
Step 11/11 : CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
---> Using cache
---> 6755d3e6c0e9
Successfully built 6755d3e6c0e9
Successfully 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 found
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_PID: /usr/local/tomcat/logs/tomcat.pid
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=500M; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=500M; support was removed in 8.0
Java 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 begin
WARNING: [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 begin
WARNING: [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 begin
WARNING: [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 begin
WARNING: [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 lifecycleEvent
INFO: 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/lib
Aug 08, 2019 5:34:56 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 08, 2019 5:34:56 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 399 ms
Aug 08, 2019 5:34:56 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 08, 2019 5:34:56 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/1.2
Aug 08, 2019 5:34:56 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 08, 2019 5:34:56 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 29 ms
4、基于上面镜像构建java管理系统镜像
FROM tomcat:v2
MAINTAINER shj
WORKDIR /home/shj/apps
ADD clq.war /home/shj/apps
ADD clq.tar.gz /home/shj/conf/
EXPOSE 8080
CMD ["/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:v1
The push refers to repository [docker.shj.com/clq/clq]
030a3cd045b4: Pushed
5ea11432f29c: Pushed
8efc68fa427d: Pushed
6def8f5c34a2: Pushed
7fe78f594dc6: Mounted from clq/tomcat7
adb1e36c47d3: Mounted from clq/tomcat7
02c88841675d: Mounted from clq/tomcat7
df64d3292fd6: Mounted from clq/tomcat7
v1: digest: sha256:ae79c893a4f94d67b605c82bc0d6c5cb493f1f60e34be507127474fce7afab15 size: 1998
3. 总结
Docker是一个开源的容器引擎,利用docker的沙箱机制,能够提高部署效率,在市面使用上已经非常成熟,掌握基本常用命令即可使用。
关注【被讨厌的勇气】