2

I'm trying to run a Docker container with flask, uwsgi and nginx in a docker container.

my Dockerfile looks so:

FROM ubuntu:16.04

MAINTAINER Dockerfiles

# Install required packages and remove the apt packages cache when done.
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y \
    ...
    
# install uwsgi 
RUN pip3 install uwsgi

# copy over requirements.txt file
COPY requirements.txt /home/docker/code/

# upgrade pip and install required python packages
RUN pip3 --no-cache-dir install -U pip
RUN pip3 --no-cache-dir install -r /home/docker/code/requirements.txt

# add (the rest of) our code
COPY ./app /home/docker/code/

# create a systemd unit file
COPY ./app.service /etc/systemd/system/  # Think the problem is here

# start the uwsgi service
RUN systemctl start app  # This is not working
RUN systemctl enable app

# setup all the configfiles
COPY nginx_app /etc/nginx/sites-available/

# enable nginx server block
RUN ln -s /etc/nginx/sites-available/nginx_app /etc/nginx/sites-enabled

# validate syntax
CMD ["nginx", "-t"]

# restart nginx
RUN systemctl restart nginx

# allow nginx
RUN ufw allow 'Nginx Full'

and then my app.service so:

[Unit]
Description=uWSGI instance to serve app
After=network.target

[Service]
User=docker
Group=www-data
WorkingDirectory=/home/docker/code
ExecStart=/usr/local/bin/uwsgi --ini uwsgi.ini

[Install]
WantedBy=multi-user.target

the nginx configuration

server {
    listen 80;
    server_name my_server_ip;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/docker/code/myproject.sock;
    }

then my uwsgi.ini

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = app.sock
chmod-socket = 664
vacuum = true

die-on-term = true

But when I try to build it, I get this error

Step 12 : RUN systemctl start app

---> Running in 79a22c2629db

failed to connect to bus: No such file or directory

INFO[0022] The command [/bin/sh -c systemctl start app] returned a non-zero code: 1

I was reading and I think that the problem is with the systemd. I should use supervisord. But I don't have a clue how to do it.

On a "normal" server I could run those commands and it would work, but in a docker container is not working.

The "only" problem I've is that I have to run my app with uwsgi and nginx but I don't get it.

I read many posts and tutorials on Internet and also tried to change this project but it still not working and I'd prefer to use my dockerfile (this one) because it's what I understand.

2
  • It doesn't really make sense to run an app as a systemd service inside a Docker container. Why are you doing that? Commented Feb 12, 2018 at 11:08
  • I would like to run it with supervisord, but I don't know how to do it. I'm new with docker containers and the only way I knew was with systemd. Commented Feb 12, 2018 at 11:09

2 Answers 2

5

I found the answer.

I resolved it with supervisord.

# install uwsgi now because it takes a little while
RUN pip3 install uwsgi

# setup all the configfiles
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
COPY nginx_app.conf /etc/nginx/sites-available/default
COPY supervisor_app.conf /etc/supervisor/conf.d/


# copy over our requirements.txt file
COPY requirements.txt /home/docker/code/

# upgrade pip and install required python packages
RUN pip3 --no-cache-dir install -U pip
RUN pip3 --no-cache-dir install -r /home/docker/code/requirements.txt

# add (the rest of) our code
COPY ./app /home/docker/code/

EXPOSE 80
CMD ["supervisord"]

and then the supervisor_app.conf looks so

[supervisord]
nodaemon=true

[program:uwsgi]
command = /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:nginx]
command = /usr/sbin/nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

the uwsg.ini

[uwsgi]
callable = app
chdir = /home/docker/code/
wsgi-file = /home/docker/code/wsgi.py
socket = /home/docker/code/app.sock
master = true
processes = 2
chmod-socket = 666
enable-threads = true

and my nginx_app.conf

server {
    listen 80 default_server;
    server_name my_ip;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///home/docker/code/app.sock;
    }
}

and that's it

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, Iam trying to follow these same configuration file with code folder name different at all the places. Step 8/21 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf ---> Running in 876f489fc21c /bin/sh: 1: cannot create /etc/nginx/nginx.conf: Directory nonexistent Getting this error while building the image. Can you plz help me out here . Thanks
0

You could also use the docker-systemctl-replacement which has been specifically made to allow the same systemctl commands to be used in a docker container as on a real systemd controlled machine. When running it as CMD then it will start all services that have been enabled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.