Docker-Compose
- DC makes it easier to configure and run applications made up of
multiple containers - DC is a very handy tool to quickly get docker environment up and running.
- It
uses yamlfiles to store the configuration of all the containers - We can create some containers like:
web app, postgres and redis- and can establish an
internal connection
- and can establish an
Installation
- sudo apt-get install docker-compose
- Check
- docker-compose –version
- docker-compose -v
Commands
# This will not build the image if it's already exists
docker-compose up
# Force to build the image
docker-compose build
docker-compose down
docker-compose config
# stops all the running containers without removing them.
docker compose stop
docker container stop <container id>
# removes all the containers.
docker compose rm
Mini project - Mongodb and Node
Initialize node and install mongodb
npm init -y
npm install mongoose
# package.json --> Update
"scripts": {
"start": "node index.js"
}
Create index.js and Check connection: npm start
# index.js
const mongoose = require("mongoose");
const DATABASE = "mongodb://localhost:27017/test";
mongoose.connect(DATABASE)
.then(() => {
console.log("DB connected");
})
.catch(()=>{
console.log("ERROR in DB connection");
});
Create .dockerignore file –> to Ignore some files/folders
# .dockerignore --> file
node_modules
Create Dockerfile and TEST connection
FROM node:alpine
WORKDIR /var/app
COPY ./ ./
RUN npm install
CMD ["npm", "start"]
docker build -t node-test .
docker run node-test
# ERROR in DB connection --> Coz DB setup is pending
Create docker-compose.yml
# docker-compose.yml
version: "3"
services:
mymongo:
image: "mongo"
mynode:
build: .
ports:
- "8000:8000"
# mymongo --> container name
# mongo --> image name
Build and Run docker-compose
# Update index.js
const DATABASE = "mongodb://mymongo:27017/test";
docker-compose build
docker-compose up
# OR
docker-compose up --build
DjRest-Adv
# Dockerfile
FROM python:3.7-alpine
MAINTAINER Hitman
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client jpeg-dev
RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps
RUN mkdir /app
WORKDIR /app
COPY ./app /app
RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
RUN adduser -D user
RUN chown -R user:user /vol/
RUN chmod -R 755 /vol/web
USER user
# docker-compose.yml
version: "3"
services:
app:
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=app
- DB_USER=postgres
- DB_PASS=supersecretpassword
depends_on:
- db
db:
image: postgres:10-alpine
environment:
- POSTGRES_DB=app
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=supersecretpassword
# django app inside app folder
# Run
docker-compose up