[Docker] Docker Container 조작 총정리
docker 이미지의 인스턴스를 컨테이너라고 한다.
centos 이미지를 설치하고 실행해보자.
1. 도커 이미지 검색 (Search)
#docker 저장소에서 centos 이미지 검색
[minggu92@cloud ~]$ docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 7071 [OK]
centos/systemd systemd enabled base container. 106 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 45
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 43
centos/python-35-centos7 Platform for building and running Python 3.5… 39
centos/php-56-centos7 Platform for building and running PHP 5.6 ap… 34
centos/mysql-56-centos7 MySQL 5.6 SQL database server 22
centos/postgresql-10-centos7 PostgreSQL is an advanced Object-Relational … 19
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 16
kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 16
centos/nodejs-8-centos7 Platform for building and running Node.js 8 … 14
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
centos/mariadb-101-centos7 MariaDB 10.1 SQL database server 13
centos/mongodb-36-centos7 MongoDB NoSQL database server 8
centos/redis-32-centos7 Redis in-memory data structure store, used a… 6
continuumio/centos5_gcc5_base 3
centos/mongodb-34-centos7 MongoDB NoSQL database server 3
centos/ruby-22-centos7 Platform for building and running Ruby 2.2 a… 3
kasmweb/core-centos-7 CentOS 7 base image for Kasm Workspaces 1
bitnami/centos-base-buildpack Centos base compilation image 0 [OK]
bitnami/centos-extras-base 0
datadog/centos-i386 0
ibmcom/fhe-toolkit-centos-amd64 The IBM Fully Homomorphic Encryption (FHE) T… 0
ibmcom/fhe-toolkit-centos The IBM Fully Homomorphic Encryption (FHE) T… 0
2. 도커 이미지 설치 (Pull)
centos라는 이미지를 다운로드하도록 하겠다.
# centos7 이미지 설치
[minggu92@cloud ~]$ docker image pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
# 이미지 목록 확인 (2개 명령어)
[minggu92@cloud ~]$ docker image ls
[minggu92@cloud ~]$ docker imags
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 6 months ago 204MB
3. 도커 명령어 도움말 (docker [Command] --help)
# 도커 container 명령어 확인
[minggu92@cloud ~]$ docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
4. 도커 실행 (container run)
설치한 centos를 기반으로 컨테이너를 실행한다.
#centos 기반으로 도커 컨테이너 단순 실행
[minggu92@cloud ~]$ docker container run centos:7
#컨테이너 부팅 후 즉시 중지 및 종료
#centos 기반으로 도커 컨테이너 실행 (-i -t 명령어를 통해 대화형 컨테이너 상호작용)
# -i : 연결이 종료되어도 컨테이너 상태를 유지한다.
# -t : 가상 tty를 할당한다.
[minggu92@cloud ~]$ docker container run -it centos:7 /bin/bash
[root@ae534c1e498b /]#
#컨테이너 내부에서 작업중임을 표시
#컨테이너에 이름주기 --name
[minggu92@cloud ~]$ docker container run -it --name centos centos:7 /bin/bash
centos 이미지가 올라갔다는 것을 확인했다.
#도커 컨테이너 리스트
[root@ae534c1e498b /]# docker container ls
bash: docker: command not found
현재 작업중인 컨테이너 안에 docker 패키지가 설치되어있지 않기 때문에 확인할 수 없다.
따라서 작업중인 컨테이너 밖으로 나와 확인해야 하는데 나오는 데는 2가지 명령어가 있다.
5. 도커종료 (exit & Ctrl + D)
#도커 컨테이너 종료
[root@ae534c1e498b /]# exit
exit
[minggu92@cloud ~]$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
exit 명령어 혹은 Ctrl + D를 사용하면 컨테이너에서 나옴과 동시에 종료되어버린다.
서버 등을 컨테이너로 운영할 때는 해당 명령어를 사용해선 안된다.
6. 도커에서 빠져나오기 (Ctrl + P, Ctrl + Q)
# 도커 실행
[minggu92@cloud ~]$ docker container run -it centos:7 /bin/bash
[root@dff43ce4ad43 /]#
# Ctrl + P, Ctrl + Q
[root@dff43ce4ad43 /]# [minggu92@cloud ~]$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18ee60980dd4 centos:7 "/bin/bash" About a minute ago Up About a minute dazzling_pasteur
대신 Ctrl + p 를 누르고 한 번 더 Ctrl + q를 누르면 다음과 같이 빠져나오게 된다.
7. 도커 재접속 (attach)
해당 컨테이너에 다시 접속하기 위해선 attach 명령어를 사용해야 한다.
#컨테이너 리스트
[minggu92@cloud ~]$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9c6787a73b7 centos:7 "/bin/bash" 4 minutes ago Up 4 minutes modest_euler
#컨테이너로 재접속
[minggu92@cloud ~]$ docker attach modest_euler
[root@a9c6787a73b7 /]#
처음에 도커 실행할 때 이름을 안붙였더니 랜덤으로 붙여져 버렸다.. 앞으로 실행할 때 --name 명령어를 주도록 하자.
8. 실행중인 도커 컨테이너 정보 확인(ps, ps -a)
실행 중인 컨테이너의 정보를 확인하는 명령어이다. docker container ls는 그만 사용하도록 하자.
#실행중인 컨테이너 정보 확인
[minggu92@cloud ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dff43ce4ad43 centos:7 "/bin/bash" 12 seconds ago Up 10 seconds dazzling_pasteur
#모든 컨테이너 정보 확인
[minggu92@cloud ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fd2ebb9ed22 centos:7 "/bin/bash" 12 seconds ago Up 10 seconds fervent_cannon
18ee60980dd4 centos:7 "/bin/bash" 4 minutes ago Up 4 minutes dazzling_pasteur
4b851f2b5942 centos:7 "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago centos
eaf87b5e96de centos:7 "/bin/bash --name ce…" 6 minutes ago Exited (2) 6 minutes ago busy_joliot
dff43ce4ad43 centos:7 "/bin/bash" 7 minutes ago Up 7 minutes determined_swanson
d75ca0a23119 centos:7 "--name centos" 7 minutes ago Created jovial_margulis
06a0ba9e2c7a centos:7 "/bin/bash" 23 minutes ago Exited (127) 10 minutes ago busy_montalcini
7111edc73953 centos:7 "/bin/bash" 26 minutes ago Exited (0) 25 minutes ago epic_solomon
ae534c1e498b centos:7 "/bin/bash" 18 hours ago Exited (130) 51 minutes ago keen_turing
47df620893a1 centos:7 "/bin/bash" 18 hours ago Exited (0) 18 hours ago nostalgic_nightingale
e8f3482931be centos:7 "/sbin/init" 18 hours ago Created mycentos
9. 도커 컨테이너 전체 종료
일일이 docker rm -f [id or name]로 종료하는 것은 힘들다..
#실행중인 컨테이너 정보 확인
[minggu92@cloud ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fd2ebb9ed22 centos:7 "/bin/bash" 12 seconds ago Up 10 seconds fervent_cannon
18ee60980dd4 centos:7 "/bin/bash" 4 minutes ago Up 4 minutes dazzling_pasteur
4b851f2b5942 centos:7 "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago centos
eaf87b5e96de centos:7 "/bin/bash --name ce…" 6 minutes ago Exited (2) 6 minutes ago busy_joliot
dff43ce4ad43 centos:7 "/bin/bash" 7 minutes ago Up 7 minutes determined_swanson
d75ca0a23119 centos:7 "--name centos" 7 minutes ago Created jovial_margulis
06a0ba9e2c7a centos:7 "/bin/bash" 23 minutes ago Exited (127) 10 minutes ago busy_montalcini
7111edc73953 centos:7 "/bin/bash" 26 minutes ago Exited (0) 25 minutes ago epic_solomon
ae534c1e498b centos:7 "/bin/bash" 18 hours ago Exited (130) 51 minutes ago keen_turing
47df620893a1 centos:7 "/bin/bash" 18 hours ago Exited (0) 18 hours ago nostalgic_nightingale
e8f3482931be centos:7 "/sbin/init" 18 hours ago Created mycentos
#실행중인 컨테이너의 id 출력
[minggu92@cloud ~]$ docker ps -qa
1fd2ebb9ed22
18ee60980dd4
4b851f2b5942
eaf87b5e96de
dff43ce4ad43
d75ca0a23119
06a0ba9e2c7a
7111edc73953
ae534c1e498b
47df620893a1
e8f3482931be
#실행중인 컨테이너의 전체 종료
[minggu92@cloud ~]$ docker rm -f $(docker ps -qa)
1fd2ebb9ed22
18ee60980dd4
4b851f2b5942
eaf87b5e96de
dff43ce4ad43
d75ca0a23119
06a0ba9e2c7a
7111edc73953
ae534c1e498b
47df620893a1
e8f3482931be
[minggu92@cloud ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
다음과 같이 id만 출력해서 전체 종료하면 된다!
10. 도커 이미지 전체 삭제
[minggu92@cloud ~]$ docker rmi $(docker images -q)
Untagged: dockerfile_test:1.0
Deleted: sha256:e0bbe11b329d52fe67f00403092db84c60f7bc573fd64c1331f740941a4bc5e4
Deleted: sha256:240fcd39446274c563bcb26c1128c090e23752c14806da8fe6cc25b83ef561b2
Deleted: sha256:5671377616231efc514334b12834625e54c83eda10188a6a16a22c423899efa2
Deleted: sha256:f11428b155dcf4ec925f5bb96acbcb4d31da4e9a86e451562c0ab38e37d42389
Deleted: sha256:e79cc724f45ee7abb885d83c3757cb584a12a78c44e1d69dff5228e7b8b66b5e
Deleted: sha256:74d4f0e1745558de7aabb077e6ca2641597f2e875107fceeebd7d17c1ca80e79
Deleted: sha256:70c69974ebf305ea72ef45ab756c6f67b439019b8e258788a1bf3f340108c9f4
Deleted: sha256:c77acf31d099150c2f0910abe7c3cc49b31a8250564054d26067b06169cf8f1f
Deleted: sha256:9b357fad30410757e9e5fe18fdeff8dbdd5dddc6f6ce37c8dc608cff068f441a
Deleted: sha256:be66b57b1f3eaeffb7dc9dc6bb46899e92777890f363fecfd312b63803b42401
Deleted: sha256:b8b55b883bc22e2bfa00034fb11e9fb467ff7fdb43d5ac3a8d81d59484f2dabc
Deleted: sha256:c6e1213d14ee8ee4217ceec66075776220f4e6cd29b2fa1932d4ccedf62fc197
Deleted: sha256:a87d026ea9857609f7013de57eb5c0feadecf030312a3ae30602f1cb53d36667
Deleted: sha256:f5976c28d4f1153f8cffbc5906e2aa517afdc2261b42bedaf48e58a2aa71d7d2
Deleted: sha256:35706d3558d05682726d2a8175612860cefffc990b335c6bcdc1294dcabc10a3
Deleted: sha256:24c21579814de017459b978fb4e451a2709564f89c2aa9d4da038c23337810f2
Deleted: sha256:56d90323f3e0b63c5ce1d0463491ecf559fbbd18b4632344a47b20668325fa0e
Deleted: sha256:51e45727c5456f0ab3513a9a37fec9c92adcd7a764617c8708e53fb9a4c01270
Deleted: sha256:8ca186d18d398798727e229581c76b29f23b51c7aa25be4f22b37f5783e839c3
Deleted: sha256:8f6de4c62c25d2422a42c6ddd2441cfb2bc70e6d50c65d4ab1055202deab3746
Deleted: sha256:cd5a354a89da8fbf2bc859ffeecd009eac84c0e33154290a711adddba3cc0ad2
Deleted: sha256:6c6b34e6fa49f7c85da4fb65c2da122e3ae7303f3a11f5fdf145a1da38a6d81e
Deleted: sha256:2373d1fa866e22a74e9c2e3df6b6252e84ffdd44004383d487e6ce191bf9d3d6
Deleted: sha256:f212aa808615b46cecaf935f70707cb322672186b563d2f6a6eac2867deda2d6
Deleted: sha256:c4287f4bbc58587464860cd918acc6e64c7e562ed67e2c18acffa7d58dc6c4d0
Deleted: sha256:e52c6a7d13d1ec0906a867996a9ca0d9761e50092fcb7d85787ef0bf7f5885ff
Deleted: sha256:b16159c29ca8b09d5a4d824dc161fe62b4ca29fa351cd0d649eea5d916597660
Deleted: sha256:485febe889b8439ff590371ebfec241abeb97d0c318caec38354e3b3313a8609
Deleted: sha256:579d15867fc76e356e153addf39c9c7f174aad68583f2633ba5bd46655cd60bf
Untagged: registry:latest
Untagged: registry@sha256:65be6503496c34ec234e89a831ca248b18c2e04c800d9d74af73866e3cda8578
Deleted: sha256:946ebb70ae89ce3c5dae268db9c750473680a2e691b0a42cc8fed0b111f584b1
Deleted: sha256:c1adcdc6a82c4e2393c543d56642ef141e061df63b34a27a1cced9e67ecdbc30
Deleted: sha256:596b46ea745f11517772b2921ac3d47b5c291b641b0806bb17a83826f86fb892
Deleted: sha256:3032a0bb355065d319a6f64eebfb84310c1c75876334c23bec19a4ebf98ae145
Deleted: sha256:18ef7a24afbf82d71b696f53e3bdb8b8c97f32cf693c57515ac9e24b7367e2d3
Deleted: sha256:5e03d8cae8773cb694fff1d55da34a40d23c2349087ed15ce68476395d33753c
Untagged: centos:7
Untagged: centos@sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407
Deleted: sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9
Deleted: sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02
마찬가지로 이미지도 전체 삭제 할 수 있다.
'Server & Infra' 카테고리의 다른 글
[Docker] docker container commit, push (0) | 2022.03.21 |
---|---|
[Docker] docker container ssh 설치 및 접속 (0) | 2022.03.17 |
[Docker] docker 이미지 삭제시 에러 발생할 때 (0) | 2022.03.17 |
[Docker] sudo 권한 없이 Docker 실행 (0) | 2022.03.16 |
[Docker] yum 명령어를 통한 Docker 설치방법 (Cloud 환경) (0) | 2022.03.11 |