다루는 명령어
docker pull
docker images
docker ps
docker ps -a
docker run -d
docker run -d --name node-app
docker run -d --name node-app -p {host_port}:{container_port}
docker stop {container_id}
docker start{container_id}
docker build -t {container_name}:{tag} .
docker exec -it
docker logs
이미지 받기
이미지는 docker hub에서 받을 수 있다. 단, 회사에서 사용하는 private registry에서도 받을 수 있다.
보안상 이슈로 private registry에서 사용하는게 좋다. 회사내에서 보안팀이 검수한 이미지를 사용
명령어
docker pull {name}:{tag}
실습
nginx 이미지 받기
~ docker pull nginx:1.23
~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.23 b005e88565d7 12 months ago 135MB
nginx 최신버전 받기
~ docker pull nginx
~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 705b7f60fea5 4 days ago 193MB
nginx 1.23 b005e88565d7 12 months ago 135MB
이미지 띄우기 (컨테이너 실행)
docker run {image}:{tag}
실습
컨테이너 실행
➜ ~ docker run nginx:1.23
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01db922bcae7 nginx:1.23 "/docker-entrypoint.…" 35 seconds ago Up 33 seconds 80/tcp eloquent_hoover
background 실행
➜ ~ docker run -d nginx:1.23
f5cb95774fdd86c6867401b2ba5f850b40ea5c4b0475dd6ae4c6ce497c46414f
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5cb95774fdd nginx:1.23 "/docker-entrypoint.…" 30 seconds ago Up 29 seconds 80/tcp tender_joliot
이미지가 로컬에 없더라도 실행가능 (외부 registry에서 받아와서 실행)
➜ ~ docker run -d nginx:1.22-alpine
Unable to find image 'nginx:1.22-alpine' locally
1.22-alpine: Pulling from library/nginx
c41833b44d91: Pull complete
871f52f2d8c5: Pull complete
b94fc67712d4: Pull complete
a12426eacd50: Pull complete
0ff84bd0545a: Pull complete
e1169f8a74eb: Pull complete
ebadb19b9fa4: Pull complete
Digest: sha256:8745c93f1a1c33a8ec8c82707b9bb1c8fe9ebf2b5d82e9480e78625d809855a1
Status: Downloaded newer image for nginx:1.22-alpine
c89ca6bd468140629ceb83b6ecb67ff6340587627dc1e28b75ec88c422867db3
로그보기
docker logs {containerid}
실습
➜ ~ docker logs f5cb95774fdd
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/06/03 08:55:42 [notice] 1#1: using the "epoll" event method
2024/06/03 08:55:42 [notice] 1#1: nginx/1.23.4
2024/06/03 08:55:42 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2024/06/03 08:55:42 [notice] 1#1: OS: Linux 6.6.14-0-virt
2024/06/03 08:55:42 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/06/03 08:55:42 [notice] 1#1: start worker processes
2024/06/03 08:55:42 [notice] 1#1: start worker process 29
2024/06/03 08:55:42 [notice] 1#1: start worker process 30
➜ ~
container 종료
docker stop {container}
실습
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c89ca6bd4681 nginx:1.22-alpine "/docker-entrypoint.…" 30 seconds ago Up 29 seconds 80/tcp silly_lalande
f5cb95774fdd nginx:1.23 "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp tender_joliot
➜ ~ docker stop c89ca6bd4681
c89ca6bd4681
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5cb95774fdd nginx:1.23 "/docker-entrypoint.…" 34 minutes ago Up 34 minutes 80/tcp tender_joliot
port binding
외부 통신을 하기위해 호스트의 포트와 컨테이너 포트를 1:1 매핑 시킴
-p ${host_port}:${container_port}
실습
nginx 컨테이너를 실행하는데 호스트 포트 9000으로 들어오는 요청을 컨터이너 80포트에 넘긴다.
➜ ~ docker run -d -p 9000:80 nginx:1.23
31e5a41f4758b7461a2e7e6281eeba3d5e111e9b4c1f844fe8a5f6e17537200f
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31e5a41f4758 nginx:1.23 "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 0.0.0.0:9000->80/tcp, :::9000->80/tcp lucid_kilby
http://localhost:9000/
컨테이너 시작
run 명령어를 사용 하면 같은 이미지에 새로운 컨테이너가 만들어짐
그래서 start를 이용해서 기존에 사용했던 컨테이너를 실행할 수 있음
docker start ${contianer_id}
실습
➜ ~ docker stop 31e5a41f4758
31e5a41f4758
➜ ~ docker start 31e5a41f4758
31e5a41f4758
container 이름설정
이름 설정을 안하면 docker가 임의로 이름을 설정합니다.
그래서 이름을 설정해두면 관리가 더 편하다
--name ${name}
➜ ~ docker run -d --name web-app -p 9000:80 nginx:1.23
5df0eb9234fdbe56a95666d54b2b28e3d3785a74c19085a9e9799d672f0dc57e
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5df0eb9234fd nginx:1.23 "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:9000->80/tcp, :::9000->80/tcp web-app
➜ ~ docker logs web-app
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/06/03 09:38:26 [notice] 1#1: using the "epoll" event method
2024/06/03 09:38:26 [notice] 1#1: nginx/1.23.4
2024/06/03 09:38:26 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2024/06/03 09:38:26 [notice] 1#1: OS: Linux 6.6.14-0-virt
2024/06/03 09:38:26 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/06/03 09:38:26 [notice] 1#1: start worker processes
2024/06/03 09:38:26 [notice] 1#1: start worker process 29
2024/06/03 09:38:26 [notice] 1#1: start worker process 30
➜ ~
Docker 이미지 만들기
Dockerfile 파일 생성 후 workflow 작성
- base image를 참고
- COPY: image에서 필요한 파일 복사
- WORKDIR: image 내에 어디서 명령어가 실행되어야 하는지 설정
- CMD: docker container를 실행하기 위한 명령어, 딱 한번 실행되어야 함
FROM node:20.14-alpine
COPY package.json /apps/
COPY server.js /apps/
WORKDIR /apps
RUN npm install
CMD ["node", "server.js"]
이미지 빌드 명령어
// Dockerfile 경로에서
docker build -t node-app:1.0 .
빌드 확인
➜ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node-app 1.0 e2b53833363e 4 minutes ago 159MB
nginx latest 705b7f60fea5 4 days ago 193MB
container shell 접근
~ docker exec -it ${container_id} sh
'IT' 카테고리의 다른 글
[docker] volume (0) | 2024.06.04 |
---|---|
[docker] compose 기본 명령어 (0) | 2024.06.04 |
[linux] lsof command (0) | 2024.06.03 |
[tcp] connection 개론 (1) | 2024.05.10 |
stream 이해 (0) | 2024.05.03 |