skip to Main Content

I’m trying to set up a lab using docker containers with base image centos7 and docker-compose.
Here is my docker-compose.yaml file

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:

My Docker files are as below

Base Image docker file:

# 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

Master 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

Host Image Dockerfile:

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

My run.sh file:

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

My Problems are:

  1. If I run docker-compose up -d –build, I see no containers coming up. they all getting created but exiting.
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

  1. ssh keys generated in ‘centos_master’ image are not available in centos_host container, even though I have added a volume mapping ‘ansible_vol:/var/ans’ in docker-compose file
    My intention is these ssh key files generated in master should be available in host containers ,therefore the run.sh script can copy them to authorized_keys section of host containers.

Any help is greatly appreciated.

2

Answers


  1. Try to use external: false, to not attempt the container to create it and override the previous data at creation

    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
    
    Login or Signup to reply.
  2. Try to put in base/Dockerfile :

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

    and rerun docker-compose build

    /etc/ssh/ssh_host_rsa_key is a key used by sshd (ssh daemon), so that containers can be started properly.

    The key you generated and copied into authorized_keys will be used to allow ssh client to connect to container via ssh.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search