docker的安装及基础操作与镜像构建

仓库配置及安装启动

[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]#  yum makecache fast
[root@localhost ~]# yum -y install docker-ce
[root@localhost ~]#  service docker start
Redirecting to /bin/systemctl start docker.service

版本查看

[root@localhost ~]# docker -v
Docker version 18.09.0, build 4d60db4

镜像下载

[root@localhost ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
4fe2ade4980c: Pull complete 
Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Status: Downloaded newer image for alpine:latest

查看的本地镜像

[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              196d12cf6ab1        3 months ago        4.41MB

镜像启动成容器

[root@localhost ~]# docker run -it alpine sh
/ # 

 容器里创建一个大小为20M

/ # dd if=/dev/zero of=chenxi bs=10M count=2
2+0 records in
2+0 records out
/ # ls -l chenxi 
-rw-r--r--    1 root     root      20971520 Dec 12 04:31 chenxi

用快捷键Ctrl+p,q退出容器,根据此运行的容器制作镜像

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
292d32b4b3a1        alpine              "sh"                15 minutes ago      Up 15 minutes                           condescending_wu
[root@localhost ~]# docker commit 292d32b4b3a1 chenxi:test
sha256:cfcb3e42b392ac74fb8984f339916cbd062a968f6398af47e24d55dbb4364152
[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
chenxi              test                cfcb3e42b392        13 seconds ago      25.4MB
alpine              latest              196d12cf6ab1        3 months ago        4.41MB

查看镜像层

[root@localhost ~]# docker history  alpine:latest 
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
196d12cf6ab1        3 months ago        /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B                  
<missing>           3 months ago        /bin/sh -c #(nop) ADD file:25c10b1d1b41d46a1…   4.41MB              
[root@localhost ~]# docker history  chenxi:test 
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
cfcb3e42b392        2 minutes ago       sh                                              21MB                
196d12cf6ab1        3 months ago        /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B                  
<missing>           3 months ago        /bin/sh -c #(nop) ADD file:25c10b1d1b41d46a1…   4.41MB              

使用dockerfil简单构建tomcat镜像文件

[root@localhost ~]# ls /tomcat/
apache-tomcat-9.0.13.tar.gz  Dockerfile
[root@localhost ~]# cat /tomcat/Dockerfile 
FROM centos
RUN mkdir /tomcat && yum -y install java 
COPY apache-tomcat-9.0.13.tar.gz /tomcat
RUN tar -zvxf /tomcat/apache-tomcat-9.0.13.tar.gz -C /usr/local/ 
EXPOSE 8080
CMD ["/usr/local/apache-tomcat-9.0.13/bin/catalina.sh", "run"]

[root@localhost tomcat]#  docker build -f Dockerfile -t tomcat:chenxi .
Sending build context to Docker daemon  9.991MB
Step 1/6 : FROM centos
 ---> 1e1148e4cc2c
Step 2/6 : RUN mkdir /tomcat && yum -y install java
 ---> Using cache
 ---> 05e991a5d3f7
Step 3/6 : COPY apache-tomcat-9.0.13.tar.gz /tomcat
 ---> Using cache
 ---> 3dffdd201d5f
Step 4/6 : RUN tar -zvxf /tomcat/apache-tomcat-9.0.13.tar.gz -C /usr/local/
 ---> Using cache
 ---> 1b8021e0f9fb
Step 5/6 : EXPOSE 8080
 ---> Using cache
 ---> a3d6bc853d8c
Step 6/6 : CMD ["/usr/local/apache-tomcat-9.0.13/bin/catalina.sh", "run"]
 ---> Using cache
 ---> 3969b96f4e53
Successfully built 3969b96f4e53
Successfully tagged tomcat:chenxi

查看构建的容器镜像并启动

[root@localhost tomcat]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              7                   3969b96f4e53        7 minutes ago       445MB
tomcat              chenxi              3969b96f4e53        7 minutes ago       445MB
<none>              <none>              2b60cff33319        2 days ago          435MB
<none>              <none>              0717bf973613        2 days ago          435MB
centos              latest              1e1148e4cc2c        8 days ago          202MB
[root@localhost tomcat]# docker run -d --name chenxi -p 8080:8080 tomcat:chenxi
a7713ecd235158cdc7c276d057a8fb06561002f119d80b6ee882810e1d3c8488
[root@localhost tomcat]# ss -lntp
State       Recv-Q Send-Q                                                  Local Address:Port                                                                 Peer Address:Port              
LISTEN      0      128                                                                 *:22                                                                              *:*                   
users:(("sshd",pid=1022,fd=3))LISTEN      0      100                                                         127.0.0.1:25                                                                              *:*                   
users:(("master",pid=1293,fd=13))LISTEN      0      128                                                                :::8080                                                                           :::*                   
users:(("docker-proxy",pid=2259,fd=4))LISTEN      0      128                                                                :::22                                                                             :::*                   
users:(("sshd",pid=1022,fd=4))LISTEN      0      100                                                               ::1:25                                                                             :::*                   
users:(("master",pid=1293,fd=14))

  

草都可以从石头缝隙中长出来更可况你呢
原文地址:https://www.cnblogs.com/rdchenxi/p/10109456.html