DEV Community

badruti94
badruti94

Posted on • Updated on

Catatan belajar docker

Github source code link

Docker Compose

Command

# build with no cache and detail
docker compose build --no-cache --progress plain
# create and run
docker compose create
docker compose start

# delete
docker compose down
docker image rm image-name


Enter fullscreen mode Exit fullscreen mode

Example

Mongo Db

version: "3.8"

services:
  mongodb1:
    image: mongo:latest
    container_name: mongodb1
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: khannedy123
      MONGO_INITDB_ROOT_PASSWORD: khannedy123
      MONGO_INITDB_DATABASE: admin
    volumes:
      - "mongo-data1:/data/db"
  mongodb-express:
    image: mongo-express:latest
    container_name: mongodb-express
    restart: always
    ports:
      - "8081:8081"
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: khannedy123
      ME_CONFIG_MONGODB_ADMINPASSWORD: khannedy123
      ME_CONFIG_MONGODB_SERVER: mongodb1
      ME_CONFIG_BASICAUTH_USERNAME: root
      ME_CONFIG_BASICAUTH_PASSWORD: example
    depends_on:
      - mongodb1

volumes:
  mongo-data1:
    name: mongo-data1
  mongo-data2:
    name: mongo-data2
Enter fullscreen mode Exit fullscreen mode

For Dockerfile

version: "3.8"

services:

  node-example:
    container_name: node-example
    build:
      context: "."
      dockerfile: Dockerfile
    image: "app-node-example:1.0.0"
    ports:
      - "3000:3000"

Enter fullscreen mode Exit fullscreen mode

Dockerfile

Command

# inspect image
docker image inspect badru/workdir
# logs container
docker container logs workdir
# get into container
docker container exec -i -t workdir /bin/sh
Enter fullscreen mode Exit fullscreen mode

Example

Golang

FROM golang:1.18-alpine

WORKDIR /app/
COPY . /app/

EXPOSE 8080
CMD go run main.go
Enter fullscreen mode Exit fullscreen mode

Node js (no dependency)

FROM node:16-alpine

WORKDIR /app/

COPY . /app/

EXPOSE 3000

CMD node app.js
Enter fullscreen mode Exit fullscreen mode

Node js with dependency

FROM node:16-alpine

WORKDIR /app/

COPY . /app/

EXPOSE 3000

RUN npm install

CMD node app.js
Enter fullscreen mode Exit fullscreen mode

Multi with Golang


FROM golang:1.18-alpine as builder
WORKDIR /app/
COPY . /app/
RUN go build -o main main.go

FROM alpine:3
WORKDIR /app/
COPY --from=builder /app/main /app/
EXPOSE 8080
CMD ./main
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
badruti94 profile image
badruti94

tes1

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted