-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.sh
More file actions
225 lines (177 loc) · 7.75 KB
/
docker.sh
File metadata and controls
225 lines (177 loc) · 7.75 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
Docker: contain include: operating system, version-python, package, ...: everything for a your program can be runned at a place orther, just run dockerfile
Docker: is completely independent of everything outside and it is like a separate independent computer, and when deleted, everything is lost.
Docker: equivalent to a virtual machine.
Docker: Can download image from the Docker repository.
'@': Reference operator
':': Operator to convert user to folder.
special: 'imageName:tag'
<type>: container, image, volume
Methology:
docker file -> docker image -> docker container
Create docker file:
file dockerfile: edit into file
Convert to docker image:
docker build -t <name>:<tag> <path/to/folder/contain/dockerfile>: -> docker image
Convert to docker container:
docker run <id/of/docker/image>: contain full env for your project
# Syntax base
Syntax:
container:
docker ps: print list docker container
docker ps -a: print list docker container stopping, starting
image:
docker images: print list docker image
docker images ls: print list docker image detail
active:
docker start <name container | id container> # start container created
docker stop <name container | id container> # stop container created
remove image:
# remove image repo collapse
# rmi: remove image
docker rmi <name image repo | id imagerepo>
# remove after stop conrainer
docker image remove <name image repo | id imagerepo>
# stop and remove image
docker image remove -f <name image repo | id imagerepo>
remove container:
# remove collapse
# rm: remove
docker rm <name container | id container>: remove container
# remove after stop conrainer
docker remove <name container | id container>: remove container
# stop and remove container
docker remove -f <name container | id container>: remove container
Build:
docker build -t <name>:<tag> <folder/contain/dockerfile>: create docker image
Ex: docker build -t getting-start .
Run:
docker run --name <name container> -dp <host> <name image>: run image to create container
# d: --detach - run in background
# p: --publish - host:container
docker run <name image>
update:
image:
docker build -t <name>:<tag> <dockerfile>: build again image
container:
docker run -dp <host> <name image>:<tag>: build again container
if error:
"
docker: Error response from daemon: driver failed programming external connectivity on endpoint laughing_burnell
(bb242b2ca4d67eba76e79474fb36bb5125708ebdabd7f45c8eaf16caaabde9dd): Bind for 127.0.0.1:3000 failed: port is already allocated.
"
Fix this error, you can remove the old contain
rename repo:
docker tag <name image> <new name follow account want push>
# <new name follow account want push>: username/env
share image:
docker push <username/nameenv>
login:
docker login -u <Username>
# Docker volume runs with data that remains saved when changed, and is not deleted when the container is deleted.
# Remove volume will cause everything to be lost
volume embedding data: (volume)
create:
docker volume create <name volume>: create a volume
embedding <container> into <volume>:
docker run -dp ... --mount type=volume,src=<name volume>,target=<path/to/repo/changes/that/need/saved> <name image>
EX: docker run -dp 127.0.0.1:3000:3000 --mount type=volume,src=todo-db,target=/etc/todos suzeai/myenv
view config:
<type>: container, image, volume
docker <type> inspect <type>: view config of the <type> (json)
view logs:
<type>: container, image, volume
docker logs -f <type> # view log of the <type> (json)
bind embedding core: (bind): to mount bind, you must have: "--mount type=bind,src="$(pwd)",target=/src"
<type="bind", src="filesystem local", target="folder contain filesystem">
docker run ... --mount type=bind,src="$(pwd)",target=/src <name image>: create container with core can changes
docker run -dp ... -w /app --mount type=bind,src="$(pwd)",target=/app <name image> <commandline implement into -w>
-w: working dir
--mount: connect folder working local
EX:
docker run -dp 127.0.0.1:3000:3000 --mount type=bind,src="$(pwd)",target=/src suzeai/myenv
docker run -it --mount type=bind,src="$(pwd)",target=/src ubuntu bash (container pulled)
docker run -dp 127.0.0.1:3000:3000 -w /app --mount type=bind,src="$(pwd)",target=/app node:18-alpine sh -c "yarn install && yarn run dev"
<commandline>: sh -c "yarn install && yarn run dev"
bind&volume:
# Both link to the local system and store.
# multiple mounts
docker run .... --mount --mount <name image> <commandline>
EX:
docker run -dp 127.0.0.1:3000:3000 -w /app \
--mount type=bind,src="$(pwd)",target=/app \
--mount type=volume,src=todo-db,target=/etc/todos \
suzeai/myenv \
sh -c "yarn install && yarn run dev"
exec docker container shell:
# When operating on a container shell using the terminal instead of Docker Desktop, we will enter the environment we previously created.
# Execute <command line> in the environment of <name container>
<env>: FROM node:18-alpine, ubuntu ...
docker exec -it <name container> <commend line>: move to the environment of the container.
EX:
docker exec -it pedantic_lehmann docker-entrypoint.sh
docker exec -it priceless_liskov bash
debug:
docker debug <type>
copy:
docker cp <localfile> <namecontainer>
Ex:
docker cp ./some_file CONTAINER:/work
# multi-container
network:
# created to volume
# the same volume
docker network create <name network>
start container node:
Ex Mysql:
# init container Mysql
docker run -d \
--network todo-app --network-alias mysql \
-v todo-mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=todos \
mysql:8.0
docker exec -it <mysql-container-id> mysql -u root -p
SHOW DATABASES;
exit # stop bash mysql
# create a orther container to connect container mysql
docker run -it --network todo-app nicolaka/netshoot
dig mysql
# use app getting started connect container mysql
docker run -dp 127.0.0.1:3000:3000 \
-w /app -v "$(pwd):/app" \
--network todo-app \
-e MYSQL_HOST=mysql \
-e MYSQL_USER=root \
-e MYSQL_PASSWORD=secret \
-e MYSQL_DB=todos \
node:18-alpine \
sh -c "yarn install && yarn run dev"
IP:
# check ip container
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <name container>
edit dockerfile:
COPY . . # copy all local file to container
# edit in file compose.yaml
compose:
docker compose up -d # -d: to run in background
# logs compose
docker compose logs -f
# remove compose and volumes
docker compose down --volumes
history:
docker image history <name image>:<tag> # render history image
env:
docker run -e <name>=<value> <image>
docker run --env-file <env_file> <image>
Appendix:
docker logs -f --tail 10 <name container>: show the logs of container when container is running with background
docker <option> inspect <name option>: view the config of the option (Option : container, image, volume, ... )
docker <option> --filter <name option>: find in the config match with filter view the config of the option (Option : container, image, volume, ... )
#docker compose: must exec in enviroment contain file .yaml
docker compose ls: list of project which is running
docker compose logs -f --tail 10: show log real-time in terminal
docker compose up -d: run many config - image, network, volume, container in the file .yaml
docker compose rm: only stop and remove contain in file .yaml
docker compose start: start all container in the file .yaml
docker compose stop: stop all container in the file .yaml
docker compose down: stop and remove all everything of project instead of components in file .yaml