#docker 명령어 모음
https://www.daleseo.com/dockerfile/ // 다커 명령어 모음
# docker pull // docker-hub로부터 컨테이너 생성을 위한 이미지 가져오기
[ec2-user@ip-xxxx nodejs]$ docker pull python:3.9-slim
3.9-slim: Pulling from library/python
214ca5fb9032: Pull complete
fa7d81b69b9a: Pull complete
96a5a0a62ab5: Pull complete
7d3628511179: Pull complete
ea3879bc1c47: Pull complete
Digest: sha256:364ada4cb4f943b603d330cbb3d26f689c8b54de51cb12bad1202de7dba31814
Status: Downloaded newer image for python:3.9-slim
docker.io/library/python:3.9-slim
// docker pull을 하면 하나의 파일이 다운되지 않고 여러 파일이 다운로드 된다.
docker는 레이어(layer)라는 것으로 이미지가 여러 개의 레이어가 되어있는데, 이게 다운로드 되는 것이다.
받아진 이미지를 분석해보고 싶다면 "docker inspect python:3.9-slim" 를 입력하면이 이미지 구성을 볼 수 있다.
[ec2-user@ip-xxxx nodejs]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.9-slim 29cc46b21611 5 days ago 125MB
[ec2-user@ip-xxxx nodejs]$ docker run -it --name=jane_python python:3.9-slim /bin/bash
root@c0b5a0c315fb:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
#docker_image삭제 // 삭제가 안될 때가 있음. 그럴 땐 container 확인
[ec2-user@ip-xxxx nodejs]$ docker rmi $(docker images -q).
Error response from daemon: conflict: unable to delete 29cc46b21611 (must be forced) - image is being used by stopped container 7fabb7cdfd7a
#docker_container_조회
[ec2-user@ip-xxxx nodejs]$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7fabb7cdfd7a python:3.9-slim "python3" 4 minutes ago Exited (0) 4 minutes ago jane-test
#docker_container_삭제
[ec2-user@ip-xxxx nodejs]$ docker container rm 7fabb7cdfd7a
7fabb7cdfd7a
[ec2-user@ip-xxxx nodejs]$ docker rmi 29cc46b21611
Untagged: python:3.9-slim
Untagged: python@sha256:364ada4cb4f943b603d330cbb3d26f689c8b54de51cb12bad1202de7dba31814
Deleted: sha256:29cc46b21611795a55a23f40290d9af983162d3d0a2cf4a11ab109a6c3183e06
#Dockerfile
[ec2-user@ip-xxxx nodejs]$ cat Dockerfile
FROM python:3.9-slim
WORKDIR /usr/src/app // python 안에 있는 directory
COPY . .
CMD ["test.py"]
[ec2-user@ip-xxxx nodejs]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
#docker_build
[ec2-user@ip-xxxx nodejs]$ docker build --tag jane-test .
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM python:3.9-slim
3.9-slim: Pulling from library/python
[root@ip-192-168-23-47 jane-test]#
[ec2-user@ip-xxxx nodejs]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jane-test latest 7639b54570bf 2 seconds ago 125MB
python 3.9-slim 29cc46b21611 5 days ago 125MB
#docker_run
[ec2-user@ip-xxxx nodejs]$ docker run -it --name jane-test jane-test:latest
Hello World!
#docker_mount // Bind mounts를 이용해 호스트 파일 or 디렉토리를 컨테이너에 붙여 사용하기
[ec2-user@ip-xxxx nodejs]$ docker run -itd --name jane-test --mount type=bind,source="$(pwd)"/mount,target=/app ubuntu:18.04
Status: Downloaded newer image for ubuntu:18.04
d5c20934eb0f6a72286c24c5a092876bdd5064b84e5434031e29dc68a6657460
[ec2-user@ip-xxxx nodejs]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5c20934eb0f ubuntu:18.04 "bash" 7 seconds ago Up 6 seconds jane-test
# 내부 디렉토리에 있는 게 컨테이너(위에서 target으로 삼은 경로)에도 동일하게 보임
[ec2-user@ip-xxxx nodejs]$ pwd
/home/ec2-user/jane-test/nodejs/mount
[ec2-user@ip-xxxx nodejs]$ ls
test.txt
[ec2-user@ip-xxxx nodejs]$ sudo docker exec -it jane-test ls /app
test.txt
Note) python 버전별 차이 (3.9-slim, buster …)
https://medium.com/swlh/alpine-slim-stretch-buster-jessie-bullseye-bookworm-what-are-the-differences-in-docker-62171ed4531d