Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.
port binding
: talk to application running inside of container.flag | description |
---|---|
-d | detach mode |
-p | bind host port to container port |
-a | all |
—name | set custom name |
-it | interactive terminal |
-e | environment variable |
# 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]
# 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
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
# 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
# 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
참고한 블로그 포스트
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
는 도커 설치시 함께 설치된다.# 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
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
The
Dockerfile
is essentially the build instructions to build the image.
Dockerfile
을 사용해야한다.dockerFile
을 수정하면, 이미지도 다시 빌드해야한다.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"]
# 버전업해서 도커 파일 생성(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
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.
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.
# Host Volume
docker run -v [host_directory]:[container_directory]
# Named Volume: reference the volumn by name 💫
# production
docker run -v name:[container_path]
mongodb: /data/db
mysql: var/lib/mysql
postgres: var/lib/postgresql/data
c:\ProgramData\docker\volumns
/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
.gitignore
modules/*
node.js
)mongodb
를 로컬에 설치하지 않고, 도커 컨테이너로 mongodb image 실행mongodb
연결한 뒤 개발Dockerfile
로 jenkins의 CI/CD를 트리거pull
하여 사용