Skip to content

Commit 7834f23

Browse files
authored
How To Create a mern app with Docker
1 parent 06e5aa4 commit 7834f23

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

MERNWITHDOCKER/README.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## Building Images For A MERN App thanks to Docker
2+
1. Image Build 1(Client):
3+
```bash
4+
docker build -t "react-app" ./client/
5+
```
6+
2. Image Build 2(Server)
7+
```bash
8+
docker build -t "api-server" ./server/
9+
```
10+
11+
3. I create a docker compose file
12+
13+
1. I specify the version of the docker compose api I want to use
14+
15+
2. I define a set of services I want running on my app:
16+
- react-app: client
17+
- express-api: server
18+
- mongodb: database
19+
20+
3. I supply the image tags for each of the necessary Services I want running on my container
21+
22+
23+
4. For MongoDB I use the publicly available docker image from docker hub
24+
25+
4. In order to communicate with other services I need to setup access to proper ports for each
26+
- Remember to setup different ports for EACH service
27+
28+
5. For the react container I add the standard input option to true to keep the container alive and listening for requests after start
29+
30+
6. Because the express server needs to connect to the MongoDB service I say it depends on it. This ensures
31+
the containers start in a correct order
32+
33+
7. We can have one application running on one network but if we want isolation we can run multiple applications on different networks
34+
35+
8. I must define the network explicitly. Mern-app is what I called my network uses the default driver
36+
37+
9. I add all the services to the network
38+
39+
10. The services will talk to one another, while providing isolation from other docker containers that run on the same host
40+
41+
11. I define a docker volume to enable persistence of db data across container resources, which is mounted into the mongodb container
42+
43+
```yml
44+
version: "3"
45+
services:
46+
react-app:
47+
image: react-app
48+
stdin_open: true
49+
ports:
50+
- "3000:3000"
51+
networks:
52+
- mern-app
53+
api-server:
54+
image: api-server
55+
ports:
56+
- "5000:5000"
57+
depends_on:
58+
- mongo
59+
networks:
60+
- mern-app
61+
mongo:
62+
image: mongo:3.6.19-xenial
63+
ports:
64+
- "27017: 27017"
65+
networks:
66+
- mern-app
67+
volumes:
68+
- mongo-data:/data/db
69+
networks:
70+
mern-app:
71+
driver: bridge
72+
volumes:
73+
mongo-data:
74+
driver: local
75+
```
76+
77+
12. To start the application I run the command:
78+
```bash
79+
docker-compose up
80+
```
81+
This will start all three containers, create and attach the network and volume resources
82+
83+
13. To see if the containers are running I run the command:
84+
```bash
85+
docker ps
86+
```

docker-compose.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: "3"
2+
services:
3+
react-app:
4+
image: react-app
5+
stdin_open: true
6+
ports:
7+
- "3000:3000"
8+
networks:
9+
- mern-app
10+
api-server:
11+
image: api-server
12+
ports:
13+
- "5000:5000"
14+
networks:
15+
- mern-app
16+
depends_on:
17+
- mongo
18+
mongo:
19+
image: mongo:3.6.19-xenial
20+
ports:
21+
- "27017:27017"
22+
networks:
23+
- mern-app
24+
volumes:
25+
- mongo-data:/data/db
26+
networks:
27+
mern-app:
28+
driver: bridge
29+
volumes:
30+
mongo-data:
31+
driver: bridge

0 commit comments

Comments
 (0)