暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Docker命令行参数和Dockerfile指令「收藏版」

IT大咖说 2020-06-29
3619

一、介绍

如今有越来越多的应用运行在容器里,我们可以将应用程序部署包以及程序运行所需要的任何东西都打包到容器镜像里。在这个容器里可以包含一个基本操作系统,依赖库,文件和目录,环境变量,挂载点以及应用程序二进制包。

容器镜像是一个容器运行的模板。通过一个相同的镜像启动多个容器实例,这些容器实例共享相同的行为,比如扩缩容,应用的分布。这些镜像存储在远程的镜像仓库里进行统一管理和统一分发。

一旦这个容器被启动,容器的执行过程以及容器的生命周期通过容器运行时本身管理。可以通过docker这个命令进行交互。

二、架构图

在整个容器架构里包含三个主要的组件:Client,Runtime和Registry.

Docker架构图

三、Docker命令行

1、与容器相关的命令

docker [CMD] [OPTS] [CONTAINER]

以交互模式启动容器

#在容器内部运行bash [root@localhost ~]# docker run -it richxsl/rhel7 bash #进入到容器里查看操作系统[root@ea569e9732e1 ]# cat etc/redhat-release Red Hat Enterprise Linux Server release 7.0 (Maipo)[root@ea569e9732e1 ]#

以后台进程模式运行

$ docker run --name mywildfly -d -p 8080:8080 jboss/wildfly

使用新创建的网络,以后台进程模式运行容器

$ docker network create mynetwork$ docker run --name mywildfly-net -d --net mynetwork \ -p 8080:8080 jboss/wildfly

挂载本地文件目录到容器内部的目录,以后台进程模式运行容器

$ docker run --name mywildfly-volume -d \  						-v myfolder/:/opt/jboss/wildfly/standalone/deployments/ \              -p 8080:8080 jboss/wildflyjboss/wildfly

查看一个容器的log

$ docker logs -f mywildfly$ docker logs -f [container-name|container-id]

查看容器列表

#查看运行中的docker容器$ docker ps#查看所有容器$ docker ps -a

停止容器

$ docker stop [container-name|container-id]#停止一个容器,超时1秒$ docker stop -t1

移除一个容器

# move a stopped container$ docker rm [container-name|container-id]# Force stop and remove a container$ docker rm -f [container-name|container-id]# Remove all containers$ docker rm -f $(docker ps-aq)# Remove all stopped containers$ docker rm $(docker ps -q -f “status=exited”)

在容器里执行一个进程

# Execute and access bash inside a WildFly container$ docker exec -it mywildfly bash

2、与镜像相关的命令

docker [CMD] [OPTS] [IMAGE]

使用Dockerfile构建一个镜像

#Build an image$ docker build -t [username/]<image-name>[:tag] <dockerfile-path>  #Build an image called myimage using the Dockerfile in the same folder where the command was executed$ docker build -t myimage:latest .

检查一个镜像的历史

# Check the history of the jboss/wildfly image$ docker history jboss/wildfly# Check the history of an image$ docker history [username/]<image-name>[:tag]

显示镜像

$ docker images

移除本地的镜像

$ docker rmi [username/]<image-name>[:tag]

为一个镜像打Tag

# Creates an image called “myimage” with the tag “v1” for the image jboss/wildfly:latest$ docker tag jboss/wildfly myimage:v1# Creates a new image with the latest tag$ docker tag <image-name> <new-image-name>   # Creates a new image specifying the “new tag” from an existing image and tag$ docker tag <image-name>[:tag][username/]<new-image-name> .[:new-tag]

从一个镜像导出一个文件或从一个文件导入镜像

# Export the image to an external file$ docker save -o <filename>.tar# Import an image from an external file$ docker load -i <filename>.tar

将一个镜像推送到镜像仓库

$ docker push [registry/][username/]<image-name>[:tag]

3、网络相关的命令

docker network [CMD] [OPTS]

4、镜像仓库相关的命令

Default is https://index.docker.io/v1/

5、挂载卷相关的命令

docker volume [CMD] [OPTS]

6、其他的命令

四、Dockerfile文件

Dockerfile提供了构建容器镜像的指令,可以通过

`docker build -t [username/]<image-name>[:tag] <dockerfile-path> `

命令。一个dockerfile从一个基础镜像开始,然后后面是一些其他Dockerfile指令。这个过程与从源码编译成二进制文件一样,只不过dockerfile的输出是一个容器镜像。

Dockerfile样例

这个例子通过一个自定义的管理账户创建一个WildFly容器,暴露了9990的管理端口,并通过'bmanagement'参数设置为公开的。

# 使用已经存在的wildfly镜像FROM jboss/wildfly # 添加一个管理员账号RUN opt/jboss/wildfly/bin/add-user.sh admin Admin#70365 --silent #暴露管理端口EXPOSE 8080 9990 #绑定WildFly管理端口到所有IP地址CMD [“/opt/jboss/wildfly/bin/standalong.sh”, “-b”, “0.0.0.0”, “-bmanagement”, “0.0.0.0”]

通过如下命令使用该Dockerfile

# 通过该Dockerfile构建镜像$ docker build -t mywildfly .#运行WildFly server$ docker run -it -p 8080:8080 -p 9990:9990 mywildfly#访问管理控制台,并使用admin/Admin#70635登录open http://<docker-daemon-ip>:9990 in a browser

Dockerfile指令列表:

来源:

https://www.toutiao.com/a6842957946431734280/

“IT大咖说”欢迎广大技术人员投稿,投稿邮箱:aliang@itdks.com

来都来了,走啥走,留个言呗~

 IT大咖说  |  关于版权 

由“IT大咖说(ID:itdakashuo)”原创的文章,转载时请注明作者、出处及微信公众号。投稿、约稿、转载请加微信:ITDKS10(备注:投稿),茉莉小姐姐会及时与您联系!

感谢您对IT大咖说的热心支持!

相关推荐

推荐文章


文章转载自IT大咖说,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论