- Create a network
docker network create goals-net
- Run MongoDB Container
docker run -d --rm -v mongo-vol:/data/db --name mongodb -e MONGO_INITDB_ROOT_USERNAME=anand -e MONGO_INITDB_ROOT_PASSWORD=secret --network goals-net mongo:7.0
docker run --name mongodb \
-e MONGO_INITDB_ROOT_USERNAME=anand \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
-v data:/data/db \
--rm \
-d \
--network goals-net \
mongo
-
Build Node API Image (backend)
- Navigate to
backend
folder whereDockerfile
exists and build the image
- Navigate to
docker build . -t actionanand/docker_multi-container_node
- Run Node API Container
docker run -d --rm --name docker_multi-container_node -e MONGODB_USERNAME=anand -e MONGODB_PASSWORD=secret -v logs:/app/logs -v /app/node_modules -v "D:\AR_extra\rnd\docker\docker_multi-container\backend:/app:ro" --network goals-net -p 3002:80 actionanand/docker_multi-container_node
below command for mac, linux and wsl2
docker run -d --rm --name docker_multi-container_node -e MONGODB_USERNAME=anand -e MONGODB_PASSWORD=secret -v logs:/app/logs -v /app/node_modules -v $(pwd)/backend:/app:ro --network goals-net -p 3002:80 actionanand/docker_multi-container_node
docker run --name docker_multi-container_node \
-e MONGODB_USERNAME=anand \
-e MONGODB_PASSWORD=secret \
-v logs:/app/logs \
-v /Users/actionanand/development/docker/docker_multi-container/backend:/app:ro \
-v /app/node_modules \
--rm \
-d \
--network goals-net \
-p 3002:80 \
actionanand/docker_multi-container_node
-
Build React SPA Image
- Navigate to
frontend
folder whereDockerfile
exists and build the image
- Navigate to
docker build . -t actionanand/docker_multi-container_react
- Run React SPA Container
docker run -d --rm -it --name docker_multi-container_react -p 3001:3000 -v "D:\AR_extra\rnd\docker\docker_multi-container\frontend\src:/app/src:ro" actionanand/docker_multi-container_react
below command for mac, linux and wsl2
docker run -d --rm -it --name docker_multi-container_react -p 3001:3000 -v $(pwd)/frontend/src:/app/src:ro actionanand/docker_multi-container_react
docker run --name docker_multi-container_react \
-v /Users/actionanand/development/docker/docker_multi-container/frontend/src:/app/src:ro \
--rm \
-d \
-p 3001:3000 \
-it \
actionanand/docker_multi-container_react
-it
should be added for frontend apps. They're for interactive tty. If not added, process will exit.
-
You can visit at
http://localhost:3001/
to view the app. -
Stopping all Containers
docker stop mongodb docker_multi-container_node docker_multi-container_react
Docker Compose is an additional tool, offered by the Docker ecosystem, which helps with orchestration / management of multiple Containers. It can also be used for single Containers to simplify building and launching.
Consider this example:
docker network create shop
docker build -t shop-node .
docker run -v logs:/app/logs --network shop --name shope-web shop-node
docker build -t shop-database
docker run -v data:/data/db --network shop --name shop-db shop-database
This is a very simple (made-up) example - yet you got quite a lot of commands to execute and memorize to bring up all Containers required by this application.
And you have to run (most of) these commands whenever you change something in your code or you need to bring up your Containers again for some other reason.
With Docker Compose, this gets much easier.
You can put your Container configuration into a docker-compose.yaml
or docker-compose.yml
file and then use just one command to bring up the entire environment: docker-compose up
.
A docker-compose.yaml
file looks like this:
version: "3.8" # version of the Docker Compose spec which is being used
services: # "Services" are in the end the Containers that your app needs
web:
build: # Define the path to your Dockerfile for the image of this container
context: .
dockerfile: Dockerfile-web
volumes: # Define any required volumes / bind mounts
- logs:/app/logs # will create volume as `docker_multi-container_logs`
db: # will create container name as `docker_multi-container_db_1`
build:
context: ./db
dockerfile: Dockerfile-web
container_name: containerName # custom name
volumes:
- data:/data/db
environment:
- MONGO_INITDB_ROOT_PASSWORD=secret
- MONGO_INITDB_ROOT_USERNAME=anand
networks:
- networkName # automatically ceated network name will be `docker_multi-container_default`, if not mentioned here
volumes:
data: # named valume should be declared at the end separately
logs:
docker compose will automatically create network
for all the services whichever defined under one file. We can also manually create if needed the deferent one.
You can conveniently edit this file at any time and you just have a short, simple command which you can use to bring up your Containers:
docker-compose up
docker-compose up -d
You can find the full (possibly intimidating - you'll only need a small set of the available options though) list of configurations here
Important to keep in mind: When using Docker Compose, you automatically get a Network for all your Containers - so you don't need to add your own Network unless you need multiple Networks!
There are two key commands:
-
docker-compose up
: Start all containers / services mentioned in the Docker Compose file-d
: Start in detached mode--build
: Force Docker Compose to re-evaluate / rebuild all images (otherwise, it only does that if an image is missing. That means it'll serve the cached image only)
-
docker-compose build
is used to just build the docker image -
docker-compose down
: Stop and remove all containers / services-v
: Remove all Volumes used for the Containers - otherwise they stay around, even if the Containers are removed
So, to remove all generated named volums along with container, below command will help:
docker-compose down -v
Of course, there are more commands. We see more commands in other sections (e.g. the "Utility Containers" and "Laravel Demo" sections) but you can of course also already dive into the official command reference
The displayed localhost port
3000
is to access it inside the docker. This port will be exposed at port3001
for our host machine. Please refer thedocker-compose.yaml
file for more details.
Point your browser to http://localhost:3001/