Docker-101 (Tutorial)

HYUNGU, KANG·2020년 5월 21일
0
post-thumbnail

https://www.docker.com/101-tutorial

Default commands

show list

docker ps (= docker container ls)
docker images (= docker image ls)

make docker image (build)

vi Dockerfile
docker build -t my-app

image re-naming(tag)

docker tag original-name new-name

run image

// docker -d -p host:container image-name
docker -d -p 80:80 my-app

push to remote-repo

// login to docker
docker login

// push to remote
docker push user-name/repo-name

remove container

// get container ids
docker ps
// stop container, remove container
docker stop container-id
docker rm container-id
(= docker rm -f container-id)

logs of container

docker logs -f container-id


Dockerfile

At the root of the app proeject, create a file named Dockerfile

  FROM node:10-alpine
  WORKDIR /app
  COPY . .
  RUN yarn install --production
  CMD ["node", "/app/src/index.js"]

Persist Data

Volume

Host(Volume) <-Data-> Container

Create Volume

docker volume create volume-name

docker run -dp 3000:3000 -v volume-name:/host/volume/path image-name

check volume info

docker volume inspect volume-name


Multi Container App

In general, each container should do one thing and do it well.
Node.js Container + MySQL Container => App

If two containers are on the same network, they can talk to each other. If they aren't, they can't.
Node.js Container <-Network-> MySQL Container
(remember each container has its own IP address. To figure it out, we're going to make use of the nicolaka/netshoot container, which ships with a lot of tools that are useful for troubleshooting or debugging networking issues.)

Start MySQL Container using Network

(--network-alias=[] : Add network-scoped alias for the container)

docker run -d \
    --network app-name --network-alias network-alias \
    -v app-mysql-data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=secret \
    -e MYSQL_DATABASE=myapp \
    mysql:5.7

Run MySQL CLI of Container

docker exec -it <mysql-container-id> mysql -p

(skip) Get MySQL Container network info

connect nicolaka/netshoot container to network-name network
docker run -it --network network-name nicolaka/netshoot
Inside the container, We're going to look up the IP address for the hostname mysql
dig mysql
In the "ANSWER SECTION", you will see an A record for mysql that resolves to
mysql. 600 IN A XXX.XXX.XXX.XXX

Start Node.js Container with MySQL Container

docker run -dp 3000:3000 \
  -w /app -v $PWD:/app \
  --network network-name \
  -e MYSQL_HOST=network-alias \
  -e MYSQL_USER=root \
  -e MYSQL_PASSWORD=secret \
  -e MYSQL_DB=myapp \
  node:10-alpine \
  sh -c "yarn install && yarn run dev"

Check data of MySQL Container

docker exec -it <mysql-container-id> mysql -p myapp


Docker Compose (= Multi Container App)

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

Compose File

At the root of the app project, create a file named docker-compose.yml

version: "3.7"

services:
  app:
    image: node:10-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: myapp
      
  mysql:
    image: mysql:5.7
    volumes:
      - volume-name:/host/volume/path
    environment: 
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: myapp

volumes:
  todo-mysql-data:

Up Containers

docker-compose up -d

Down Containers

docker-compose down

profile
JavaScript, TypeScript and React-Native

0개의 댓글