基础
(一)安装与卸载
1、Ubuntu下安装与卸载
先更新apt包索引
sudo apt-get update
安装最新版docker
sudo apt-get install docker-ce docker-ce-cli containerd.io
卸载
sudo apt-get remove docker docker-engine docker.io containerd runc
2、Centos下安装与卸载
安装
sudo yum install docker-ce docker-ce-cli containerd.io
卸载
sudo yum remove docker docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
(二)Docker镜像加速
需要配置的地址: /etc/docker/daemon.json
配置的参数是json格式的
zhenghui@zhenghui-F117-F:~$ cat /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
zhenghui@zhenghui-F117-F:~$
FROM scratch
MAINTAINER The CentOS Project <cloud-ops@centos.org>
ADD c68-docker.tar.xz /
LABEL name="CentOS Base Image" \
vendor="CentOS" \
license="GPLv2" \
build-date="2016-06-02"
# Default command
CMD ["/bin/bash"]
(三)启动与停止
systemctl restart docker
systemctl stop docker
systemctl start docker
systemctl daemon-reload
https://www.cnblogs.com/panwenbin-logs/p/8007348.html
Docker HelloWorld
1、获取Hello-World
zhenghui@zhenghui-F117-F:~$sudo docker pull hello-world
zhenghui@zhenghui-F117-F:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 13 months ago 1.84kB
zhenghui@zhenghui-F117-F:~$
2、运行
zhenghui@zhenghui-F117-F:~$ sudo docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Docker基本操作
首先拉去一个centos的镜像
先查询下
zhenghui@zhenghui-F117-F:~$ sudo docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 5813 [OK]
选取一个使用多的拉取
zhenghui@zhenghui-F117-F:~$ sudo docker pull centos
查看已经拉取的镜像
zhenghui@zhenghui-F117-F:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 470671670cac 3 weeks ago 237MB
zhenghui@zhenghui-F117-F:~$
启动容器并进入
zhenghui@zhenghui-F117-F:~$ sudo docker run -it centos /bin/bash
[root@637df1614bf7 /]#
参数说明:
- -i: 交互式操作。
- -t: 终端。
- ubuntu: ubuntu 镜像。
- /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。
要退出终端,直接输入 exit:
root@637df1614bf7:/# exit
查看所有的容器命令如下:
$ docker ps -a
后台运行
在大部分的场景下,我们希望 docker 的服务是在后台运行的,我们可以过 -d 指定容器的运行模式。
docker run -itd --name mycentos centos /bin/bash
**注:**加了 -d 参数默认不会进入容器,想要进入容器需要使用指令 docker exec
exec 命令
下面演示了使用 docker exec 命令。
docker exec -it mycentos /bin/bash
停止容器
docker stop <容器 ID>
启动容器
docker start <容器 ID>
重启容器
docker restart <容器 ID>
Tomcat
docker run -d --name mytomcat -p 8888:8080 tomcat:latest
注意
1:每条保留字指令都必须大写字母,后面要跟至少一个参数
2:指令按照从红上到下,顺序执行
3:#号表示注释
4:每条指令都会创建一个新的镜像层,并对镜像进行提交
保留字指令
FROM
:
指明基础镜像,当前新镜像是基于哪个镜像的
例如:FROM scratch
MAINTAINER
:
镜像维护者的姓名和邮箱地址
例如:MAINTAINER The CentOS Project <cloud-ops@centos.org>
RUN
:
容器构建时需要运行的命令
例如:
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r -g 999 redis && useradd -r -g redis -u 999 redis
EXPOSE
:
暴露出对外的端口
例如:
EXPOSE 6379
WORKDIR
:
指定在创建容器后,终端默认登陆的进来工作目录,一个落脚点.
WORKDIR /data #刚登陆的时候就会进入到/data
ENV
:
用来构建镜像过程中设置环境变量
ADD
:
相当于COPY但是比COPY强大,ADD可以拷贝+解压缩
#ADD 文件放在当前目录下,拷过去会自动解压
ADD nginx-1.8.0.tar.gz /usr/local/
COPY
:
类似ADD,拷贝文件和目录到镜像中.
但是是不会自动解压文件,也不能访问网络资源
COPY docker-entrypoint.sh /usr/local/bin/
VOLUME
:
容器数据卷,用于数据保持和持久化工作
CMD
:
指定一个容器启动时要运行的命令
Dockerfile中可以有多个CMD指令,但只有最后一个生效,CMD会被docker run之后的参数替换
ENTRYPOINT
:
指定一个容器启动时要运行的命令
ENTRYPOINT的目的和CMD一样,都是在指定容器启动程序及参数
ONBUILD
:
用于设置镜像触发器
格式:
ONBUILD [INSTRUCTION]
示例:
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
注:
当所构建的镜像被用做其它镜像的基础镜像,该镜像中的触发器将会被钥触发
案例
一,自定义centos镜像
首先呢,有一个centos精简版的镜像
zhenghui@zhenghui-F117-F:~$ sudo docker images centos
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 470671670cac 3 weeks ago 237MB
zhenghui@zhenghui-F117-F:~$
在这个精简版的基础上做修改,最后根据自己的需求定义一个自己的容器
FROM centos #cento是原有的REPOSITORY的名字
MAINTAINER zhenghui<8042965@qq.com> #定义作者等信息
ENV MYPATH /usr/local # 定义变量
WORKDIR $MYPATH #定义登录后进入的目录
RUN yum -y install vim #在原来的基础上安装vim工具
RUN yum -y install net-tools #在原来centos的基础上安装net-tools工具
EXPOSE 80 #抛出80功能
#执行一些命令
CMD echo $MYPATH
CMD echo "success----------------ok"
CMD /bin/bash
FROM centos
mAINTAINER zhenghui<8042965@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "success----------------ok"
CMD /bin/bash
构建一个容器
执行:
sudo docker build -f /soft/code/Dockerfile -t mycentos:0.1 .
构建一个新的容器
zhenghui@zhenghui-F117-F:/soft/code$ pwd
/soft/code
zhenghui@zhenghui-F117-F:/soft/code$
zhenghui@zhenghui-F117-F:/soft/code$
zhenghui@zhenghui-F117-F:/soft/code$ ll
总用量 20
drwxr-xr-x 3 zhenghui zhenghui 4096 2月 9 23:14 ./
drwxrwxrwx 14 root root 4096 2月 9 22:23 ../
-rw-r--r-- 1 zhenghui zhenghui 222 2月 8 19:49 Dockerfile
-rw-r--r-- 1 zhenghui zhenghui 777 2月 7 19:35 Dockerfile.bak
drwxr-xr-x 3 zhenghui zhenghui 4096 2月 10 00:23 meisaifile/
zhenghui@zhenghui-F117-F:/soft/code$
zhenghui@zhenghui-F117-F:/soft/code$ cat Dockerfile
FROM centos
mAINTAINER zhenghui<8042965@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "success----------------ok"
CMD /bin/bash
zhenghui@zhenghui-F117-F:/soft/code$
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker build -f /soft/code/Dockerfile -t mycentos:0.1 .
zhenghui@zhenghui-F117-F:/soft/code$
运行测试
可以看出落脚点(刚登陆的就进入的目录)就是咱们定义好的
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 562dc14ac536 3 minutes ago 327MB
centos latest 470671670cac 3 weeks ago 237MB
zhenghui@zhenghui-F117-F:/soft/code$
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker run -it mycentos:0.1
[root@880d703643a7 local]#
[root@880d703643a7 local]# pwd
/usr/local
[root@880d703643a7 local]#
[root@880d703643a7 local]#
可以看出现在也支持了vim和ifconfig命令
[root@880d703643a7 local]#
[root@880d703643a7 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 51 bytes 7624 (7.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@880d703643a7 local]#
[root@880d703643a7 local]# vi
vi view vigr vim vimdiff vimtutor vipw
[root@880d703643a7 local]# vim a.txt
查看历史变成记录
可以看出很像栈一样,一层一层往上叠加.
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker history mycentos:0.1
IMAGE CREATED CREATED BY SIZE COMMENT
562dc14ac536 10 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
7c06ee68cc75 10 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
5a6e959532f3 10 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
5038be3aed0b 10 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
f09f3c49b5c1 10 minutes ago /bin/sh -c yum -y install net-tools 26.5MB
bb8075343f64 10 minutes ago /bin/sh -c yum -y install vim 63MB
ce9cd2ea3e6d 14 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
66f820f1c9b5 14 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
a19e432a03d6 14 minutes ago /bin/sh -c #(nop) MAINTAINER zhenghui<80429… 0B
470671670cac 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:aa54047c80ba30064… 237MB
zhenghui@zhenghui-F117-F:/soft/code$
二,CMD与ENTRYPOINT指令的区别
都是制定一个容器启动时要运行的命令
CMD指令
例如下面这个查询ip地址的一个小功能
vim /soft/code/Dockerfile2
FROM centos
RUN yum -y install curl
CMD ["curl","-s","http://wttr.in/shanxian"]
构建:
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker build -f /soft/code/Dockerfile2 -t mytianqi:0.2 .
zhenghui@zhenghui-F117-F:/soft/code$
如果运行:`sudo docker run -it mytianqi:0.12
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker run -it mytianqi:0.2
Weather report: shanxian
\ / Partly cloudy
_ /"".-. 13 °C
\_( ). ↖ 14 km/h
/(___(__) 10 km
0.0 mm
******省略
Location: 单县, 北城街道, 单县 (Shan), 菏泽市, 山东省, 274300, 中国 [34.7945948,116.0838296]
Follow @igor_chubin for wttr.in updates
zhenghui@zhenghui-F117-F:/soft/code$
如果执行:sudo docker run -it mytianqi:0.2 -i
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker run -it mytianqi:0.2 -i
docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"-i\": executable file not found in $PATH": unknown.
zhenghui@zhenghui-F117-F:/soft/code$
ENTRYPOINT指令
会继续追加命令
vim /soft/code/Dockerfile3
FROM centos
RUN yum -y install curl
ENTRYPOINT ["curl","-s","http://wttr.in/shanxian"]
构建:
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker build -f /soft/code/Dockerfile3 -t mytianqi:0.3 .
zhenghui@zhenghui-F117-F:/soft/code$
如果运行:sudo docker run -it mytianqi:0.3
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker run -it mytianqi:0.3
Weather report: shanxian
\ / Partly cloudy
_ /"".-. 13 °C
\_( ). ↖ 14 km/h
/(___(__) 10 km
0.0 mm
******省略
Location: 单县, 北城街道, 单县 (Shan), 菏泽市, 山东省, 274300, 中国 [34.7945948,116.0838296]
Follow @igor_chubin for wttr.in updates
zhenghui@zhenghui-F117-F:/soft/code$
如果执行:sudo docker run -it mytianqi:0.3 -i
可以看出查出来报文头等信息
zhenghui@zhenghui-F117-F:/soft/code$ sudo docker run -it mytianqi:0.3 -i
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Mon, 10 Feb 2020 15:48:44 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 8761
Connection: keep-alive
Weather report: shanxian
\ / Partly cloudy
_ /"".-. 13 °C
\_( ). ↖ 14 km/h
/(___(__) 10 km
0.0 mm
****省略
Location: 单县, 北城街道, 单县 (Shan), 菏泽市, 山东省, 274300, 中国 [34.7945948,116.0838296]
Follow @igor_chubin for wttr.in updates
zhenghui@zhenghui-F117-F:/soft/code$
三,自定义tomcat容器
1,创建mkdir tomcat
2,cd tomcat/
3,touch c.txt
4,sudo cp -p apache-tomcat-8.5.50.tar.gz jdk-8u221-linux-x64.tar.gz /soft/code/tomcat/
Dockerfile:
FROM centos
MAINTAINER zhenghui<zhenghui@163.com>
#把宿主机当前上下文的c.txt,拷贝到要构建的容器的/usr/local/下
COPY c.txt /usr/local/copy_c.txt
#把java和tomcat压缩包添加到容器中
ADD jdk-8u221-linux-x64.tar.gz /usr/local
ADD apache-tomcat-8.5.50.tar.gz /usr/local
#安装vim容器
RUN yum -y install vim
#设置工作访问时候的WORKDIR路径,登录后进入的目录
ENV MYLOGINPATH /usr/local
WORKDIR $MYLOGINPATH
#配置java与tomcat的环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_221
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.50
ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.50
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#抛出对外的端口
EXPOSE 8080
# 启动时运行tomcat
#ENTRYPOINT ["usr/local/apache-tomcat-8.5.50/bin/startup.sh"]
#CMD ["usr/local/apache-tomcat-8.5.50/bin/startup.sh","run"]
CMD usr/local/apache-tomcat-8.5.50/bin/startup.sh && tail -F
/usr/local/apache-tomcat-8.5.50/bin/logzh.out
构建容器
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$ sudo docker build -f Dockerfile -t zhtomcat8 .
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhtomcat8 latest d0596af2705c About a minute ago 721MB
centos latest 470671670cac 3 weeks ago 237MB
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$
运行容器
-t:会运行日志,如果不想看到屏幕上的日志可以不用写
sudo docker run -d -p 8888:8080 --name myt8 -v /soft/code/zhtomcat8/test:/usr/local/apache-tomcat-8.5.50/webapps/test -v /soft/code/zhtomcat8/logs:/usr/local/apache-tomcat-8.5.50/logs --privileged=true mytomcat8
sudo docker run -d
-p 8888:8080
–name myt8
-v /soft/code/zhtomcat8/test:/usr/local/apache-tomcat-8.5.50/webapps/test
-v /soft/code/zhtomcat8/logs:/usr/local/apache-tomcat-8.5.50/logs
–privileged=true
mytomcat8
查看是否运行成功
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
73d69bfc923e mytomcat8 "/bin/sh -c '/usr/lo…" 4 seconds ago Up 3 seconds 0.0.0.0:8888->8080/tcp myt8
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$
浏览器访问http://ip:8888访问测试下,如果出现tomcat的默认界面就说明成功了
其他
sudo docker exec 容器名/容器ID 执行的命令
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$ sudo docker exec myt8 java -version
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$
发布web工程测试
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8/test$ sudo touch index.html
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8/test$
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8/test$ sudo vim index.html
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8/test$
发布war包测试
把war包拷贝到tomcat容器的webpass目录中
sudo docker cp /home/zhenghui/IdeaProjects/code/target/code.war myt8:/usr/local/apache-tomcat-8.5.50/webapps
查看是否成功拷贝
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$ sudo docker exec myt8 ls -l /usr/local/apache-tomcat-8.5.50/webapps
total 32
drwxr-x--- 3 root root 4096 Dec 7 19:23 ROOT
drwxr-x--- 4 root root 4096 Feb 10 17:14 code
-rw-r--r-- 1 1000 1000 2072 Feb 8 06:23 code.war
drwxr-x--- 15 root root 4096 Dec 7 19:23 docs
drwxr-x--- 6 root root 4096 Dec 7 19:23 examples
drwxr-x--- 5 root root 4096 Dec 7 19:23 host-manager
drwxr-x--- 5 root root 4096 Dec 7 19:23 manager
drwxr-xr-x 2 root root 4096 Feb 10 16:35 test
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$
zhenghui@zhenghui-F117-F:/soft/code/zhtomcat8$
转载:https://blog.csdn.net/qq_17623363/article/details/104276578