Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2fca2f4

Browse files
Added Docker notes
1 parent 22f035b commit 2fca2f4

File tree

7 files changed

+811
-0
lines changed

7 files changed

+811
-0
lines changed

‎docker/Dockerfile‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Base Image
2+
FROM ubuntu
3+
4+
# Install nodejs
5+
RUN apt-get update
6+
RUN apt-get install -y curl
7+
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -
8+
RUN apt-get update
9+
RUN apt-get install -y nodejs
10+
11+
# Copy the files
12+
COPY package.json package.json
13+
COPY package-lock.json package-lock.json
14+
COPY main.js main.js
15+
16+
# Install the dependencies
17+
RUN npm install
18+
19+
# Run the app
20+
ENTRYPOINT [ "node", "main.js" ]
21+
22+
# Build the image

‎docker/README.md‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Docker
2+
3+
### Problem statement which docker is solving
4+
1) Same environment replica
5+
- Provide same environmet for each and every services and bind them in one container.
6+
- To run a project in a very large scale team size it is getting very hard to setup same environment across all team member machines in different OS's also, with the help of Docker we can easily overcome this issue.
7+
- Some services are OS specifics like some service wil run in windows machines only but we needed same in MacOS also, by the help of Docker we can acheive this also.
8+
2) Setup environment in cloud for every instance
9+
- It is getting very tough to setup same environment to deploy our services on cloud
10+
- If we are using serverless then in each and every instance we have to replicate same environment
11+
- We can solve this problem with the help of Docker.
12+
13+
### Docker Container
14+
1) Container
15+
- Inside container we will create all our services and install all our required software or packages.
16+
- like we can create **Container** with below services
17+
- OS:Linux
18+
- Tools: Node, MongoDB, ReactJS, SQL and many more
19+
- And we can use this container anywhere by running our Docker container
20+
2) Installation of Docekr CLI and Desktop
21+
- Download [Docker](https://www.docker.com/products/docker-desktop/) and install
22+
- It will download docker and provide **Daemon** and **Docker Desktop**
23+
- To run Ubuntu container in our machine run this command for running ubunut in container
24+
- ```docker run -it ubuntu ```
25+
- it will install ubuntu image in you local docker container, after installing it will available in Docker desktop
26+
- it will download from [hub.docker.com](https://hub.docker.com)
27+
- It will provide a isolate container, where we can download and run anything it will not affect our physical machine.
28+
- **Docker images behave like a OS and container run this images which are isolated from each container, can be created multiple container with single image**
29+
30+
3) Port Mapping
31+
- As Docker container is Isolated from our physical machine so it will not be accessible from physical machine browser
32+
- let say node env is running in PORT:8000, so it will not be available in chrome browser, it will throw not found error.
33+
- So for solving this we can expose container PORT
34+
- To expose have to run below command in physical machine terminal
35+
- ```docker run -it -p <physical_machine_port>:<container_port> <image_name>```
36+
4) Environment variable
37+
- We can pass env variable also when we are running image in host server
38+
- ```docker run -it -p <physical_machine_port>:<container_port> -e key=value -e key=value <image_name>```
39+
40+
5) Dockerization
41+
- Dockerize node js server
42+
- create one file with name **Dockerfile** without any extension
43+
- check all config in Dockerfile from above dir
44+
- after adding configuration build the docker image from below command
45+
- ``` docker build -t <our_image_name> <docker_file path '.'>```
46+
- -t means tag, in <docker_file_path> press **dot(.)** if it in parent dir
47+
- After building it will availbale in Image section of Docker Desktop
48+
- after running and mapping port, we will be able to run in out host machine
49+
- Caching the layer
50+
- Every command in Dockerfile it is in cache, so add command according to it
51+
- Publish to Docker Hub
52+
- Create accoung on docker hub and create image from portal
53+
- build our docker image with the same name as we created on docker portal
54+
- And Run below command
55+
- ``` docker push <image_name>```
56+
- If it is throwing error 'denied:requested access to the resource is denied' error, then we have to login, for login run below command
57+
- ```docker login``` and provide credentials
58+
- after login push docker image, it will available in docker hub portal after push
59+
6) Docker Compose
60+
- Services
61+
- Sometimes we have to use multiple container or images for running a project
62+
- One way is we can run each caontainer command one by one and use it, but it will not preferable
63+
- (Preferred way): create **docker-compose.yml** file and add config file inside this, we can setup, create and destroy container from this
64+
- Check the above ***docker-compose.yml*** file
65+
- After adding config run below command
66+
- ```sudo docker compose up```
67+
- It will up and running all the services which are in compose file
68+
- It will create node and run all container inside this one, it can be available in Docker Desktop
69+
- to stop compose service run below command
70+
- ```sudo docker compose down```
71+
- Port Mapping
72+
- Env Variable
73+
74+
75+
### Docker Commands
76+
- ```docker run -it <image_name> ``` --> It will pull docker image from docker hub repo
77+
- ```docker container ls ``` --> Display running container
78+
- ```docker container ls -a``` --> Display all container
79+
- ```docker start <contaienr_name> ```
80+
- ```docker stop <contaienr_name> ```
81+
- ```docker exec <contaienr_name> ls``` --> Just for dispaly dir in container
82+
- ```docker exec -it <contaienr_name> bash``` --> attach terminal in container
83+
- ```docker images ```
84+
85+
86+

‎docker/docker-compose.yml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '3.8'
2+
3+
services:
4+
postgres:
5+
image: postgres:12.2 # https://hub.docker.com/_/postgres
6+
restart: always
7+
environment:
8+
POSTGRES_USER: postgres
9+
POSTGRES_PASSWORD: password
10+
POSTGRES_DB: review
11+
ports:
12+
- "5432:5432"
13+
14+
redis:
15+
image: redis # https://hub.docker.com/_/redis
16+
ports:
17+
- "6379:6379"

‎docker/docker.txt‎

Whitespace-only changes.

‎docker/main.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const express = require('express')
2+
3+
const app = express()
4+
5+
const port = process.env.PORT || 8000
6+
7+
app.get('/', (req, res) => {
8+
res.send('Hello There from Docker')
9+
})
10+
11+
app.listen(port, () => {
12+
console.log(`Server is running on port ${port}`)
13+
}
14+
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /