docker 정리
생활코딩
생활코딩
의 docker 수업을 base로 정리하였습니다.
생활코딩 Docker 입구 수업
생활코딩 서말
설치
https://docs.docker.com/desktop/install/windows-install/
Docker CLI
<example>
CLI의 []
부분은 CLI에서 생략 가능한 Optional 임을 표현하기 위해 표기했습니다. 실제로는 CLI 입력시 []
은 없어야 합니다.
image pull
https://hub.docker.com/ 에서 image를 다운로드 받습니다.
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
<example>
docker pull httpd
container run
pull로 다운로드 받은 image를 container로 실행합니다.
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
<example>
docker run [--name ws1] httpd
container List
container
목록 조회
docker ps [OPTIONS]
<example>
docker ps [-a]
container process 중지
실행된 container를 중지합니다.
docker stop [OPTIONS] CONTAINER [CONTAINER...]
<example>
docker stop ws1
container process 실행
중지된 container를 실행합니다.
이때는 run
과는 다르게 log가 출력되지 않습니다.
docker start [OPTIONS] CONTAINER [CONTAINER...]
<example>
docker start ws1
container logs
start
된 container의 log
를 출력합니다.
docker logs [OPTIONS] CONTAINER
<example>
docker logs [-f] ws1
container 삭제
container
를 삭제합니다.
docker rm [OPTIONS] CONTAINER [CONTAINER...]
<example>
docker rm ws1
image 삭제
image
를 삭제합니다.
docker rmi [OPTIONS] IMAGE [IMAGE...]
<example>
docker rmi httpd
네트워크
port forwarding
docker의 Host
와 Container
의 port를 연결합니다.
<example>
docker run [--name ws1 -p 8080:80] httpd
// Host 8080 port를 Container 80 port와 연결합니다.
Container 안에서 명령어 실행
exec
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
<example>
docker exec ws1 pwd
docker exec ws1 ls
docker exec [-it] ws1 /bin/sh
docker exec [-it] ws1 /bin/bash
Host와 Container의 File System 연결
-v
–volume list : Bind mount a volume
<example>
docker run [--name ws1 -p 8080:80 -v ~/Desktop/htdocs:/usr/local/apache2/htdocs] httpd
<window cmd example>
D:\dev\docker\desktop>docker run --name ws1 -p 8080:80 -v .\htdocs:/usr/local/apache2/htdocs httpd
// httpd(ws1) Container가 /usr/local/apache2/htdocs 폴더가 아닌 D:\dev\docker\desktop\htdocs 폴더를 바라보게 합니다.
image 만드는 법
commit
container
를 기반으로 image
를 생성합니다.
단점으로 이미지가 어떻게 만들어졌고 그 안에 뭐가 있는지 알 수 가 없습니다.
그렇기 때문에 좀 더 체계적으로 만들기 위해서는 dockerfile
을 이용합니다.
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
<example>
docker commit my-ubuntu [moregorenine:ubuntu-git]
// 1. ubuntu image로 my-ubuntu container 생성
// 2. my-ubuntu container에 git 설치
// 3. my-ubuntu container를 moregorenine reposity에 ubuntu-git 이름으로 image 생
dockerfile & build
Dockerfile
FROM ubuntu:20.04
RUN apt update && apt install -y python3
WORKDIR /var/www/html
# RUN echo "Hello, <strong>Docker</strong>" > index.html
# COPY ["index.html", "."]
CMD [ "python3", "-u", "-m", "http.server" ]
shell
docker buildx build [OPTIONS] PATH | URL | -
<example>
docker build -t web-server-build .;
docker rm --force web-server;
docker run -p 8888:8000 --name web-server web-server-build;
댓글남기기