Today we are using Docker-compose to quickly install a DJANGO+POSTGRESQL development environment.
Compose introduction
Compose positioning is "defining and running complex applications with Docker", formerly Fig, compatible with fig template files.
Dockerfile lets users manage a single application container, while Compose allows the user to define a set of associated application containers (called One, Project) in a template (YAML format) project
, such as a WEB service container plus a backend database service container.
The project was written by Python and actually invokes the API provided by Docker.
Install Docker-compose Please poke here, after the installation, let's start together!
I. Define project components 1.1 Create an empty project directory
[Email protected] sparks]# mkdir django_test
1.2 Create a file named Dockerfile in your project directory
Docker can automatically build images with Dockerfile content. Dockerfile is a text file that contains all the commands to create a mirror, and the Docker Build command allows you to build an image based on the contents of Dockerfile, introducing the basic syntactic structure of the Dockerfile before describing how to build it.
To learn more about the official documentation
1.3 Add the following content to Dockerfile
From Python:3 ENV pythonunbuffered 1 RUN mkdir/code workdir/code ADD requirements.txt/code/run pip Install-r requ Irements.txt ADD. /code/
This dockerfile file starts with the Python3 base image, creates and moves the working directory to the Code directory, adds requirements.txt to the code directory, and installs the dependent packages in the file with the PIP command.
1.4 Create and add the following to Requirements.txt in your project directory
django>=1.8,<2.0 PSYCOPG2
1.5 Create and add the following to DOCKER-COMPOSE.YML in your project directory
Version: ' 3 ' Services: DB: image:postgres Web: build:. Command:python3 manage.py runserver 0.0.0.0:8000 volumes: -.:/ Code ports: -"8000:8000" depends_on: -DB
This file defines two services, database (DB) services, and Web services, and to learn more click here
Ii. Creating a Django Project
In this step, create the image with the file created in the previous step and use this image to create the Django project
2.1 Go to the project root directory and run the following command to create a Django project
sudo docker-compose run web django-admin.py startproject composeexample.
Here django-admin.py Startproject Composeexample needs to use the image and configuration of the Web service, but the web image does not exist at the moment, so compose automatically finds and creates the service in the current directory.
See Docker-compose.yml in Build:. This line.
Once the Web service image is created, run django-admin.py startproject in that container to create the Django project
2.2 After the above command is finished, review your project directory file
$ ls-l drwxr-xr-x 2 root root composeexample-rw-rw-r--1 user user docker-compose.yml-rw-rw-r--1 user User Dockerfile-rwxr-xr-x 1 root root manage.py-rw-rw-r--1 user user requirements.txt
If you are running the docker,manage.py file on Linux is root created, because running inside the container is the root user, you can change the permissions by following the command
sudo chown-r $USER: $USER.
Third, connect the database
In this section, we will create the database and connect the Django
3.1 In your project directory, edit the composeexample/settings.py file 3.2 to databases = ... Replace with the following:
DATABASES = {' default ': { ' ENGINE ': ' Django.db.backends.postgresql ', ' NAME ': ' Postgres ', ' USER ': ' Postgres ', ' HOST ': ' db ', ' PORT ': 5432, }}
3.3 Run the command under your project's root directory
$ docker-compose updjangosample_db_1 is up-to-datecreating djangosample_web_1 ... Creating djangosample_web_1 ... doneattaching to djangosample_db_1, djangosample_web_1db_1 | The files belonging to this database system is owned by user "Postgres". Db_1 | This user must also own the server process.db_1 |db_1 | The database cluster is initialized with locale "En_us.utf8". Db_1 | The default database encoding have accordingly been set to "UTF8". Db_1 | The default text search configuration would be set to "中文版" .... web_1 | May, 2017-21:44:49web_1 | Django version 1.11.1, using Settings ' composeexample.settings ' web_1 | Starting development Server at Http://0.0.0.0:8000/web_1 | Quit the server with Control-c.
Here, your Django application is already running on the 8000 port of the Docker host, and you can see the Django Welcome screen using the browser input http://localhost:8000.
3.4 Viewing a running container
On another terminal, you can view the running container through the Docker PS command
$ docker pscontainer ID IMAGE COMMAND CREATED STATUS PORTS namesdef85eff5f51 django_ Web "Python3 manage.py ..." minutes ago up 9 minutes 0.0.0.0:8000->8000/tcp django_web_ 1678ce61c79cc postgres "Docker-entrypoint ..." minutes ago up 9 minutes 5432/tcp Django_db_1
A Web container, a Postgre container is running in the background, so we are done!
Docker-compose Combat--django+postgresql