[Docker] Docker 입문하기

Suyeon·2022년 3월 8일
3

Docker

목록 보기
1/1
post-thumbnail

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.

1. Docker Image

  • actual package (e.g. config, postgresql, script)
  • artifact

2. Docker Container

  • when you run an image on local machine, it is a container.
  • Running environment for image.
  • port binding: talk to application running inside of container.

3. Docker vs VM

Docker

  • Running on application.
  • Docker image’s size is smaller than VM (MB).
  • Docker container is much faster than VM.

VM

  • Running on application and OS kernel.
  • Size is bigger than dockaer image (GB).
  • VM of any OS can run on any OS host.

4. Docker Commands

4.1 Flags

flagdescription
-ddetach mode
-pbind host port to container port
-aall
—nameset custom name
-itinteractive terminal
-eenvironment variable

4.2 Basic Commands

# downloaded images
docker images

# download image
docker pull [image]

# download and run an image (pull + run)
docker run [image]:[version]

# run container on detach mode: running on background
docker run -d [container_id]

# stop container
docker stop [container_id]

# restart container
docker restart [container_id]
docker start [container_id]

# delete container
docker rm [container_id]

# running containers list
docker ps

# list running and stopped history
docker ps -a

# search a container
docker ps -a | grep [container_name]

# port binding(map port 80 in the container to port 8080 on the host machine)
docker container run -p 8080:80 -d nginx

# login to registry
docker login [registry_address]

4.3 Debug Commands

# container logs
docker logs [container_id]
docker logs [container_name]

# enter on container
docker exec -it [container_id] bin/bash
docker exec -it [container_name] bin/bash
docker exec -it [container_name] bin/sh # bash가 설치되지 않은 경우

# exix exec mode
exit

docker exec -it [container_name] redis-cli
docker exec -it [container_name] mysql

4.4 Network Command

  • busybox: utility library
# docker network list
docker network ls

# create a network
docker network create [network_name]

# network detail
docker network inspect [network_name]

# connect container to network
docker network connect [network_name] [container_name]

# disconnect container from network
docker network disconnect [network_name] [container_name]

# run and connect container to network
docker run -itd --name [container_name] --network [network_name] busybox

4.5 Docker Compose

# run all containers using YAML file
docker-compose -f [YAML_FILE_NAME] up

# stop and remove containers and network
docker-compose -f [YAML_FILE_NAME] down

4.6 Docker Image

# create an image
docker build -t [image_name]:[image_tag] [location_dockerFile]
docker build -t my-node-app:1.0 .

# delete an image
docker rmi [image_id]

# docker image list
docker images

5. Docker Network

참고한 블로그 포스트

6. Docker Compose

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.

  • docker-compose는 도커 설치시 함께 설치된다.

6.1 Docker Run Command

  • 여러개의 컨테이너를 돌릴 경우, 아래와 같이 일일히 설정하는 과정은 매우 귀찮다.
# create a network
docker network create [mongo-network]

# run mongo container
docker run -p 27017:27017 -d -e MONGO_INITDB_ROOT_USERNAME=suyeon -e MONGO_INITDB_ROOT_PASSWORD=suyeonme --name mongodb --net mongo-network

# run mongo-express container (connect mongodb to express)
docker run -d \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=suyeon \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=suyeonme \
--net mongo-network \
--name mongo-express \
-e ME_CONFIG_MONGODB_SERVER=mongo \
mongo-express

6.2 compose.yaml

  • 따라서 docker-compose를 사용하여 아래와 같이 YAML 파일로 작성하여 한번에 관리한다.
version: '3'
services:
	## my application image
  # my-app:
  # image: ${docker-registry}/my-app:1.0
  # ports:
  # - 3000:3000
  mongodb:
    image: mongo
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - mongo-data:/data/db
  mongo-express:
    image: mongo-express
    restart: always # fixes MongoNetworkError when mongodb is not ready when mongo-express starts
    ports:
      - 8080:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
  mongo-data:
    driver: local

7. Docker File

The Dockerfile is essentially the build instructions to build the image.

  • 파일 이름은 Dockerfile 을 사용해야한다.
  • dockerFile을 수정하면, 이미지도 다시 빌드해야한다.
    • 해당 이미지를 사용하는 컨테이너가 있다면 컨테이너와 이미지를 제거한다.
    • 다시 이미지 빌드 후 수정된 이미지로 컨테이너를 실행한다.
  • 주로 압축된 artifact로 dockerFile을 만든다.
# base image
FROM node:13-alpine

# environment variables
ENV MONGO_DB_USERNAME=admin \
    MONGO_DB_PWD=password

# executes command in a new layer
RUN mkdir -p /home/app

# executes on the host machine
COPY ./app /home/app

# set default dir so that next commands executes in /home/app dir
WORKDIR /home/app

# will execute npm install in /home/app because of WORKDIR
RUN npm install

# executes entrypoint (set a default command)
CMD ["node", "/home/app/server.js"]

8. Docker Image

8.1 When you modify the source code,

# 버전업해서 도커 파일 생성(1.0 -> 1.1)
docker build -t [image_name]:1.1 

# rename to include repository address
docker tag [image_name]:1.1 [repository_address]:1.1

# push to the repository
docker push [repository_address]:1.1

9. Docker Registry

A Docker registry is a storage and distribution system for named Docker images. The same image might have multiple different versions, identified by their tags. A Docker registry is organized into Docker repositories , where a repository holds all the versions of a specific image.

  • Upload a docker image to AWS ECR

10. Docker Volumes

Docker volumes are directories and files that exist on the host file system outside of the Docker container. These volumes are used to persist data and share data between Docker containers. Docker supports the mounting of one or more data volumes from the host OS to the Docker container.

  • Container uses virtual file system. So when restarting or removing the container, data is lost.

10.1. 3 Volume Type

# Host Volume
docker run -v [host_directory]:[container_directory]

# Named Volume: reference the volumn by name 💫
# production
docker run -v name:[container_path]

10.2. compose.yaml

  • 데이터 경로 지정
    • mongodb: /data/db
    • mysql: var/lib/mysql
    • postgres: var/lib/postgresql/data
  • Docker Volume Locations
    • Window: c:\ProgramData\docker\volumns
    • LInux, Mac: /var/lib/docker/volumes
version: '3'
services:
  mongodb:
		#...
    volumes:
      - mongo-data:/data/db # [host_volumn_name]:[path_inside_of_the_container]
volumes: 
  mongo-data:
    driver: local

11. .dockerignore

  • like .gitignore
  • Optimize container image size by including unnecessary files in .dockerignore.
modules/*

12. Workflow

  1. 어플리케이션 개발(e.g. node.js)
  2. mongodb를 로컬에 설치하지 않고, 도커 컨테이너로 mongodb image 실행
  3. 어플리케이션과 mongodb 연결한 뒤 개발
  4. GIT에 commit (repository에 push)
  5. push된 내용중 Dockerfile 로 jenkins의 CI/CD를 트리거
    5.1. 젠킨스는 Dockerfile로 도커 이미지를 생성한다.
  6. Registry에 도커 이미지 업로드 (AWS ECR)
  7. Dev 서버에서 업데이트된 이미지를 pull하여 사용
profile
Hello World.

0개의 댓글