Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate docker-compose.yml to Dockerfile

I have an application written in Django and I am trying to run it in docker on Digital Ocean droplet. Currently I have two files.

Can anybody advise how to get rid of docker-compose.yml file and integrate all the commands within Dockerfile ???

Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY . /code/
RUN pip install -r reqirements.txt
RUN python /code/jk/manage.py collectstatic --noinput

docker-compose.yml

version: '3'
services:
  web:
    build: .
    command: python jk/manage.py runserver 0.0.0.0:8081
    volumes:
      - .:/code
    ports:
      - "8081:8081"

I run my application and docker image like following:

  • docker-compose run web python jk/manage.py migrate
  • docker-compose up

output:

Starting workspace_web_1 ...
Starting workspace_web_1 ... done
Attaching to workspace_web_1
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  | December 02, 2017 - 09:20:51
web_1  | Django version 1.11.3, using settings 'jk.settings'
web_1  | Starting development server at http://0.0.0.0:8081/
web_1  | Quit the server with CONTROL-C.
...

Ok so I have take the following approach: Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY . /code/
RUN pip install -r reqirements.txt
RUN python /code/jk/manage.py collectstatic --noinput

then I ran:

 docker build -t "django:v1" .

So docker images -a throws:

docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
django              v1                  b3dec6aaf9b9        5 minutes ago       949MB
<none>              <none>              55370397f7f2        5 minutes ago       948MB
<none>              <none>              e7eba7113203        7 minutes ago       693MB
<none>              <none>              dc3d7705c45a        7 minutes ago       691MB
<none>              <none>              12825382746d        7 minutes ago       691MB
<none>              <none>              2304087e8b82        7 minutes ago       691MB
python              3                   79e1dc9af1c1        3 weeks ago         691MB

And finally I ran:

cd /opt/workspace
docker run -d -v /opt/workspace:/code -p 8081:8081 django:v1 python jk/manage.py runserver 0.0.0.0:8081

Two simple questions:

  1. do i get it right that each <none> listed image is created when running docker build -t "django:v1" . command to build up my image ... So it means that it consumes like [(691 x 4) + (693 x 1) + (948) + (949)]MB of disk space ??
  2. Is it better to use gunicorn or wsgi program to run django in production ?

And responses from @vmonteco:

  1. I think so, but a way to reduce the size taken by your images is to reduce their number by using a single RUN directive for several chained commands in your Dockerfile. (RUN cmd1 && cmd2 rather than RUN cmd1 then RUN cmd
  2. It's up to you to make your own opinion. I personnally use uWSGI but there even are other choices than gunicorn/uwsgi (Not just "wsgi", that's the name of a specification for interface, not a programm). Have fun finding your prefered one! :)
like image 795
user2156115 Avatar asked Dec 02 '17 09:12

user2156115


People also ask

Is docker compose the same as Dockerfile?

A Dockerfile is a simple text file that contains the commands a user could call to assemble an image whereas Docker Compose is a tool for defining and running multi-container Docker applications. Docker Compose define the services that make up your app in docker-compose.

Does docker compose replace Dockerfile?

No—Docker Compose does not replace Dockerfile. Dockerfile is part of a process to build Docker images, which are part of containers, while Docker Compose is used for orchestrating.

Does docker compose read Dockerfile?

Dockerfile and docker-compose combined In this case, the docker-compose command actually uses a Dockerfile to build the latest version of a Docker image before it runs it. The docker-compose. yaml file can reference a Dockerfile and force a new Docker image to be built.


1 Answers

TL;DR

You can pass some informations to your Dockefile (the command to run) but that wouldn't be equivalent and you can't do that with all the docker-compose.yml file content.

You can replace your docker-compose.yml file with commands lines though (as docker-compose is precisely to replace it).


In your case you can add the command to run to your Dockerfile as a default command (which isn't roughly the same as passing it to containers you start at runtime) :

CMD ["python", "jk/manage.py", "runserver", "0.0.0.0:8081"]

or pass this command directly in command line like the volume and port which should give something like :

docker run -d -v .:/code -p 8081:8080 yourimage python jk/manage.py runserver 0.0.0.0:8081

BUT

Keep in mind that Dockerfiles and docker-compose serve two whole different purposes.

  • Dockerfile are meant for image building, to define the steps to build your images.

  • docker-compose is a tool to start and orchestrate containers to build your applications (you can add some informations like the build context path or the name for the images you'd need, but not the Dockerfile content itself).

So asking to "convert a docker-compose.yml file into a Dockerfile" isn't really relevant.

That's more about converting a docker-compose.yml file into one (or several) command line(s) to start containers by hand.

The purpose of docker-compose is precisely to get rid of these command lines to make things simpler (it automates it).

also :

From the manage.py documentation:

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay.

Django's runserver included in the manage.py tool isn't meant for production.

You might want to consider using a WSGI server behind a proxy.

like image 61
vmonteco Avatar answered Oct 04 '22 19:10

vmonteco