오늘은 도커의 기본 명령어 중에서
docker create -it --name os1 centos:latest
docker inspect httpd
...
...
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
632dc7e05bc3 centos:latest "/bin/bash" 15 minutes ago Created os1
1e1c57ce01c6 httpd:latest "httpd-foreground" 21 minutes ago Created web1
52a57138f5c2 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago wonderful_swirles
docker start web1
docker inspect web1 # 실행중인 컨테이너 이름으로 조회
...
...
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": null
},
"SandboxKey": "/var/run/docker/netns/04b18731c711",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "8f72ee56903e2425188e046aa4bdcfa1adc8fee3dc07e210ef7451580cb1bdac",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
# 포트, ip주소 확인 가능
curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
docker start -ai os1
# OS 컨테이너 시작하기 -ai 옵션 필요
# 시작하면 컨테이너 쉘 접속 됨
# exit로 빠져나오면 컨테이너가 종료됨
# ctrl+p+q로 빠져나와야 컨테이너 유지
docker run --name os3 centos:latest hostname
fff3f921c001
docker run -it --name os3 -e "LAB=docker" centos:latest
echo $LAB # 컨테이너 환경
docker
docker inspect os3
...
...
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"LAB=docker",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos:latest",
"Volumes": null,
docker run -it --name os3 --cpus 0.3 centos:latest
dd if=/dev/zero of=/dev/null & #컨테이너 환경에서 강제로 부하 발생시킴
docker update --memory 700m os3 # 메모리 한도 업데이트
docker update --cpus 0.4 os3 # cpu 한도 업데이트
docker stats --no-stream # 할당된 컴퓨팅 리소스 확인
docker attach os2
docker exec os2 hostname
docker exec -it web2 bash # -it 옵션을 통해 웹 컨테이너에서 쉘을 실행 할 수도 있다.
# docker top에서 조회하는 PID값은 호스트 PID값과 같다.
docker top os2
UID PID PPID C STIME TTY TIME CMD
root 11980 11959 0 10:40 pts/0 00:00:00 /bin/bash
ps -ef | grep 11980
root 11980 11959 0 10:40 pts/0 00:00:00 /bin/bash
root 12445 2266 0 10:54 pts/0 00:00:00 grep --color=auto 11980
# 컨테이너 안에서의 PID 값은 다름
# 네임스페이스가 다르기 때문
docker attach os2
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 01:40 pts/0 00:00:00 /bin/bash
root 33 1 0 01:55 pts/0 00:00:00 ps -ef
docker run -d --name db1 mysql:5.7
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
569e57fe90d9 mysql:5.7 "docker-entrypoint.s…" 16 seconds ago Exited (1) 15 seconds ago db1
df4133201ceb centos:latest "/bin/bash" 20 hours ago Up 18 minutes os2
97f283e30b33 httpd:latest "httpd-foreground" 20 hours ago Up 15 minutes 80/tcp web2
52a57138f5c2 hello-world "/hello" 23 hours ago Exited (0) 23 hours ago wonderful_swirles
# status에서 Exited 값이 0이면 정상 종료, 1이면 이상 종료
# db1 컨테이너에 에러가 있어서 실행이 안됨
# 해당 컨테이너의 로그 확인
docker logs db1
2023-06-27 01:58:00+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.42-1.el7 started.
2023-06-27 01:58:00+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-06-27 01:58:00+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.42-1.el7 started.
2023-06-27 01:58:00+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following as an environment variable:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
# 에러에 대한 수정사항 반영해서 run 실행
docker run -d --name db2 -e "MYSQL_ROOT_PASSWORD=1234" mysql:5.7
# 파일 가져오기
docker cp web2:/usr/local/apache2/htdocs/index.html /tmp/index.html
Successfully copied 2.05kB to /tmp/index.html
cat /tmp/index.html
<html><body><h1>It works!</h1></body></html>
# 파일 집어넣기
docker cp ./index2.html web2:/usr/local/apache2/htdocs/index2.html
Successfully copied 2.05kB to web2:/usr/local/apache2/htdocs/index2.html
docker diff os2
C /tmp
A /tmp/services
docker diff web2
C /usr
C /usr/local
C /usr/local/apache2
C /usr/local/apache2/logs
A /usr/local/apache2/logs/httpd.pid
C /usr/local/apache2/htdocs
A /usr/local/apache2/htdocs/index2.html
C /root
A /root/.bash_history