首页 > 其他分享 >[Docker] Bind Mounts

[Docker] Bind Mounts

时间:2023-08-07 13:56:04浏览次数:33  
标签:use container Bind mount re bind Mounts Docker containers

So far we've been dealing with self-contained containers. Normally this is all you ever want: containers that can spin up and spin down as frequently as they need to. They're ephemeral, temporary, and disposable. None of these containers are "snowflakes". When I say snowflakes, picture you're running a server that's serving a Wordpress site. Imagine setting up this server, SSH'ing into the server, and setting everything up to be just right and tuned to the exact way you need it. This would be a snowflake server: if someone goes and deletes this server, you're screwed. You have to go and spend a bunch of time re-setting up this server. This is exactly the sort of thing we're trying to avoid with containers. We want to make our servers easy to reproduce whenever we want so we can spin up and spin down servers at will.

However not everything can fit neatly into a container all the time. Sometimes our containers need to be stateful in some capacity. Sometimes our containers need to read and write to the host. This is fundamentally at odds with the idea of a stateless, able-to-create-and-destroy-anytime container that we've been adhering to thusfar. So what are we to do?

Enter volumes and bind mounts. Both of these are methods of reading and writing to the host but with slight-but-important differences of when to use which. We'll go over both.

Bind Mounts

Let's start here because this is easier to see the use case for. Bind mounts allow you to mount files from your host computer into your container. This allows you to use the containers a much more flexible way than previously possible: you don't have to know what files the container will have when you build it and it allows you to determine those files when you run it.

Let's go over an example of how this could be useful.

In the previous project, we used the NGINX container to build a container with our static assets baked into the container. In general this what I recommend you do since now we can ship that container anywhere and it'll just work. It's totally self-contained. But what if we just want to run a NGINX container locally to test stuff out? Sure, we could make a new Dockerfile and write it, but wouldn't it be cool if we could just use the NGINX container directly? We can! Let's try it. Go back to your static site project from the previous lesson. Let's use the nginx container to serve directly from it.

# from the root directory of your CRA app
docker run --mount type=bind,source="$(pwd)"/build,target=/usr/share/nginx/html -p 8080:80 nginx

This is how you do bind mounts. It's a bit verbose but necessary. Let's dissect it.

  • We use the --mount flag to identify we're going to be mounting something in from the host.
  • As far as I know the only two types are bind and volume. Here we're using bind because we to mount in some piece of already existing data from the host.
  • In the source, we identify what part of the host we want to make readable-and-writable to the container. It has to be an absolute path (e.g we can't say "./build") which is why use the "$(pwd)" to get the present working directory to make it an absolute path.
  • The target is where we want those files to be mounted in the container. Here we're putting it in the spot that NGINX is expecting.
  • As a side note, you can mount as many mounts as you care to, and you mix bind and volume mounts. NGINX has a default config that we're using but if we used another bind mount to mount an NGINX config to /etc/nginx/nginx.conf it would use that instead.

Again, it's preferable to bake your own container so you don't have to ship the container and the code separately; you'd rather just ship one thing that you can run without much ritual nor ceremony. But this is a useful trick to have in your pocket.

标签:use,container,Bind,mount,re,bind,Mounts,Docker,containers
From: https://www.cnblogs.com/Answer1215/p/17611242.html

相关文章

  • linux(centos版本)下docker部署.netcore项目
    准备:1.要在Linux系统上使用Docker部署系统,你需要安装Docker引擎以及一些其他相关的软件。以下是安装步骤:安装Docker引擎:对于Ubuntu系统,可以使用以下命令安装Docker:sudoapt-getupdatesudoapt-getinstalldocker-cedocker-ce-clicontainerd.......
  • DockerFile 构建缓存 Buildkit
    缓存Dockerfile构建缓存功能从Docker17.05版本开始引入可以加速构建直接服用缓存构建我们看到fromcache就是来自缓存缓存是一个命令为单位如果想服用缓存需要考虑查看缓存大小清除缓存dockervolumeprune--all--forcedockerimageprune--all--force......
  • 微服务架构下的应用程序开发与Docker实战指南
    前言本文将带领读者探索在微服务架构下如何使用Docker来进行应用程序开发。我们将详细介绍微服务的基本概念,深入了解Docker容器的原理与优势,并通过一个实际的案例演示如何使用Docker容器化和部署多个微服务。本文还包含实际代码示例,帮助读者更好地理解和实践微服务架构与Docker的结......
  • 将Docker与DevOps文化结合:实现自动化、监控与日志管理
    引言:DevOps文化作为一种融合开发和运维的方法论,旨在加速软件交付和增强团队协作。而Docker作为一种轻量级、可移植的容器化技术,提供了快速部署和高度可扩展的解决方案。本文将深入探讨如何将Docker与DevOps文化结合,以实现自动化部署、高效监控和集中式日志管理,为现代软件开发与运维......
  • 如何构建一个 NodeJS 影院微服务并使用 Docker 部署
    前言如何构建一个NodeJS影院微服务并使用Docker部署。在这个系列中,将构建一个NodeJS微服务,并使用DockerSwarm集群进行部署。以下是将要使用的工具:NodeJS版本7.2.0MongoDB3.4.1DockerforMac1.12.6在尝试本指南之前,应该具备:NodeJS的基本知识Docker的基本知识(并且已经......
  • Docker部署SpringBoot项目微服务
     登录linux服务器,进入目录  usr/local目录,创建两个文件夹使用alt+p快捷键,将windows目录中生成的jar包,拖到linux桌面使用cp命令,将linux桌面中jar包,复制到指定目录cplesson01-0.0.1-SNAPSHOT.jar/usr/local/docker/lesson01使用vi命令,编辑dockerfile,制作镜......
  • docker 安装 Influxdb-relay 使用
    转载请注明出处:1.influxdbrelay的作用和特性数据转发:InfluxDBRelay可以从一个或多个源(input)接收数据,并将其转发到一个或多个目标(output)的InfluxDB实例中。负载均衡:Relay可以通过在多个目标实例之间分配请求来提供负载均衡,确保可伸缩性和高可用性。故障恢复:当某个目标实......
  • Docker部署SpringBoot项目准备
     创建一个SpringBoot项目测试程序,在Windows中执行java  -jar  xxx.jar查看效果java-jarlesson01-0.0.1-SNAPSHOT.jar主要代码1、pom.xml文件<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0&q......
  • Windows11使用docker desktop安装kafka&zookeeper集群
    docker-compose安装zookeeper集群参考文章:http://t.csdn.cn/TtTYIhttps://blog.csdn.net/u010416101/article/details/122803105?spm=1001.2014.3001.5501准备工作:​ 在开始新建集群之前,新建好文件夹,用来挂载kafka、zookeeper数据,如下:zookeeper文件夹D:\soft\docker\zookee......
  • centos下安装docker
    #1.更新yumyumupdate#2.yum安装docker的依赖包yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2#3.配置yum源yum-config-manager--add-repohttps://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo#查看仓库中的docker版本yumlistdock......