rovan-a of Pixel's Programmer
1/4/2017 - 1:10 AM

Docker syntax

Docker syntax

# set docker compose version 2
version: '2'

#start listing service
services:
  # Build container with name nginx from docker file on directory ./nginx/Dockerfile, expose and forward port 80, link to another docker with name node1~node4
  nginx:
    build: ./nginx
    ports:
      - "80:80"
    links:
      - node0
      - node1
      - node2
      - node3

  # Build container with name node0 from docker file on directory ./expressServer/Dockerfile, expose and forward port 3000, set ENV VAR PORT = 3000
  node0:
    build: ./expressServer
    volumes:
      - ./expressServer:/var/www/expressServer
    environment:
      - PORT=3000
    ports:
      - "3000:3000"

  # Build container with name node1 from docker file on directory ./expressServer/Dockerfile, expose and forward port 3001, set ENV VAR PORT = 3001
  node1:
    build: ./expressServer
    volumes:
      - ./expressServer:/var/www/expressServer
    environment:
      - PORT=3001
    ports:
      - "3001:3001"

  # Build container with name node2 from docker file on directory ./expressServer/Dockerfile, expose and forward port 3002, set ENV VAR PORT = 3002
  node2:
    build: ./expressServer
    volumes:
      - ./expressServer:/var/www/expressServer
    environment:
      - PORT=3002
    ports:
      - "3002:3002"

  # Build container with name node3 from docker file on directory ./expressServer/Dockerfile, expose and forward port 3003, set ENV VAR PORT = 3003
  node3:
    build: ./expressServer
    volumes:
      - ./expressServer:/var/www/expressServer
    environment:
      - PORT=3003
    ports:
      - "3003:3003"
# Set base image to node:6
FROM node:6

# make directory and set as working directory
RUN mkdir -p /var/www/expressServer
WORKDIR /var/www/expressServer

# copy package.json and install npm dependencies
COPY package.json /var/www/expressServer
RUN npm install

# Copy all file from currect directory to working directory
COPY . /var/www/expressServer

# Expose port
EXPOSE 3000

# Run program
CMD ["npm", "start"]
# Build docker image with name tester from Docker file on current directory
docker build -t tester .

# Create and run docker container with nginx from image tester and attach to forward port 80
docker run -d --name nginx -p 80:80 tester

# Start container with name tester
docker run tester

# Stop container with name tester
docker stop tester

# See all docker image on host
docker images

# See all docker container
docker ps             # only running 
docker ps -a          # all created container

# Delete container with name nginx
docker rm nginx

# Delete image with name tester
docker rmi tester

# Build / Run many container from docker-compose.yml file
docker-compose up            # basic
docker-compose up -d         # run on detachable mode
docker-compose up -d --build # rebuild all image before run the container

# Stop docker-compose
docker-compose down_