How to launch a docker with fleet given a dockerfile?

620 views Asked by At

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.

3

There are 3 answers

3
ebr On BEST ANSWER

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
0
mbo On

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

0
Kimy BF On

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.