-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocker.txt
91 lines (70 loc) · 3.86 KB
/
Docker.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
# Docker -
> Benefits : Consistency, Isolation, Portability, Microservices
> Docker Image VS Docker Container :
Docker image is the blueprint (Dockerfile and its associated files), and the Docker container is the actual running instance of that image, where your application runs. You can have multiple containers based on the same image, just like you can have multiple servings of the same dish made from the same recipe.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Eg. Dockerfile - script that Docker uses to create an image
( designating base image, workdir, env. vars details, files to be copied, commands to execute )
FROM node:18-alpine :- specifies the base image to be used and in this instance, the image we are using is an Alpine-based image of Node.js version 18
WORKDIR /app :- sets the working directory inside the Docker container to /app where any subsequent command will be executed from this directory
COPY . . :- copies the current directory contents on the host machine to the current directory set inside the container (/app)
RUN npm install :- installs all the libraries and dependencies defined from the package.json file
CMD [“node”, “src/index.js”] :- runs the index.js file located in the src directory
EXPOSE 3000 :- informs Docker that the container listens on port 3000 at runtime
---> NOTE: RUN is used during the image build process to perform setup and configuration tasks,
while CMD is used to specify the default command to run when a container is started from the image
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Building the docker image
> docker build -t <my_docker_image> . (t-tag, . => Dockerfile is in current directory)
## List all docker image
> docker images
## Run docker container
> docker run -dp 127.0.0.1:3000:3000 <my_docker_image>
(-d : detach (run container in background)
-p : port mapping (host port:container port))
## To view docker containers
> docker ps
## To stop & remove a docker container
> docker stop <container_id>
> docker rm <container_id>
---------------------------------------------------------------------------------------------------------------------
# Docker Compose - Docker Compose is a tool that allows developers to define and share multi-container applications.
|- Benefits
|- Simplified config (uses yaml file)
|- Efficien dev. workflow
|- Dependency mgt.
Key Syntax of a docker-compose.yml :-
1. version - Specifies the version of the Docker Compose file
2. services - Defines each service (Docker Container) that should be run in your application
3. build - Provides build instructions if you’re not using a pre-existing image from Docker Hub
4. image - Specifies the Docker image to use for a service (Docker Container)
5. ports - Maps ports from the host to the service
6. volumes - Defines any storage volumes you would like to attach to the service
7. depends_on - Define the dependencies between services in your application
8. networks - Specifies which networks the service is connected to
----------------------------
Eg. docker-compose.yml
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
depends_on:
- redis
networks:
- webnet
redis:
image: redis:alpine
networks:
- webnet
networks:
webnet:
----------------------------
## To run application
> docker compose up --detach
## To stop application
> docker compose down
## To execute commands inside a container (Eg. To use mongodb shell in mongodb container)
> docker exec -it <containerID> mongosh -u "root" -p "example" (it-interactive cmd in container, u-username, p-pwd)
> Inside shell, > db.users.find().pretty() (to display all users)