Docker 기초 - boostcampwm-2022/web36-Japari GitHub Wiki
Preface
생활코딩님의 Docker 강의를 보고 공부한 내용을 정리해 보았다.
- Docker 입구 수업
- docker commit
- docker build
- docker push
- docker compose
Docker란?
Linux는 한 대의 컴퓨터 안에서 여러 프로그램을 각자 격리된 환경(container)에서 실행할 수 있도록 해주는 기능을 제공한다.
여기서 '환경이 격리되었다'는 것은 A container에서 git과 Node.js를 설치하더라도 B container에는 아무런 변화도 일어나지 않음을 의미한다.
이 때, Docker는 격리된 실행 환경(container)을 image로 저장, 업로드, 다운로드 할 수 있게 해준다.
Docker를 사용하면 손쉽게 개발 환경과 배포 환경을 일치시킬 수 있다.
Docker 설치
container는 리눅스 OS 고유의 기술이기 때문에 Windows나 Mac OS 유저는 가상 머신을 깔고 거기에 Linux를 깔아야 container를 사용할 수 있다.
Linux 유저는 바로 Docker를 사용할 수 있지만 Windows 유저는 WSL2를 설치해준 뒤에야 Docker를 사용할 수 있다.
설치 단계
- (Windows / MacOS) WSL2 설치 - 5단계까지
- Docker Desktop 설치
Docker CLI 기본 명령어
docker hub에서 image를 pull 할 수 있다.
image를 run하면 container가 만들어진 뒤 실행된다. 즉, run = create + start 이다.
shell
docker pull httpd # pull Apache Web Server image from docker hub.
docker images # list images.
docker run --name apache httpd # run image 'httpd'. the container's name will be 'apache'.
docker ps # list running containers.
docker stop apache # stop the container 'apache'.
docker ps --all # list all containers. stopped containers are also listed.
docker start apache # start the container 'apache'.
docker logs --follow apache # fetch the logs of the container 'apache'. the container's logs will be followed.
docker rm --force apache # remove the container 'apache'.
docker rmi --force httpd # remove the image httpd
Docker로 Apache Server 실행하기
Docker가 설치한 가상 OS를 host라고 한다.
host와 container는 독립적인 file system, port를 가지기 때문에 container 내부의 web server로 접속하기 위해서는 port forwarding이 필요하다.
참고로 option의 -p는 —port의 줄임말이 아닌 —publish의 줄임말이다.
shell
docker pull httpd
docker run -p 80:80 --name apache httpd
container 내부에서 명령어 실행하기
shell
docker exec apache "ls -al" # "apache" container 내부에서 "ls -al" 명령 실행
docker exec -ti apache /bin/bash # 이후 실행되는 명령은 "apache" container 내부에서 실행된다.
exit # 이후 실행되는 명령은 host에서 실행된다.
-ti
옵션은 bash shell /bin/bash
가 실행 즉시 종료되는 것을 막아주는 역할을 한다.
만약 container에 bash shell이 없다면, bourne shell /bin/sh
을 사용하면 된다.
host의 volume으로 container의 volume 덮어 쓰기
shell
docker run -p 80:80 -v /apache/:/usr/local/apache2/htdocs/ httpd
docker의 /usr/local/apache2/htdocs/
가 host의 /apache/
의 내용으로 덮어 씌워진다.
Docker commit & push
container를 commit하면 새로운 image가 생성된다.
image를 push하면 docker hub에 image가 업로드된다.
물론, public 업로드 뿐만 아니라 private 업로드도 가능하다.
시나리오
이 순서로 시나리오를 진행하면 Git + Node.js
, Git + PHP
, Git + Python
container를 얻을 수 있다.
shell
# ubuntu image 다운로드
docker pull ubuntu
docker run -ti --name my-ubuntu ubuntu bash
# git 설치
apt update && apt install git
exit
# commit
docker commit my-ubuntu mathlife:my-git
docker images
shell-1
# container 생성
docker run -ti --name my-nodejs mathlife:ubuntu-git bash
apt update && apt install nodejs
shell-2
# container 생성
docker run -ti --name my-python mathlife:ubuntu-git bash
apt update && apt install python
Dockerfile and Docker build
commit으로 이미지를 만든다고 해보자.
이 때, 우리는 만들어진 이미지가 어떤 이미지로부터 어떤 과정을 거쳐서 만들어졌는지 알 수가 없다.
이번에는 Dockerfile을 build해서 이미지를 만든다고 해보자.
이 때, Dockerfile을 확인하면 만들어진 이미지가 어떤 이미지로부터 어떤 과정을 거쳐 만들어졌는지 쉽게 알 수 있다.
commit과 build 모두 이미지를 생성해준다는 공통점을 갖는다.
하지만, commit은 현재 사용하고 있는 container를 image로 backup 해두는 느낌에 가까운 반면 build는 image를 직접 설계하고 생성하는 느낌에 가깝다.
-
host에
index.html
파일을 만든다. -
Dockerfile을 생성한다.
Dockerfile
FROM ubuntu:20.04 # RUN: build 시점에 실행할 명령어 RUN apt update && apt install -y python3 WORKDIR /var/www/html # host의 index.html을 container의 /var/www/html/.으로 복사한다. COPY ["index.html", "."] # CMD: container가 실행되는 시점에 실행할 명령어 CMD ["python3", "-u", "-m", "http.server"]
-
Dockerfile을 build한다.
shell
docker build --tag web-server-image . # --tag [name | name:tag]
-
생성한 ‘web-server’ image를 run 해본다.
shell
docker run -p 80:8000 --name web-server web-server-image
Docker push
생성한 image를 docker hub로 push할 수 있다.
-
dockerhub에 접속해 회원가입 · 로그인을 진행한다.
-
python3
private repository를 생성한다. -
ubuntu image를 다운받고 이 이미지로 container를 만든 뒤 python3를 설치한다.
docker run -it --name my-python ubuntu apt update && apt install -y python3 # -y 옵션을 지정하면 설치 과정에 [Y/N] 입력이 요구되지 않는다. exit
-
mathlife12345/python3:tagname
형식의 이름으로 image를 생성한다.docker commit my-python mathlife12345/python3:1.0
-
docker registry에 로그인한다.
docker login
-
docker registry에 image를 업로드한다.
docker push mathlife12345/python3:1.0
-
image를 삭제하고 다시 다운로드 받아 본다.
docker rmi --force mathlife12345/python3 docker images docker pull mathlife12345/python3:1.0
Docker compose
to be continued