Search code examples
linuxmongodbdockercoreos

How to start a Docker container with cloud-config file for CoreOS?


I'm trying to configure my CoreOS server with Terraform, using cloud-config file for CoreOS. I am currently trying to set up a Mongo database in a Docker container. Here is my config file:

write_files:
  - path: "/home/core/keyfile"
  permissions: "0600"
  owner: "999"
  content: |
    hUoQVrERB0*** <here is my key for MongoDB>

coreos:
  units:
    - name: "dockerstart.service"
      command: "start"
      content: |
        [Unit]
        Description=Start
        Author=Me

        [Service]
        Restart=always
        ExecStart=/usr/bin/docker run --name mongo -v /home/core:/opt --add-host node1.example.com:127.0.0.1 -p 27017:27017 -d mongo:2.6.5 --smallfiles --keyFile /opt/keyfile --replSet "rs0"
        ExecStop=/usr/bin/docker rm -f mongo

I am not sure how to use coreOS units (when I ssh into the server, the docker container is not running, so the config file is not correct). According to CoreOS Validator, my file is valid. Also, I am not sure if that is the simplest way to deploy a MongoDB server. How to properly use CoreOS units ? Any thoughts on a way to improve how deploy a Mongo Database ?

Any help, comments, suggestions are appreciated !


Solution

  • I finally found the solution.

    Actually running docker run with -d option daemonizes the command. So, when systemd founds out that this action runs in the background, it considers that Docker is crashing.

    Here is journalctl -u dockerstart.service result on server :

    docker[1237]: ace3978442a729420ecb87af224bd146ec6ac7912c5cc452570735f4a3be3a79
    docker[1297]: mongo
    systemd[1]: dockerstart.service: Service hold-off time over, scheduling restart.
    systemd[1]: Stopped Start.
    systemd[1]: Started Start.
    

    Here you can clearly see that systemd stops and restarts the Start service.

    So the solution for this might be removing -d from the docker run command.