如何在Windows中播种Docker容器 [英] How to seed a docker container in Windows

查看:94
本文介绍了如何在Windows中播种Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算从 Docker Hub 安装mongodb Docker容器,然后插入一些数据进去.显然,需要一个mongodb种子容器.所以我做了以下事情:

I intended to install a mongodb docker container from Docker Hub, and then insert some data into it. Obviously, a mongodb seed container is needed. So I did the following:

  1. mongo_seed/Dockerfile中创建了一个Mongo种子容器Dockerfile,并且Dockerfile中的代码如下:

  1. created a Dockerfile of Mongo seed container in mongo_seed/Dockerfile and the code in Dockerfile is the following:

FROM mongo:latest
WORKDIR /tmp
COPY data/shops.json .
COPY import.sh .
CMD ["/bin/bash", "-c", "source import.sh"]

import.sh的代码如下:

#!/bin/bash
ls .
mongoimport --host mongodb --db data --collection shops --file shops.json

shops.json文件包含要导入到Mongo的数据

the shops.json file contains the data to be imported to Mongo

当前工作目录中创建了docker-compose.yml文件,其代码如下:

created a docker-compose.yml file in the current working directory, and the code is the following:

version: '3.4'
services:
  mongodb:
    image: mongo:latest
    ports: 
    - "27017:27017"
    container_name: mongodb 
  mongodb_seed:
    build: mongodb_seed
    links:
    - mongodb

上面的代码成功地使mongodb服务执行import.sh以导入json数据-shops.json. 它在我的Ubuntu中完美运行.但是,当我尝试在Windows中运行命令docker-compose up -d --build mongodb_seed时,数据导入失败,并显示错误日志:

The code above successfully made the mongodb service execute the import.sh to import the json data - shops.json. It works perfectly in my Ubuntu. However, when I tried to run command docker-compose up -d --build mongodb_seed in Windows, the import of data failed with errors logs:

Attaching to linux_mongodb_seed_1
mongodb_seed_1  | ls: cannot access '.'$'\r': No such file or directory
: no such file or directory2T08:33:45.552+0000  Failed: open shops.json
mongodb_seed_1  | 2019-04-02T08:33:45.552+0000  imported 0 documents

任何人都知道为什么会这样吗?以及如何修复它使其也可以在Windows中使用?

Anyone has any ideas why it was like that? and how to fix it so that it can work in Windows as well?

推荐答案

注意错误ls: cannot access '.'$'\r': No such file or directory.

Windows上的Docker(或任何基于Linux/macOS的系统)的问题之一是如何处理行尾.

One of the issues with Docker (or any Linux/macOS based system) on Windows is the difference in how line endings are handled.

Windows在回车符和换行符\r\n中结束行,而Linux和macOS仅使用换行符\n.当您尝试在Windows中创建文件并在Linux/macOS系统上运行该文件时,这将成为一个问题,因为这些系统将\r视为文本而不是换行符.

Windows ends lines in a carriage return and a linefeed \r\n while Linux and macOS only use a linefeed \n. This becomes a problem when you try to create a file in Windows and run it on a Linux/macOS system, because those systems treat the \r as a piece of text rather than a newline.

确保每当有人在Windows上的任何类型的编辑器上进行任何编辑时,都对脚本文件运行dos2unix.即使在Git Bash上创建了脚本文件,也不要忘记运行dos2unix

Make sure to run dos2unix on script file whenever anyone edit anything on any kind of editor on Windows. Even if script file is being created on Git Bash don’t forget to run dos2unix

dos2unix import.sh

请参见 https ://willi.am/blog/2016/08/11/docker-for-windows-dealing-with-windows-line-endings/

在您的情况下:

FROM mongo:latest
RUN apt-get update && apt-get install -y dos2unix
WORKDIR /tmp
COPY data/shops.json .
COPY import.sh .
RUN dos2unix /import.sh && apt-get --purge remove -y dos2unix
CMD ["/bin/bash", "-c", "source import.sh"]

这篇关于如何在Windows中播种Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆