-
Notifications
You must be signed in to change notification settings - Fork 1
/
mynote.txt
149 lines (95 loc) · 6.2 KB
/
mynote.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
for using docker first of all you need to create a Dockerfile in your root directory "Dockerfile"
the file need to be in Captal D and dosn't have any extention at the end.
The first line of you docker file is an image that you gonna inherate your docker from.
with docker you can build images on top of other images.
FROM ---> the image taht you wnna get it
MAINTAINER --> it's not necessary but you can put you name or comapnay name
ENV PYTHONUNBUFFERED 1 --> what it's dose ? it's tell python that run in unbuffered mode which is recommendated
when run python with docker container. the reason is that it all python to buffer the output and print it directly
and this avoid some complecation.
COPY ./requirements.txt /requirements.txt ---> what it's dose ? it's copy the ./requirements.txt or or file
in to the docker image /requirements.txt
RUN mkdir /app ---> it will create a "app" folder in our docker image
WORKDIR /app ---> then it switches to our "app" as a default directory. it means that any application we run when
we using our dodcker it will start from here.
COPY ./app /app ---> then it will copy the aplication from our "./app" forlder in local machine to the image
RUN adduser -D user ---> add user that runing application only so not for havaing home directory ---> it is because of
some security puposes. if you don't do this then the image run our application in the root account which is not
recommendate, that means if somebody compromise our application they can then have root access to the whole iamge.
USER user ---> switch to our user we just create
then you have to go and create your ./requirements.txt and put this thing inside it:
Django>=3.1.3,<3.2.0
djangorestframework>=3.12.2,<3.13.0
Then create a folder in your project by the name of "app"
then build your dokcer image:
docker build .
next is "Docker compose":
docker compose is a tool that allow us to run docker images easly from our project location
also allow us to easly manage diffrent services that make our project, which one service might be python and
another might be database.
aftehr that create a file in your root directory by the name of docker-compose.yml
and put the following content
version: "3"
services:
app: ---> which is name of our service
build:
context: .
ports:
- "8000:8000" ---> to map our port 8000 from our host to our image
volumes: ---> it's for update, when we have some change in our code so by this command we can update.
- ./app: /app
command: > ---> command is somethign that gonna run our application.
sh -c "python3 manage.py runserver 0.0.0.0:8000"
sudo docker-compose build
then go ahead and run this command it means that run that app or services that you wanna run it.
docker-compose run app sh -c "django-admin.py startproject app ."
here is some instraction from coding for entreprenurs
https://www.codingforentrepreneurs.com/blog/django-on-docker-a-simple-introduction
if we wanna add postgres instead of default database so put this data in your docker-compose file
db:
image: postgres:10-alpine
{% comment %} then setup the envionment varable {% endcomment %}
environment:
- POSTGRES_DB = app
- POSTGRES_USER = postgres
- POSTGRES_PASSWORD = supersecritpassword
also make sure that you have to specify environment varaible in your docker-compose which in this
project i wanna do it after command
also be careful about your password and make it inside environment variable
next i wanna create a depend service --> which in here i wanna add my "app" to depend on "db"
there is to reasone that i wanna do it like that:
1. our database gonna run before the app
2. the Database gonna be avalable to the network when you use HOST_NAME=db in your app.
you can add multipal dependencies in your application if you have more.
after that you have to add psycopg2 in your ./requirements.txt
also don't forget to add RUN command like the following line in django-compose
RUN apk --update --no-cache postgres-client --> what it dose it's use a package manager wiche come with alpine
which 'apk' is the name of our package manager
the 'update' means update before we added
'no-cache' don't install any extra files. the reason is taht we gonna minimize our extrafiles.
then create a temprary dependencies requirements
then delete the temprary dependancies make sure that you are runing this command after pip instal /requirements.txt
{% comment %} for testing you can do this line {% endcomment %}
docker-compose run app sh -c "python3 manage.py test"
{% comment %} if you wanna delete the docker-compose command and don't repeat every time
so do the following command.
{% endcomment %}
dockker-compose run --rm app sh -c "python manage.py startapp users"
Mocking:
mocking is when you change the behavior of dependancies of the code that you tested.
We use mocking to avoid any unintended side effects and also to isolate to spesific peace of code
you wanna test.
i.e: imagine that you wanna test a function that send an email.
remember that never write a test that depend on external services. because you can't granty that
service will be available at the point that you gonna run the tested.
you can use mocking to avoide sending an actual email.
it's avoid sending email just check that email function is working correctly
for installing and using 'pillow' library for image modification in django you have to isntall
some dependancies in 'Dockerfile'. do it in---> RUN apk add which is = 'jpeg-dev', 'musl-dev', 'zlib', 'zlib-dev'
RUN mkdir -p /vol/web/media ---> which is making directory for media files after installing pillow
RUN mkdir -p /vol/web/static ---> which is making directory for static
RUN chown -R user:user /vol/ ---> it is for ownership of /vol/ directory but amke sure that before
you are going to run this command you have to add the user with RUN adduser -D user
RUN chmod -R 755 /vol/web ---> what it means that user can do everythign or owner can do everything
the rest can read an execute from directory
after you have done your wok wo build your docker ---> docker-compose build