标签:重启 数据卷 hang lock ems commit net 上传 本地
目录
Docker已经上市很多年,不是什么新鲜事物了,很多企业或者开发同学以前也不多不少有所接触,但是有实操经验的人不多,本系列教程主要偏重实战,尽量讲干货,会根据本人理解去做阐述,具体官方概念可以查阅官方教程,因为本系列教程对前一章节有一定依赖,建议先学习前面章节内容。
本系列教程导航:
Docker深入浅出 | 容器初体验
在上一篇已经提到过,Docker Image是有多层结构,实际上由一层一层的文件系统组成,底层都是共享宿主Linux内核,Image的分层结构是是为了提高复用性。Image可以看作是Java的class文件,容器可以看成是JAVA的对象去理解,下层的每一层镜像可以是JAVA中的父类,上层镜像可以共享底层镜像的组件,类似JAVA中的继承规则。
Docker Image 基于 Union file systems做镜像和容器分层,避免在每次以新容器运行图像时复制一组完整的文件。将更改分隔为其自身层中的容器文件系统,允许将同一容器置于从已知内容重新启动(因为在删除容器时,更改的图层将被关闭)。
面向用户的是Container层,所有用户新增环境依赖和数据都会保存在容器层,下层Image只可读、不可修改。
Container是一种轻量级的虚拟技术,不需要模拟硬件创建虚拟机启动内核空间,因此启动速度很快
Docker是基于Linux Kernel的Namespace、CGroups、UnionFileSystem等技术封装成的一种自
定义容器格式,从而提供一套虚拟运行环境。
Docker commit
操作 - 通过docker commit命令反向基于容器副本创建一个新的镜像文件。但是使用docker commit
看不到Image的创建过程,因此对排查问题不友好。FROM centos:6
groupadd -r mysql && useradd -r -g mysql mysql
ENV MYSQL_MAJOR 5.7
LABEL email="evan08@163.com"
LABEL name="evan"
VOLUME /var/lib/mysql
COPY demo-api.jar /usr/loacl/app/
ADD demo-api.jar /usr/loacl/app/
WORKDIR /usr/local
WORKDIR tomcat
RUN touch test.txt
会在/usr/local/tomcat下创建test.txt文件
CMD ["mysqld"] 或CMD mysqld
docker run
执行时,会覆盖CMD的命令,而ENTRYPOINT
不会ENTRYPOINT ["docker-entrypoint.sh"]
ENTRYPOINT ["/bin/bash", "-C","/start.sh"]
EXPOSE 3306 3307
更多操作可以查看菜鸟教程
(1)创建一个Springboot项目
指定镜像的工作目录,之后的命令都是基于此目录工作,若不存在则创建
(2)创建一个Rest Api
@RestController
public class DemoController {
@GetMapping("/hello")
public String hello() {
return "Hello";
}
}
(3)通过maven命令 mvn clean package
打包应用
target目录会生成了一个demo-api-0.0.1-SNAPSHOT.jar
(4)进入上一章创建的虚拟机Centos7服务器中创建一个目录demo-dockerfile
目录
evan123
去登陆Centos7服务器Connection to 192.168.100.9 closed.
192:centos7 evan$ ssh root@192.168.100.9
root@192.168.100.9's password:
Last login: Thu Jan 30 03:22:59 2020 from 192.168.100.7
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[root@10 ~]#
[root@10 /]# mkdir /usr/local/demo-dockerfile
(4)回到宿主机器,上传demo-api-0.0.1-SNAPSHOT.jar
到Centos7服务器demo-dockerfile
目录,我这里用使用命令行操作,其他童鞋可以使用其他sfpt工具上传
192:centos7 evan$ scp Users/evan/development/repository/eshare-docker-in-action/demo-api/target/demo-api-0.0.1-SNAPSHOT.jar root@192.168.100.9:/usr/local/demo-dockerfile
root@192.168.100.9's password:
/etc/profile.d/lang.sh: line 19: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
Users/evan/development/repository/eshare-docker-in-action/demo-api/target/demo-api-0.0.1-SNAPSHOT.jar: No such file or directory
192:centos7 evan$ scp /Users/evan/development/repository/eshare-docker-in-action/demo-api/target/demo-api-0.0.1-SNAPSHOT.jar root@192.168.100.9:/usr/local/demo-dockerfile
root@192.168.100.9's password:
/etc/profile.d/lang.sh: line 19: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
demo-api-0.0.1-SNAPSHOT.jar 100% 18MB 105.8MB/s 00:00
192:centos7 evan$
(5)回到虚拟机,在刚才创建的目录下创建一个Dockerfile
文件
192:centos7 evan$ ssh root@192.168.100.9
root@192.168.100.9's password:
Last login: Thu Jan 30 03:25:43 2020 from 192.168.100.7
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[root@10 ~]# cd /usr/local/demo-dockerfile
[root@10 demo-dockerfile]# mkdir Dockerfile
[root@10 demo-dockerfile]# touch Dockerfile
[root@10 demo-dockerfile]# ls
Dockerfile demo-api-0.0.1-SNAPSHOT.jar
(6)填入以下内容到Dockerfile
FROM openjdk:8
MAINTAINER evan
LABEL name="demo-dockerfile" version="1.0"author="evan" COPY demo-api-0.0.1-SNAPSHOT.jar demo-api-image.jar CMD ["java","-jar","demo-api-image.jar"]
(7)先启动Docker服务,假如不存在
[root@10 demo-dockerfile]# sudo systemctl start docker
(8)在该目录下基于Dockerfile
构建自定义的镜像
命令:docker build -t demo-api-image .
运行结果如下:
[root@10 demo-dockerfile]# docker build -t demo-api-image .
Sending build context to Docker daemon 19.32MB
Step 1/5 : FROM openjdk:8
8: Pulling from library/openjdk
146bd6a88618: Already exists
9935d0c62ace: Already exists
db0efb86e806: Already exists
e705a4c4fd31: Already exists
3d3bf7f7e874: Already exists
49371c5b9ff6: Already exists
3f7eaaf7ad75: Already exists
Digest: sha256:7b7408b997615b4d6aaf6c1f0de8a32182497250288ee0a27b4e98cf14a52fb3
Status: Downloaded newer image for openjdk:8
---> 8c6851b1fc09
Step 2/5 : MAINTAINER evan
---> Running in 8fb93afccef8
Removing intermediate container 8fb93afccef8
---> 1f516267494a
Step 3/5 : LABEL name="demo-dockerfile" version="1.0"author="evan"
---> Running in 12cf6c64acf8
Removing intermediate container 12cf6c64acf8
---> 5ab38f113669
Step 4/5 : COPY demo-api-0.0.1-SNAPSHOT.jar demo-api-image.jar
---> d752dc61c8a9
Step 5/5 : CMD ["java","-jar","demo-api-image.jar"]
---> Running in d8bb64f014ee
Removing intermediate container d8bb64f014ee
---> cd026463e853
Successfully built cd026463e853
Successfully tagged demo-api-image:latest
(9)基于我们自定义的image启动容器,容器命名为demo-api
docker run -d --name demo-api -p 8999:8080 demo-api-image
(10)查看容器的启动日志
[root@10 demo-dockerfile]# docker logs demo-api
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.4.RELEASE)
2020-01-30 04:32:29.537 INFO 1 --- [ main] com.example.demo.api.DemoApiApplication : Starting DemoApiApplication v0.0.1-SNAPSHOT on 4b90fdd0dd97 with PID 1 (/demo-api-image.jar started by root in /)
2020-01-30 04:32:29.540 INFO 1 --- [ main] com.example.demo.api.DemoApiApplication : No active profile set, falling back to default profiles: default
2020-01-30 04:32:30.478 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-01-30 04:32:30.488 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-01-30 04:32:30.489 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-01-30 04:32:30.540 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-01-30 04:32:30.540 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 895 ms
2020-01-30 04:32:30.712 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-30 04:32:30.888 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-30 04:32:30.890 INFO 1 --- [ main] com.example.demo.api.DemoApiApplication : Started DemoApiApplication in 1.756 seconds (JVM running for 2.25)
(11)在宿主机器上访问容器服务,测试Api是否成功部署
[root@10 demo-dockerfile]# curl localhost:8999/hello
Hello
(12)再启动多一个容器实例测试
[root@10 demo-dockerfile]# docker run -d --name demo-api02 -p 8998:8080 demo-api-image
3029d6b4325a6773ddcacca8d39917b1abfe12d6004cc8a765608f7f2d64edb3
[root@10 demo-dockerfile]# curl localhost:8998/hello
Hello
(12)登陆Docker镜像仓库,没有账号的童鞋提前到hub.docker.com
注册
[root@10 demo-dockerfile]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 10856214
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
(13)推送定制的Docker镜像到远程仓库,镜像名称要跟Docker id一致
[root@10 demo-dockerfile]# docker tag demo-api-image 10856214/demo-api-image
[root@10 demo-dockerfile]# docker push 10856214/demo-api-image
The push refers to repository [docker.io/10856214/demo-api-image]
9fb0d7d193ef: Pushed
a6ded049566a: Mounted from library/openjdk
e7fe5541de5f: Mounted from library/openjdk
03ff63c55220: Mounted from library/openjdk
bee1e39d7c3a: Mounted from library/openjdk
1f59a4b2e206: Mounted from library/openjdk
0ca7f54856c0: Mounted from library/openjdk
ebb9ae013834: Mounted from library/openjdk
latest: digest: sha256:32afd9d8ca8205d6e667543a66163330c5067c5a37ebd80d53e9563b809e8bb4 size: 2007
(14)查看Image是否已经成功推送到远程仓库
(15)从远程仓库把Image拉到本地
[root@10 demo-dockerfile]# docker pull 10856214/demo-api-image
Using default tag: latest
latest: Pulling from 10856214/demo-api-image
Digest: sha256:32afd9d8ca8205d6e667543a66163330c5067c5a37ebd80d53e9563b809e8bb4
Status: Image is up to date for 10856214/demo-api-image:latest
docker.io/10856214/demo-api-image:latest
(16)基于远程10856214/demo-api-image
启动容器
[root@10 demo-dockerfile]# docker run -d --name demo-api03 -p 8997:8080 10856214/demo-api-image
f6907cc3f8b7c21e26111c748de23d923f96f5b2ffef54478ff49d8c03416651
[root@10 demo-dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6907cc3f8b7 10856214/demo-api-image "java -jar demo-api-…" 4 seconds ago Up 3 seconds 0.0.0.0:8997->8080/tcp demo-api03
3029d6b4325a demo-api-image "java -jar demo-api-…" About an hour ago Up About an hour 0.0.0.0:8998->8080/tcp demo-api02
4b90fdd0dd97 demo-api-image "java -jar demo-api-…" About an hour ago Up About an hour 0.0.0.0:8999->8080/tcp demo-api
[root@10 demo-dockerfile]#
(1)查看运行中的容器
[root@10 demo-dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6907cc3f8b7 10856214/demo-api-image "java -jar demo-api-…" 2 hours ago Up 2 hours 0.0.0.0:8997->8080/tcp demo-api03
3029d6b4325a demo-api-image "java -jar demo-api-…" 3 hours ago Up 3 hours 0.0.0.0:8998->8080/tcp demo-api02
4b90fdd0dd97 demo-api-image "java -jar demo-api-…" 3 hours ago Up 3 hours 0.0.0.0:8999->8080/tcp demo-api
[root@10 demo-dockerfile]#
(2)进入我们在上一节从远程仓库拉回来的自定义镜像内部
[root@10 demo-dockerfile]# docker exec -it demo-api03 bash
root@f6907cc3f8b7:/#
(3)修改容器demo-api03,在容器内部安装yum工具
root@f6907cc3f8b7:/# yum
bash: yum: command not found
root@f6907cc3f8b7:/#
root@f6907cc3f8b7:/# cat /etc/issue
Debian GNU/Linux 9 \n \l
内置的操作系统用的Debian
root@f6907cc3f8b7:/# apt-get update
Ign:1 http://deb.debian.org/debian stretch InRelease
Get:2 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Get:3 http://deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:4 http://deb.debian.org/debian stretch Release [118 kB]
Get:5 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [516 kB]
Get:6 http://deb.debian.org/debian stretch-updates/main amd64 Packages [27.9 kB]
Get:7 http://deb.debian.org/debian stretch Release.gpg [2365 B]
Get:8 http://deb.debian.org/debian stretch/main amd64 Packages [7086 kB]
Fetched 7936 kB in 1min 18s (101 kB/s)
Reading package lists... Done
apt-get install build-essential
apt-get install yum
yum
是否安装成功root@f6907cc3f8b7:/# yum --help
Usage: yum [options] COMMAND
List of Commands:
check Check for problems in the rpmdb
check-update Check for available package updates
clean Remove cached data
deplist List a package's dependencies
...
(4)通过以上步骤,我们已经成功修改了容器,接下来我们推出容器,基于当前容器版本创建一个新的Image,命名为demo-api-image-v1
[root@10 demo-dockerfile]# docker commit demo-api03 demo-api-image-v1
sha256:7887668690a3e53c27535669252f67b0391d54443776f1a431f91fe5800958be
[root@10 demo-dockerfile]#
这时候创建出来的Image是默认已经携带了yum
组件
(5)基于上面更新的新镜像demo-api-image-v1
创建一个容器
[root@10 demo-dockerfile]# docker run -d -it --name demo-api-v1 demo-api-image-v1
6363450e8f7af1f53f935e345d50f7bbfd3ff82bb2409fd5fd75a0f253ed1de8
(6)进入demo-api-v2
容器验证是否已经存在yum
组件
[root@10 demo-dockerfile]# docker exec -it demo-api-v1 bash
root@6363450e8f7a:/# yum
You need to give some command
Usage: yum [options] COMMAND
List of Commands:
check Check for problems in the rpmdb
check-update Check for available package updates
clean Remove cached data
deplist List a package's dependencie
...
docker images docker image ls
docker pull
docker image rm imageid docker rmi -f imageid docker rmi -f $(docker image ls)
删除所有镜像docker run image
docker push
docker run -d --name -p 9090:8080 my-tomcat tomcat
docker ps
docker ps -a
docker rm containerid docker rm -f $(docker ps -a)
删除所有containerdocker exec -it container bash
docker commit demo-api03 demo-api-image-v1
docker logs container
docker stats
docker inspect container
docker stop/start container
https://github.com/EvanLeung08/eshare-docker-in-action.git
标签:重启 数据卷 hang lock ems commit net 上传 本地
原文地址:https://www.cnblogs.com/evan-liang/p/12244304.html