Skip to content

Instantly share code, notes, and snippets.

@rst0git
Created August 17, 2019 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rst0git/eea73821ef927c04d49fe2f4ba035e02 to your computer and use it in GitHub Desktop.
Save rst0git/eea73821ef927c04d49fe2f4ba035e02 to your computer and use it in GitHub Desktop.
Migrate Flask server between containers

Example:

docker-compose up -d nginx web1

curl http://10.0.0.4

docker-compose exec web1 sh -c 'criu dump -t $(pidof python3) -j -D /mnt --tcp-established'
docker-compose up -d web2

curl http://10.0.0.4
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello world!\n"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)
version: "2.4"
services:
web1:
privileged: true
build: .
expose:
- "80"
volumes:
- "checkpoint:/mnt"
networks:
app_net:
ipv4_address: 10.0.0.2
web2:
command: criu restore -D /mnt --tcp-established
privileged: true
build: .
expose:
- "80"
volumes:
- "checkpoint:/mnt"
networks:
app_net:
ipv4_address: 10.0.0.3
nginx:
image: nginx:stable-alpine
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "80:80"
networks:
app_net:
ipv4_address: 10.0.0.4
networks:
app_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 10.0.0.0/24
gateway: 10.0.0.1
volumes:
checkpoint:
FROM ubuntu:18.04 as build
RUN apt-get update -qq
RUN apt-get install -qq \
libnet-dev \
libnl-route-3-dev \
gcc \
bsdmainutils \
build-essential \
git-core \
iptables \
libbsd-dev \
libaio-dev \
libcap-dev \
libgnutls28-dev \
libgnutls30 \
libnl-3-dev \
libprotobuf-c-dev \
libprotobuf-dev \
libselinux-dev \
pkg-config \
protobuf-c-compiler \
protobuf-compiler \
python3-minimal \
python3-distutils \
asciidoctor \
curl
RUN git clone --depth 1 https://github.com/checkpoint-restore/criu/ /usr/src/criu \
&& cd /usr/src/criu \
&& make -j $(nproc) \
&& make install PREFIX=/usr \
&& rm -rf /usr/src/criu
RUN apt-get install -qq python3-flask
COPY app.py /app.py
CMD python3 app.py
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream web {
server 10.0.0.2:80;
server 10.0.0.3:80;
}
server {
listen 80;
location / {
proxy_pass http://web;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment