docker容器无法查看共享卷中的数据

时间:2020-08-01 19:04:01

标签: linux docker docker-compose

我正在尝试使用具有基本映像centos7和docker-compose的docker容器建立实验室。 这是我的docker-compose.yaml文件

version: "3"
services:
  base:
    image: centos_base
    build:
      context: base
  master:
    links:
      - base
    build:
      context: master
    image: centos_master
    container_name: master01
    hostname: master01
    volumes:
      - ansible_vol:/var/ans
    networks:
      - net
  host01:
    links:
      - base
      - master
    build: 
      context: host
    image: centos_host
    container_name: host01
    hostname: host01
    command: ["/var/run.sh"]
    volumes:
     - ansible_vol:/var/ans
    networks:
      - net
networks:
  net:
volumes:
  ansible_vol:

我的Docker文件如下

基本映像docker文件:

# For centos7.0
FROM centos:7
RUN yum install -y net-tools man vim initscripts openssh-server
RUN echo "12345" | passwd root --stdin
RUN mkdir /root/.ssh

主Dockerfile:

FROM centos_base:latest
# install ansible package
RUN yum install -y epel-release
RUN yum install -y ansible openssh-clients

RUN mkdir /var/ans
# change working directory
WORKDIR /var/ans

RUN ssh-keygen -t rsa -N 12345 -C "master key" -f master_key

CMD /usr/sbin/sshd -D

主机映像Dockerfile:

FROM centos_base:latest
RUN mkdir /var/ans
COPY run.sh /var/
RUN chmod 755 /var/run.sh

我的run.sh文件:

#!/bin/bash
cat /var/ans/master_key.pub >> /root/.ssh/authorized_keys
# start SSH server
/usr/sbin/sshd -D

我的问题是:

  1. 如果我运行docker-compose up -d --build,则看不到任何容器。他们都被创建了但退出了。
Successfully tagged centos_host:latest
Creating working_base_1 ... done
Creating master01       ... done
Creating host01         ... done

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
433baf2dd0d8        centos_host         "/var/run.sh"            12 minutes ago      Exited (1) 12 minutes ago                       host01
a2a57e480635        centos_master       "/bin/sh -c '/usr/sb…"   13 minutes ago      Exited (1) 12 minutes ago                       master01
a4acf6fb3e7b        centos_base         "/bin/bash"              13 minutes ago      Exited (0) 13 minutes ago                       working_base_1

    即使我在docker-compose文件中添加了卷映射'ansible_vol:/ var / ans',在'centos_master'映像中生成的
  1. ssh密钥在centos_host容器中也不可用 我的意图是在master容器中生成的这些ssh密钥文件应该在主机容器中可用,因此run.sh脚本可以将它们复制到主机容器的authorized_keys部分。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

尝试使用external:false,以不尝试容器创建它并在创建时覆盖先前的数据

version: "3"
services:
  base:
    image: centos_base
    build:
      context: base
  master:
    links:
      - base
    build:
      context: master
    image: centos_master
    container_name: master01
    hostname: master01
    volumes:
      - ansible_vol:/var/ans
    networks:
      - net
  host01:
    links:
      - base
      - master
    build: 
      context: host
    image: centos_host
    container_name: host01
    hostname: host01
    command: ["/var/run.sh"]
    volumes:
     - ansible_vol:/var/ans
    networks:
      - net
networks:
  net:
volumes:
  ansible_vol:
    external: false

答案 1 :(得分:0)

尝试放入base / Dockerfile:

RUN echo "12345" | passwd root --stdin; \
    ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -b 4096 -t rsa

并重新运行docker-compose build

/ etc / ssh / ssh_host_rsa_key是sshd(ssh daemon)使用的密钥,因此可以正确启动容器。

您生成并复制到authorized_keys中的密钥将用于允许ssh client通过ssh连接到容器。