Contents

Docker- Building Image

Some notes about building docker image from Dockerfile

Dockerfile

simple example

1
2
3
4
5
6
7
8
9
FROM alpine

# Step 2: Download and install dependency

RUN apk add --update redis

# Step 3: Tell the image what to do when it starts as container

CMD ["redis-server"]

to run

1
docker build -t dockerID/project:version(latest) .

Alternatively, docker commit could be used.

  1. In terminal 1, docker run -it alpine sh
  2. Inside the container , run apk add --update redis
  3. Open terminal 2, docker ps get the container id
  4. run docker commit -c 'CMD ["redis-server"]' containerid

Copy building files

1
COPY ./(in host relative to build context)   ./(inside container)

Port Mapping

1
docker run -p localhost_port:port_inside_container <imagename>

Specifying Working Directory

1
WORKDIR  /usr/app

Minimizing Cache Busting and Rebuilds

Make sure there are less change possible in the previous steps

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Specify a base image
FROM node:alpine

WORKDIR /usr/app

# Less change in the previous steps
COPY ./package.json ./
RUN npm install
COPY ./ ./

# Default command
CMD ["npm", "start"]

Docker Compose

docker-compose.yml

1
2
3
4
5
6
7
8
9
version: "3"
services:
  redis-server:
    image: "redis"
  node-app:
    restart: on-failure[always/no/unless-stopped]
    build: .
    ports:
      - "4001:8081"

Same network will be created for services.

in redis, refer other services just by name.

1
2
3
4
const client = redis.createClient({
  host: "redis-server",
  port: 6379, // default
});

Command

Launch

1
2
3
docker-compose up
docker-compose up --build
docker-compose up -d // in background

Stop

1
docker-compose down

Status

1
docker-compose ps