Docker Learning Note (iv)

E.Y.
3 min readAug 8, 2020
Photo by Derek Oyen on Unsplash

This is a series blogs about Docker and Kubernetes — This is the 4th blog following up to my last one.

In last blog, we will focus on Docker compose file.

Docker compose acts like a CLI tool that helps to start up multiple docker containers at the same time; it helps to specify what images are required, what ports they need to expose, and so on. Common commands exclude:

- docker run my_image
docker-compose up
- docker build. && docker run myimage
docker-compose up --build
$docker-compose up - launch in the background
docker-compose up -d
- stop contianers
docker-compose down
- look for container status (from root directory with the yaml file)
docker-compose ps

Replace Dockerfile with docker-compose.yml

A common use case for multiple files is changing a development Compose app for a production-like environment (which may be production, staging or CI). To support these differences, you can split your Compose configuration into a few different files. In the example we have web and tests for dev environment.

version: “3” //version 3 of the tool
services:
web:
stdin_open: true
build:
context: . //the content to build the Dockerfile
dockerfile: Dockerfile.dev //get the name more clear
ports:
- “3000:3000”
volumes: //mapping local file to docker container == setting up reference to the local file
- /app/node_modules //make this reserved in container, so this dir won't be overwritten by the copy command below
- .:/app. //map the parent directory /app in the container

tests: //2nd service solely to run the test live
stdin_open: true
build:
context: .
dockerfile: Dockerfile.dev
volumes:
- /app/node_modules
- .:/app
command: ["npm", "run", "test"] //override original npm run start command

As reference, the Dockerfile.dev that is mentioned above:

Dockerfile.dev
=====================
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "run", "start"]

A more complex but common configure with client, server, worker and nginx:

//docker-compose.yml
version: “3”
services:
postgres:
image: “postgres:latest”
environment:
- POSTGRES_PASSWORD=postgres_password
redis:
image: “redis:latest”
server:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /app/node_modules
- ./server:/app
environment: //set up envName=value
- REDIS_HOST=redis
- REDIS_PORT=6379
- PGHOST=postgres
- PGUSER=postgres
- PGDATABASE=postgres
- PGPASSWORD=postgres_password
- PGPORT=5432
client:
stdin_open: true
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- /app/node_modules
- ./client:/app
worker:
build:
dockerfile: Dockerfile.dev
context: ./worker
volumes:
- /app/node_modules
- ./worker:/app
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- "3050:80"

And the Dockerfiles mentioned above are:

//server 
FROM node:alpine
WORKDIR '/app'
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
//worker
FROM node:alpine
WORKDIR '/app'
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
//nginx
FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf
//client
FROM node:alpine
WORKDIR '/app'
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]

That’s so much of it. Happy Reading!

🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟 🐬 🐳 🐋🐟

--

--