3

Im just experimenting with coreOS, docker and fleet. I have the next dockerfile:

FROM ubuntu:14.04

RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y install nginx

RUN echo "daemon off;" >> /etc/nginx/nginx.conf
RUN mkdir /etc/nginx/ssl
ADD default /etc/nginx/sites-available/default

EXPOSE 80

CMD ["nginx"]

I created an image ("nginx-example") from this file and i can launch the container with:

docker run -v /home/core/share:/var/www:rw -p 80:80 -d nginx-example

Now, I want to launch it with fleet, so I undertand that I have to create a service file and then launching it with fleet.

So I try to create de service file (nginx1.service):

[Unit]
Description=MyTry
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill nginx
ExecStartPre=-/usr/bin/docker rm nginx
ExecStartPre=/usr/bin/docker pull nginx-example
ExecStart=/usr/bin/docker docker run -p 80:80 -d nginx-example  
ExecStop=/usr/bin/docker stop nginx

I submmited and started it but when I do:

fleetctl list-units
nginx1.service  cbbed2c1.../IP  failed      failed

And I cant run the web server. I think that the problem is in the service file but I dont know how to construct it. Thank you.

BMW
  • 42,880
  • 12
  • 99
  • 116
Kimy BF
  • 1,029
  • 3
  • 13
  • 23

3 Answers3

1

You shouldn't start your container in daemon-mode (-d):

"If you are going to modify these units, be sure you don't copy a docker run command that starts a container in detached mode (-d). Detached mode won't start the container as a child of the unit's pid. This will cause the unit to run for just a few seconds and then exit."

https://coreos.com/docs/launching-containers/launching/fleet-example-deployment/#service-files

mbo
  • 758
  • 1
  • 5
  • 11
1

This works:

[Service]
TimeoutStartSec=0
ExecStartPre=/usr/bin/docker pull kimberlybf/nginx-example:latest
ExecStart=/usr/bin/docker run -p 80:80 -d --name nginx kimberlybf/nginx-example:latest

And I push my image to DockerHub.

Kimy BF
  • 1,029
  • 3
  • 13
  • 23
0

Here's a key line in your service file that should get you thinking:

ExecStartPre=/usr/bin/docker pull nginx-example

Where do you think is this image being pulled from?
In order to pull an image, you need to push it somewhere first. The easiest, of course, is DockerHub. You will need to create an account. I'll leave the exercise of creating the account, repository, and configuring authentication to you, as the documentation is readily available here.

Now, if you were to just try docker push nginx-example, it would fail, because it needs to be associated with your user account's namespace, via a tag. For the sake of this answer, let's assume your account is kimberlybf.

$ docker tag nginx-example:latest kimberlybf/nginx-example:latest - this will tag your image correctly for pushing to DockerHub.

$ docker push kimberlybf/nginx-example:latest - this will actually push your image. The image will be public, so don't put any sensitive data in your configs.

Then you would modify your Service, and replace the container tags accordingly, also remembering to give your container a name, e.g.:

[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill nginx
ExecStartPre=-/usr/bin/docker rm nginx
ExecStartPre=/usr/bin/docker pull kimberlybf/nginx-example:latest
ExecStart=/usr/bin/docker docker run -p 80:80 -d --name nginx kimberlybf/nginx-example:latest
ExecStop=/usr/bin/docker stop nginx
ebr
  • 606
  • 8
  • 13
  • Not directly related to your question, but another tidbit that might help you troubleshoot, is getting journal logs. `journalctl --unit=nginx1.service` will "tail" the logs from the service, so hopefully if it fails to start, you should be able to glean some clues from there. – ebr Jun 25 '15 at 16:29
  • Thank you, actually `journalctl --unit=nginx1.service` helped me a lot. And I push my image so it works, well i had to delete this lines too: `ExecStartPre=-/usr/bin/docker kill nginx ExecStartPre=-/usr/bin/docker rm nginx ExecStop=/usr/bin/docker stop nginx` – Kimy BF Jul 02 '15 at 21:23
  • Glad that helped. Though if I understand your message correctly, you've deleted the lines that kill, remove, and stop the container. You should keep those, because when the service is stopped, you want it stopped gracefully, and when it's started, you want to ensure old lingering containers are cleaned up. Though there are different use cases of course. At any rate, if my answer seems correct, please feel free to mark it as such :) Thanks. – ebr Jul 15 '15 at 22:13