Server Deployments on Ubuntu with Docker and Supervisor
We'll use supervisor to manage docker containers in our host machine.
Installation
Install Docker and Supervisor via apt. There's a mirror that contains the latest version of docker.
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
$ sudo sh -c "echo deb https://get.docker.io/ubuntu docker main\
> /etc/apt/sources.list.d/docker.list"
$ sudo apt-get update
$ sudo apt-get install lxc-docker
Here's a complete guide on how to install docker: https://docs.docker.com/installation/ubuntulinux/
Configuring Docker
Update /etc/default/docker
and add the following line:
DOCKER_OPTS="-r=false $DOCKER_OPTS"
This stops docker from auto-restarting containers that failed to start. We'll let supervisor manage that for us.
Supervisor Configuration
Write a supervisor configuration for your application and save it in /etc/supervisor/conf.d/
.
Here's an example supervisor configuration for your app
[program:myapp]
autorestart=true
autostart=true
command=docker run --name myapp -a stdout -a stderr --rm=true -p 3000:3000 -v /home/ubuntu/deploy:/app -e PORT=3000 me/myapp python server.py
Command Arguments Used
-
--name
sets our container's name. -
-a stdout -a stderr
gets the internal process' stream so we can use supervisor to log. - Stopping a container does not delete it.
--rm
removes the container once it's stopped. -
-p 3000:3000
forwards our host's port 3000 to our container's port 3000. - You can set environment variables inside the container. ie.
-e PORT=3000
Written by Jesse Panganiban
Related protips
2 Responses
Are stopped containers removed by using --rm flag, using supervisord as a provisioner for docker containers? In my case, old containers are not removed,
even when I use --rm flag.
Thanks
Hi,
Wanted to know if --rm flag in the command above does remove the container after it has been stopped? Because i am not seeing that behaviour.
Thanks