Docker
- official, starts 따져서 다운받기
- 더블클릭 > 우클릭 : copy
- 우클릭 : paste
docker run
docker search ubuntu
docker pull ubuntu
docker login
docker images # 다운 검증
docker run -i -t --name ubuntu-hello ubuntu /bin/bash
docker run | 새로운 컨테이너를 실행하겠다는 명령이야. 즉, "이미지를 실행해!" |
-i (--interactive) | 터미널에서 입력을 받을 수 있게 만들어줘. 즉, 컨테이너 안에서 직접 타이핑 가능 |
-t (--tty) | 가상의 터미널 화면을 만들어줘. 보기 편하게 만들어줌 (예: 쉘 환경) |
--name ubuntu-hello | 실행할 컨테이너에 이름을 지정해줘. (docker ps로 확인할 때 보기 쉬움) |
ubuntu | 사용할 이미지 이름. 여기선 Ubuntu OS 이미지 |
/bin/bash | 컨테이너 안에서 실행할 명령어. Ubuntu 안에서 bash 쉘을 켜겠다는 뜻 |
📋 너의 docker ps 결과 요약
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9011e5f9fe33 ubuntu "/bin/bash" 6 minutes ago Up 6 minutes ubuntu-hello2
🧠 하나씩 설명할게!
항목의미
CONTAINER ID | 컨테이너의 고유 ID (9011e5f9fe33) — 짧은 해시값 |
IMAGE | 어떤 이미지로 만들었는가? → ubuntu |
COMMAND | 컨테이너 시작 시 실행된 명령어 → /bin/bash (쉘 실행) |
CREATED | 생성된 시점 → 6분 전 |
STATUS | 현재 상태 → "Up 6 minutes" (지금도 실행 중!) |
PORTS | 외부 포트 연결 정보 → 없음 (포트 매핑 안 함) |
NAMES | 컨테이너 이름 → ubuntu-hello2 (직접 지정했거나 자동 생성됨) |
🔍 지금 상태 요약
- ✅ ubuntu 기반 컨테이너가 실행 중
- 🐚 컨테이너 내부에서 /bin/bash로 쉘이 열려 있음
- 🌐 포트는 외부로 노출하지 않았음
- 🏷️ 이름은 ubuntu-hello2
🧩 관련 명령어 몇 가지
명령어설명
docker exec -it ubuntu-hello2 bash | 실행 중인 컨테이너에 다시 들어가서 bash 쉘 실행 |
docker stop ubuntu-hello2 | 해당 컨테이너 종료 |
docker rm ubuntu-hello2 | 해당 컨테이너 삭제 (종료 후에만 가능) |
docker logs ubuntu-hello2 | 실행 중인 컨테이너의 출력 로그 확인 |
docker stop
PS C:\Users\Playdata> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9011e5f9fe33 ubuntu "/bin/bash" 15 minutes ago Exited (137) 2 minutes ago ubuntu-hello2
21518ee92a99 ubuntu "/bin/bash" 19 minutes ago Exited (0) 12 minutes ago ubuntu-hello
a530725bc29d ubuntu:latest "/bin/bash" 23 minutes ago Exited (0) 23 minutes ago elegant_kowalevski
03126be3e72d mysql "docker-entrypoint.s…" 5 days ago Up 2 hours 33060/tcp, 0.0.0.0:3307->3306/tcp mysql_server-db-1
f495840a5237 pgvector/pgvector:pg17 "docker-entrypoint.s…" 2 weeks ago Up 2 hours 0.0.0.0:5432->5432/tcp my-postgresql
- status: exited => stop 확인
PS C:\Users\Playdata> docker start ubuntu-hello2
ubuntu-hello2
PS C:\Users\Playdata> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9011e5f9fe33 ubuntu "/bin/bash" 17 minutes ago Up 4 seconds ubuntu-hello2
03126be3e72d mysql "docker-entrypoint.s…" 5 days ago Up 2 hours 33060/tcp, 0.0.0.0:3307->3306/tcp mysql_server-db-1
f495840a5237 pgvector/pgvector:pg17 "docker-entrypoint.s…" 2 weeks ago Up 2 hours 0.0.0.0:5432->5432/tcp my-postgresql
- NAMES : ubuntu-hello2 컨테이너 확인 가능
docker start > attach : 컨테이너 접속
PS C:\Users\Playdata> docker start ubuntu-hello2
ubuntu-hello2
PS C:\Users\Playdata> docker attach ubuntu-hello2
외부에서 컨테이너에게 명령어 전달 (touch: 파일 생성)
PS C:\Users\Playdata> docker attach ubuntu-hello2
root@9011e5f9fe33:/# ls -al
total 56
drwxr-xr-x 1 root root 4096 Apr 16 02:10 .
drwxr-xr-x 1 root root 4096 Apr 16 02:10 ..
-rwxr-xr-x 1 root root 0 Apr 16 02:10 .dockerenv
lrwxrwxrwx 1 root root 7 Apr 22 2024 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Apr 22 2024 boot
drwxr-xr-x 5 root root 360 Apr 16 02:33 dev
drwxr-xr-x 1 root root 4096 Apr 16 02:10 etc
drwxr-xr-x 3 root root 4096 Apr 4 02:09 home
lrwxrwxrwx 1 root root 7 Apr 22 2024 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Apr 22 2024 lib64 -> usr/lib64
drwxr-xr-x 2 root root 4096 Apr 4 02:03 media
drwxr-xr-x 2 root root 4096 Apr 4 02:03 mnt
drwxr-xr-x 2 root root 4096 Apr 4 02:03 opt
dr-xr-xr-x 306 root root 0 Apr 16 02:33 proc
drwx------ 1 root root 4096 Apr 16 02:31 root
drwxr-xr-x 4 root root 4096 Apr 4 02:09 run
lrwxrwxrwx 1 root root 8 Apr 22 2024 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Apr 4 02:03 srv
dr-xr-xr-x 11 root root 0 Apr 16 02:33 sys
drwxrwxrwt 2 root root 4096 Apr 4 02:09 tmp
drwxr-xr-x 12 root root 4096 Apr 4 02:03 usr
drwxr-xr-x 11 root root 4096 Apr 4 02:09 var
root@9011e5f9fe33:/# ls *.txt
ls: cannot access '*.txt': No such file or directory
root@9011e5f9fe33:/# touch test.txt
root@9011e5f9fe33:/# ls *.txt
test.txt
PS C:\Users\Playdata> docker exec ubuntu-hello2 touch test.json
PS C:\Users\Playdata> docker exec -it ubuntu-hello2 bash
root@9011e5f9fe33:/# ls *.json
test.json
컨테이너 삭제 : rm
PS C:\Users\Playdata> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9011e5f9fe33 ubuntu "/bin/bash" 32 minutes ago Up 3 minutes ubuntu-hello2
03126be3e72d mysql "docker-entrypoint.s…" 5 days ago Up 2 hours 33060/tcp, 0.0.0.0:3307->3306/tcp mysql_server-db-1
f495840a5237 pgvector/pgvector:pg17 "docker-entrypoint.s…" 2 weeks ago Up 2 hours 0.0.0.0:5432->5432/tcp my-postgresql
PS C:\Users\Playdata> docker stop ubuntu-hello2
ubuntu-hello2
PS C:\Users\Playdata> docker rm ubuntu-hello2
ubuntu-hello2
PS C:\Users\Playdata> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03126be3e72d mysql "docker-entrypoint.s…" 5 days ago Up 2 hours 33060/tcp, 0.0.0.0:3307->3306/tcp mysql_server-db-1
f495840a5237 pgvector/pgvector:pg17 "docker-entrypoint.s…" 2 weeks ago Up 2 hours 0.0.0.0:5432->5432/tcp my-postgresql
이미지 삭제 : rmi
PS C:\Users\Playdata> docker rmi 1e622c5f073b
Untagged: ubuntu:latest
Deleted: sha256:1e622c5f073b4f6bfad6632f2616c7f59ef256e96fe78bf6a595d1dc4376ac02
dockerfile 생성
# server image는 ubunutu 18.04를 사용
FROM ubuntu:18.04
# image가 올라갔을 때 수행되는 명령어들
# -y 옵션을 넣어서 무조건 설치가 가능하도록 한다.
RUN \
apt-get update && \
apt-get install -y apache2
# 파일 복사
COPY hello.html /var/www/html
# 작업공간 이동 (=cd)
WORKDIR /var/www/html
# 로그 확인
RUN ls
# apache가 기본적으로 80포트를 사용하기 때문에 expose를 이용해 apache server로 접근이 가능하도록 한
EXPOSE 80
# 컨테이너가 생성 된 이후에 내부의 아파치 서버는 항상 실행중인 상태로 만들어준다.
# apachectl을 foreground(즉, deamon)상태로 돌아가도록 한다.
CMD ["apachectl", "-D", "FOREGROUND"]
📦 이 Dockerfile, 무슨 일을 하는 걸까?
"🏗️ Ubuntu 18.04 위에 🧱 Apache 웹서버를 설치하고,
📄 hello.html 파일을 서비스하는 서버를 만든다!"
🧱 한 줄씩 해설
dockerfile
CopyEdit
# 서버 이미지로 Ubuntu 18.04를 사용할 거야 FROM ubuntu:18.04
- 요리의 기본 재료 선택!
- "Ubuntu 18.04 박스"에서 시작할 거야.
dockerfile
CopyEdit
# apache2 설치 (웹서버 역할) RUN \ apt-get update && \ apt-get install -y apache2
- RUN: 명령어 실행 단계
- Apache2라는 웹서버 프로그램을 설치해
- -y: "예/아니오 물어보지 말고 그냥 설치해!"
dockerfile
CopyEdit
# HTML 파일을 아파치의 웹 루트 디렉토리로 복사! COPY hello.html /var/www/html
- COPY: 내 컴퓨터의 파일을 컨테이너 안으로 복사!
- /var/www/html: Apache가 기본으로 서빙하는 폴더야.
즉, 누가 웹브라우저로 접근하면 이 HTML이 보이게 되는 거지 👀
dockerfile
CopyEdit
# 앞으로 작업할 디렉토리 설정 WORKDIR /var/www/html
- 이후 명령어 실행 시 기준이 될 폴더야.
- 마치 cd /var/www/html 한 거랑 같아.
dockerfile
CopyEdit
# 디버깅용 - html 폴더 안에 뭐 있나 리스트 보기 RUN ls
- 현재 디렉토리 내용을 보여줘서, 파일 잘 들어왔나 확인하는 용도.
dockerfile
CopyEdit
# 외부에서 접속할 수 있도록 80번 포트를 연다 EXPOSE 80
- Apache는 기본적으로 80번 포트로 동작하니까,
- 나중에 docker run -p 8080:80처럼 외부랑 연결할 수 있도록 알려주는 선언.
dockerfile
CopyEdit
# Apache 서버를 '계속 켜진 상태로' 실행하도록 한다 CMD ["apachectl", "-D", "FOREGROUND"]
- CMD: 컨테이너 시작할 때 실행할 명령어
- apachectl: Apache 시작 명령어
- -D FOREGROUND: 백그라운드가 아니라 계속 실행 상태로 유지되게 함
컨테이너가 죽지 않고 계속 돌아가려면 꼭 이렇게 해야 해!
🧭 이 Dockerfile 전체 요약
단계설명
FROM | Ubuntu 18.04로 시작 |
RUN | Apache2 설치 |
COPY | hello.html을 웹 서버 위치로 복사 |
WORKDIR | 그 위치로 이동 |
RUN | 파일 잘 복사됐는지 ls로 확인 |
EXPOSE | Apache용 포트 80 열기 |
CMD | Apache 웹 서버를 계속 켜놓기 |
dockerfile 변경후 이미지 만들고 새로 컨테이너 만들어서 웹 띄우기
- container stop > rm
- image 에다 태그만 새로 붙여서 빌드
- 똑같이 컨테이너 만들어서 실행
여러개 이미지 두고 컨테이너 운영하고 싶다면?
- 이미지에다 태그 새로 붙여서 빌드
- 컨테이너 런할때 부모 포트를 81로 새로 할당, 자식 포트는 80으로 기존대로 해도 됨
29 docker build -t apache-image:ops .
30 docker images
31 docker run -d -p 81:80 --name apache-container-world-ops apache-image:ops
- http://localhost:81/common/world.html 로 접속
docker pull
PS C:\dev\수업자료\docker> docker pull ubuntu/apache2
Using default tag: latest
latest: Pulling from ubuntu/apache2
7b7f87d905cc: Download complete
742adbd14f98: Download complete
4dc44e212a77: Download complete
Digest: sha256:16698e95077b9f1fa47fd3e133d41b8116614c19d5ce16e14ef898406d8145fd
Status: Downloaded newer image for ubuntu/apache2:latest
docker.io/ubuntu/apache2:latest
PS C:\dev\수업자료\docker> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu/apache2 latest 16698e95077b 6 days ago 270MB
pgvector/pgvector pg17 5982c00a2cdf 5 weeks ago 634MB
mysql latest 0596fa224cdf 2 months ago 1.09GB
- 반드시 실행후 확인하는 습관 들여야 한다
docker mount
PS C:\dev\수업자료\docker> docker run -d --name apache-container `
>> -p 80:80 `
>> -v C:\dev\수업자료\docker\common:/var/www/html `
>> ubuntu/apache2
docker compose로 이미지 만들기
version: '3'
services:
# dockerfile
apache2:
# from
image: ubuntu/apache2:latest
# build
container_name: apache2-container
volumes:
- ./common:/var/www/html
ports:
- "80:80"
- docker-compose.yml로 만들고
- docker-compose up -d
'SK Networks Family AI bootcamp 강의노트' 카테고리의 다른 글
65일차 [ docker & linux ] (0) | 2025.04.22 |
---|---|
64일차 [ docker & nginx , network ] (0) | 2025.04.17 |
60일차 [ langgraph, MCP ] (0) | 2025.04.15 |
59일차 [ Langsmith ] (0) | 2025.04.10 |
57-58일차 [ RAG ] (0) | 2025.04.10 |