Установка
sudo add-apt-repository ppa:docker-maint/testing
sudo apt-get update
sudo apt-get install docker.io
sudo pip install docker-compose
Настройка
Подробно о настройке Quickstart Guide: Compose and Django
Создать Dockerfile файл с содержимым:
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
Создать файл requirements.txt:
Django
psycopg2
Создать файл docker-compose.yml:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db
Запуск
docker-compose run web django-admin.py startproject composeexample .
Ошибка
---> Running in 4d2a320bff11
Requirement already satisfied (use --upgrade to upgrade): pip in /usr/local/lib/python3.4/site-packages (from -r requirements.txt (line 1))
Collecting Django (from -r requirements.txt (line 2))
Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/django/
Could not find a version that satisfies the requirement Django (from -r requirements.txt (line 2)) (from versions: )
No matching distribution found for Django (from -r requirements.txt (line 2))
Service 'web' failed to build: The command "/bin/sh -c pip install -r requirements.txt" returned a non-zero code: 1
Мне помогло, первый вариант.
➜ docker_hello_django docker-compose run web django-admin.py startproject composeexample .
Building web...
Step 0 : FROM python:3.4
---> 545faa5764ae
Step 1 : ENV PYTHONUNBUFFERED 1
---> Using cache
---> 33d6f340b1cf
Step 2 : RUN mkdir /code
---> Using cache
---> 1ec39055f34e
Step 3 : WORKDIR /code
---> Using cache
---> 20aab30864ca
Step 4 : ADD requirements.txt /code/
---> Using cache
---> 9ea2b493fe7e
Step 5 : RUN pip install -r requirements.txt
---> Running in 6be7c2161299
Requirement already satisfied (use --upgrade to upgrade): pip in /usr/local/lib/python3.4/site-packages (from -r requirements.txt (line 1))
Collecting Django (from -r requirements.txt (line 2))
Downloading Django-1.8.4-py2.py3-none-any.whl (6.2MB)
Collecting psycopg2 (from -r requirements.txt (line 3))
Downloading psycopg2-2.6.1.tar.gz (371kB)
Installing collected packages: Django, psycopg2
Running setup.py install for psycopg2
Successfully installed Django-1.8.4 psycopg2-2.6.1
---> e73ddf36d3d3
Removing intermediate container 6be7c2161299
Step 6 : ADD . /code/
---> 226ccc34ee37
Removing intermediate container 09644c2e1c94
Successfully built 226ccc34ee37
CommandError: /code/manage.py already exists, overlaying a project or app into an existing directory won't replace conflicting files
Это значит, что файлы уже есть в папке откуда выполняется проект.
docker-compose up
''' ➜ docker_hello_django docker-compose up dockerhellodjango_db_1 is up-to-date Starting dockerhellodjango_web_1... Attaching to dockerhellodjango_db_1, dockerhellodjango_web_1 db_1 | The files belonging to this database system will be owned by user "postgres". db_1 | This user must also own the server process. db_1 | db_1 | The database cluster will be initialized with locale "en_US.utf8". db_1 | The default database encoding has accordingly been set to "UTF8". db_1 | The default text search configuration will be set to "english". db_1 | db_1 | Data page checksums are disabled. db_1 | db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok db_1 | creating subdirectories ... ok db_1 | selecting default max_connections ... 100 db_1 | selecting default shared_buffers ... 128MB db_1 | selecting dynamic shared memory implementation ... posix db_1 | creating configuration files ... ok db_1 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok db_1 | initializing pg_authid ... ok db_1 | initializing dependencies ... ok db_1 | creating system views ... ok db_1 | loading system objects' descriptions ... ok db_1 | creating collations ... ok db_1 | creating conversions ... ok db_1 | creating dictionaries ... ok db_1 | setting privileges on built-in objects ... ok db_1 | creating information schema ... ok db_1 | loading PL/pgSQL server-side language ... ok db_1 | vacuuming database template1 ... ok db_1 | copying template1 to template0 ... ok db_1 | copying template1 to postgres ... ok db_1 | syncing data to disk ... ok db_1 | db_1 | Success. You can now start the database server using: db_1 | db_1 | postgres -D /var/lib/postgresql/data db_1 | or db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start db_1 | db_1 | db_1 | WARNING: enabling "trust" authentication for local connections db_1 | You can change this by editing pg_hba.conf or using the option -A, or db_1 | --auth-local and --auth-host, the next time you run initdb. db_1 | ******* db_1 | WARNING: No password has been set for the database. db_1 | This will allow anyone with access to the db_1 | Postgres port to access your database. In db_1 | Docker's default configuration, this is db_1 | effectively any other container on the same db_1 | system. db_1 | db_1 | Use "-e POSTGRES_PASSWORD=password" to set db_1 | it in "docker run". db_1 | ******* db_1 | PostgreSQL init process in progress... db_1 | LOG: database system was shut down at 2015-08-18 15:56:44 UTC db_1 | LOG: MultiXact member wraparound protections are now enabled db_1 | LOG: database system is ready to accept connections db_1 | LOG: autovacuum launcher started db_1 | ALTER ROLE db_1 | db_1 | db_1 | /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* db_1 | db_1 | LOG: received smart shutdown request db_1 | LOG: autovacuum launcher shutting down db_1 | LOG: shutting down db_1 | LOG: database system is shut down db_1 | db_1 | PostgreSQL init process complete; ready for start up. db_1 | db_1 | LOG: database system was shut down at 2015-08-18 15:56:48 UTC db_1 | LOG: MultiXact member wraparound protections are now enabled db_1 | LOG: database system is ready to accept connections db_1 | LOG: autovacuum launcher started db_1 | LOG: received smart shutdown request db_1 | LOG: autovacuum launcher shutting down db_1 | LOG: shutting down db_1 | LOG: database system is shut down db_1 | LOG: database system was shut down at 2015-08-19 01:48:03 UTC db_1 | LOG: MultiXact member wraparound protections are now enabled db_1 | LOG: autovacuum launcher started db_1 | LOG: database system is ready to accept connections db_1 | LOG: received smart shutdown request db_1 | LOG: autovacuum launcher shutting down db_1 | LOG: shutting down db_1 | LOG: database system is shut down db_1 | LOG: database system was shut down at 2015-08-19 01:50:33 UTC db_1 | LOG: MultiXact member wraparound protections are now enabled db_1 | LOG: database system is ready to accept connections db_1 | LOG: autovacuum launcher started web_1 | Performing system checks... web_1 | web_1 | System check identified no issues (0 silenced). web_1 | web_1 | You have unapplied migrations; your app may not work properly until they are applied. web_1 | Run 'python manage.py migrate' to apply them. web_1 | August 19, 2015 - 03:09:40 web_1 | Django version 1.8.4, using settings 'composeexample.settings' web_1 | Starting development server at http://0.0.0.0:8000/ web_1 | Quit the server with CONTROL-C.
'''
подключиться к базе данных postgres
➜ docker_hello_django docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcfe8da7a44a dockerhellodjango_web "python manage.py ru 35 minutes ago Up 2 minutes 0.0.0.0:8000->8000/tcp dockerhellodjango_web_1
0c5bb163a641 postgres "/docker-entrypoint. 11 hours ago Up 2 minutes 0.0.0.0:32772->5432/tcp dockerhellodjango_db_1
➜ docker_hello_django psql -h localhost -p 32772 -l -U postgres
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
➜ docker_hello_django psql -h localhost -p 32772 -d postgres -U postgres --password
Password for user postgres:
psql (9.3.9, server 9.4.4)
WARNING: psql major version 9.3, server major version 9.4.
Some psql features might not work.
Type "help" for help.
postgres=#
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
Запуск коменды в контейнере
docker-compose run web pip install -r requirements.txt