잘못된 정보가 있다면, 꼭 댓글로 알려주세요(비로그인 익명도 가능).
여러분의 피드백이 저와 방문자 모두를 올바른 정보로 인도할 수 있습니다.
감사합니다. -현록
현록의 기록저장소
Docker 실행 본문
[Docker 실행]
$ sudo docker run [옵션...] 컨테이너명이나ID 명령어 [명령어의매개변수...] |
명령어를 생략하면, 이미지에서 기본으로 실행되도록 지정된 명령어가 실행됨
$ sudo docker run -d -p 53306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=true --name mymysql mysql |
ㆍ-d: detached 상태(백그라운드)로 실행.
ㆍ-p: 포트 연결. ex) 호스트의 53306 포트를 컨테이너의 3306 포트와 연결
ㆍ-e: 컨테이너에 환경변수 적용.
ㆍ--name: 컨테이너의 이름을 지정. (지정하지 않을 경우 랜덤으로 생성)
1feea090d373769784c44cfc8a9818813cc2b6acddc0337914cc29512bbd9f5a |
(응답으로 출력되는건 컨테이너ID)
$ sudo docker run -it --name myubuntu ubuntu /bin/bash |
ㆍ-it: i와 t를 같이 사용. 터미널 입력을 위한 옵션.
root@16321ed168f3:/# ls -al total 0 drwxr-xr-x. 1 root root 6 Apr 1 08:41 . drwxr-xr-x. 1 root root 6 Apr 1 08:41 .. -rwxr-xr-x. 1 root root 0 Apr 1 08:41 .dockerenv lrwxrwxrwx. 1 root root 7 Mar 25 16:58 bin -> usr/bin drwxr-xr-x. 2 root root 6 Apr 15 2020 boot drwxr-xr-x. 5 root root 360 Apr 1 08:41 dev drwxr-xr-x. 1 root root 66 Apr 1 08:41 etc drwxr-xr-x. 2 root root 6 Apr 15 2020 home lrwxrwxrwx. 1 root root 7 Mar 25 16:58 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Mar 25 16:58 lib32 -> usr/lib32 lrwxrwxrwx. 1 root root 9 Mar 25 16:58 lib64 -> usr/lib64 lrwxrwxrwx. 1 root root 10 Mar 25 16:58 libx32 -> usr/libx32 drwxr-xr-x. 2 root root 6 Mar 25 16:58 media drwxr-xr-x. 2 root root 6 Mar 25 16:58 mnt drwxr-xr-x. 2 root root 6 Mar 25 16:58 opt dr-xr-xr-x. 118 root root 0 Apr 1 08:41 proc drwx------. 2 root root 37 Mar 25 17:08 root drwxr-xr-x. 1 root root 21 Mar 25 22:33 run lrwxrwxrwx. 1 root root 8 Mar 25 16:58 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Mar 25 16:58 srv dr-xr-xr-x. 13 root root 0 Mar 30 11:32 sys drwxrwxrwt. 2 root root 6 Mar 25 17:08 tmp drwxr-xr-x. 1 root root 18 Mar 25 16:58 usr drwxr-xr-x. 1 root root 17 Mar 25 17:08 var root@16321ed168f3:/# exit exit |
$ sudo docker run -d --name myubuntu ubuntu /bin/bash -c "while true; do echo "still live"; sleep 100; done" |
-it에서 exit를 하면 bash쉘이 종료되며,
bash 쉘이 계속 백그라운드에서 실행되어 살아있도록 하고 싶다면 위처럼 할 수도 있고... (추천하지는 않음)
$ sudo docker run -d -it --name myubuntu ubuntu /bin/bash |
4d019c6739448b035e7dffd4d227dd0c9f11d2a5809fc3d6d4313918342516cc |
$ sudo docker ps |
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4d019c673944 ubuntu "/bin/bash" About a minute ago Up 3 seconds myubuntu |
$ sudo docker attach myubuntu |
^PQ |
$ sudo docker ps |
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4d019c673944 ubuntu "/bin/bash" About a minute ago Up 18 seconds myubuntu |
아니면 bash에서 ^PQ로 빠져나오면 백그라운드에 유지시킬 수 있다.
(이걸 다시 접속하려면 아래의 attach. 여기서 다시 종료하려면 exit, 유지하려면 ^PQ)
옵션 |
설명 |
-d |
detached mode로 실행. |
-p |
호스트와 컨테이너간 포트포워딩. -p 호스트포트:컨테이너포트 |
-v |
호스트와 컨테이너간 디렉토리 마운팅. (마운팅은 여기에서 자세히) |
-e |
컨테이너에 환경변수 적용. |
--name |
컨테이너 이름 지정. (지정하지 않으면 랜덤) |
-rm |
컨테이너 종료시 컨테이너 자동 삭제 |
-it |
i와 t를 같이 사용. 터미널 입력을 위한 옵션. |
--link |
컨테이너 연결 |
그 외 더 많은 옵션: https://docs.docker.com/engine/reference/run/
[컨테이너 상세정보 확인]
$ sudo docker inspect 컨테이너명 |
[ { "Id": "1feea090d373769784c44cfc8a9818813cc2b6acddc0337914cc29512bbd9f5a", "Created": "2021-04-01T08:08:42.852214414Z", "Path": "docker-entrypoint.sh", "Args": [ "mysqld" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 78151, "ExitCode": 0, "Error": "", "StartedAt": "2021-04-01T08:08:43.483800908Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:e646c6533b0bcb75743ea9b176a03012610b2df7072dc312bf5921d1fbc5149c", "ResolvConfPath": "/var/lib/docker/containers/1feea090d373769784c44cfc8a9818813cc2b6acddc0337914cc29512bbd9f5a/resolv.conf", "HostnamePath": "/var/lib/docker/containers/1feea090d373769784c44cfc8a9818813cc2b6acddc0337914cc29512bbd9f5a/hostname", "HostsPath": "/var/lib/docker/containers/1feea090d373769784c44cfc8a9818813cc2b6acddc0337914cc29512bbd9f5a/hosts", "LogPath": "/var/lib/docker/containers/1feea090d373769784c44cfc8a9818813cc2b6acddc0337914cc29512bbd9f5a/1feea090d373769784c44cfc8a9818813cc2b6acddc0337914cc29512bbd9f5a-json.log", "Name": "/mymysql", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "3306/tcp": [ { "HostIp": "", "HostPort": "53306" } ] }, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "host", "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/2c62f7f95d3948588a49111d225062a8b483566c6239ce4351ba908a2ae963d1-init/diff:/var/lib/docker/overlay2/c4ef60fb2e8044cf63578f0dfafc4d66e7b347ba760f3d00fa0ee0331b0611a4/diff:/var/lib/docker/overlay2/7035f5b14f80555c7e07c4f91b6a95dc912ab9f5029f2176e4d72bca88e35c5b/diff:/var/lib/docker/overlay2/b2d7740db7f2328a37bf230dfad270c69dfb65ab020f76ebedb06626295d00b1/diff:/var/lib/docker/overlay2/89c35bcf0557e45a501ad7ce8738b99702902b135366846679d62dda587bf275/diff:/var/lib/docker/overlay2/e4f2cafd451665ed033e75962854946b2342fbbfe1e3133cdead96929dbd065a/diff:/var/lib/docker/overlay2/d90d3854be59a9b2ef3478ba69ffb77a8b5ac998dea414d4c61160548f8ce2bc/diff:/var/lib/docker/overlay2/140ebeaaec8ee2f93ae7fdbfc98ba7197ecc356bad28bfbd776fb7845b6f0e87/diff:/var/lib/docker/overlay2/fc3fb83fe0b34daf8d696f09690b85a1296712243df75b23c06d3af7490124ba/diff:/var/lib/docker/overlay2/6ea6d62bfc9526486dbf81e49759e6e97dbe91e7693b3ada050de6c969dd3a72/diff:/var/lib/docker/overlay2/c914c35e89576c7af32e903b8e864a1b2c3ce3acf16a8c5d25b4e5843270bd3e/diff:/var/lib/docker/overlay2/46088c92010372d0b6a3092afcaf199684043e02b6a7b572b49d847c195fbe9c/diff:/var/lib/docker/overlay2/25a5fb0f54e240cadd062cc95068dde748c96296b991a07c36290af6df255066/diff", "MergedDir": "/var/lib/docker/overlay2/2c62f7f95d3948588a49111d225062a8b483566c6239ce4351ba908a2ae963d1/merged", "UpperDir": "/var/lib/docker/overlay2/2c62f7f95d3948588a49111d225062a8b483566c6239ce4351ba908a2ae963d1/diff", "WorkDir": "/var/lib/docker/overlay2/2c62f7f95d3948588a49111d225062a8b483566c6239ce4351ba908a2ae963d1/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "a63fd7d115aca8244b31f97785f62d3742cc2570489205c30284621d0103c2c8", "Source": "/var/lib/docker/volumes/a63fd7d115aca8244b31f97785f62d3742cc2570489205c30284621d0103c2c8/_data", "Destination": "/var/lib/mysql", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "1feea090d373", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "3306/tcp": {}, "33060/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "MYSQL_ALLOW_EMPTY_PASSWORD=true", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.12", "MYSQL_MAJOR=8.0", "MYSQL_VERSION=8.0.23-1debian10" ], "Cmd": [ "mysqld" ], "Image": "mysql", "Volumes": { "/var/lib/mysql": {} }, "WorkingDir": "", "Entrypoint": [ "docker-entrypoint.sh" ], "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "93ec920d64811694699ab3b83c5db3f2fab94b43266e1f3964a0060904c6fb73", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "3306/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "53306" } ], "33060/tcp": null }, "SandboxKey": "/var/run/docker/netns/93ec920d6481", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "7f3ac9ec4ede57ae69b0fe1b6f51de4fc61f970cacc1c83056a96de9aa8a772f", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "8b89b0f653b69cc0359b7c7e2a3ff1d4f14918d670818c410595a6a853a48503", "EndpointID": "7f3ac9ec4ede57ae69b0fe1b6f51de4fc61f970cacc1c83056a96de9aa8a772f", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] |
[컨테이너 조회]
$ sudo docker ps |
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1feea090d373 mysql "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 33060/tcp, 0.0.0.0:53306->3306/tcp mymysql |
중지된 컨테이너까지 모두 조회하려면 -a(--all) 옵션
$ sudo docker ps -a |
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1feea090d373 mysql "docker-entrypoint.s…" 12 minutes ago Exited (0) 5 seconds ago mymysql |
중지된 컨테이너는 STATUS에 Exited라고 표시됨
[컨테이너 중지]
$ sudo docker stop 컨테이너명이나ID |
[컨테이너 시작]
$ sudo docker start 컨테이너명이나ID |
[컨테이너 재시작]
$ sudo docker restart 컨테이너명이나ID |
[컨테이너 삭제]
$ sudo docker rm 컨테이너명이나ID [컨테이너명이나ID...] |
[실행 중인 컨테이너에 접속]
$ sudo docker attach 컨테이너명이나ID |
실행 중인 컨테이너에 접속하는 것.
즉, ps에서 실행 중인 COMMAND에 그대로 붙는 것.
새로운 프로세스(쉘 등)를 붙여서 실행하고 싶다면, attach가 아닌 exec를 해야한다.
$ sudo docker run -d -it --name myubuntu ubuntu /bin/bash |
4d019c6739448b035e7dffd4d227dd0c9f11d2a5809fc3d6d4313918342516cc |
$ sudo docker ps |
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4d019c673944 ubuntu "/bin/bash" About a minute ago Up 3 seconds myubuntu |
$ sudo docker attach myubuntu |
root@4d019c673944:/# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 07:27 pts/0 00:00:00 /bin/bash root 9 1 0 07:28 pts/0 00:00:00 ps -ef root@4d019c673944:/# exit exit |
[실행 중인 컨테이너에 새 프로세스로 접속]
$ sudo docker exec [옵션...] 컨테이너명이나ID 명령어 [명령어의매개변수...] |
run과 형태가 같음
$ sudo docker run -d -it --name myubuntu ubuntu /bin/bash |
1a213b84827f921d1c28e9fc6ab1f9c3225c2b9df8c704d5e6ccb946361a1337 |
$ sudo docker ps |
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a213b84827f ubuntu "/bin/bash" 3 seconds ago Up 3 seconds myubuntu |
$ sudo docker exec -it myubuntu /bin/bash |
root@1a213b84827f:/# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 07:27 pts/0 00:00:00 /bin/bash root 9 0 0 07:28 pts/1 00:00:00 /bin/bash root 17 9 0 07:28 pts/1 00:00:00 ps -ef root@1a213b84827f:/# exit exit |
컨테이너 run의 /bin/bash의 PID는 1이고,
새롭게 exec한 /bin/bash의 PID는 9인 예시.
$ sudo docker exec -it mymysql /bin/bash |
root@4a656fc2237d:/# ps -ef bash: ps: command not found root@4a656fc2237d:/# /usr/bin/mysql... mysql mysql_ssl_rsa_setup mysqladmin mysqld_multi mysqldumpslow mysqlshow mysql_config_editor mysql_tzinfo_to_sql mysqlbinlog mysqld_safe mysqlimport mysqlslap mysql_secure_installation mysql_upgrade mysqlcheck mysqldump mysqlpump root@4a656fc2237d:/# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)
mysql> exit Bye root@4a656fc2237d:/# exit exit |
mymysql 컨테이너에 /bin/bash로 새로운 쉘로 접근하여, 이 쉘에서 mysql을 실행
$ sudo docker exec -it mymysql mysql -uroot |
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
mysql> exit Bye |
mymysql 컨테이너에서 바로 mysql(컨테이너의 /usr/bin/mysql)을 새로운 프로세스로 실행
[컨테이너 로그 보기]
$ sudo docker logs [옵션] 컨테이너명이나ID |
ㆍ옵션없이: 모든 로그 전체를 출력
ㆍ-f(--follow): 로그를 실시간으로 출력(^C로 빠져나옴)
ㆍ-n(--tail) 줄수: 줄수 만큼의 최근 로그를 출력
$ sudo docker logs -n 10 mymysql |
2021-04-07 02:49:40+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
2021-04-07T02:49:40.382044Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1 2021-04-07T02:49:40.391804Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2021-04-07T02:49:40.806032Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2021-04-07T02:49:40.936441Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock 2021-04-07T02:49:41.114434Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. 2021-04-07T02:49:41.114570Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel. 2021-04-07T02:49:41.116108Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. 2021-04-07T02:49:41.131399Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL. |
'Study > Docker' 카테고리의 다른 글
Dockerfile 작성 (Dockerfile로 배포) (0) | 2023.01.03 |
---|---|
Docker 마운팅 (0) | 2021.04.02 |
Docker 이미지들 (Docker Hub) (0) | 2021.03.31 |
Docker 설치 (0) | 2021.03.31 |
도커 자료 (외부 링크, 백업) (0) | 2021.03.31 |
잘못된 정보가 있다면, 꼭 댓글로 알려주세요(비로그인 익명도 가능).
여러분의 피드백이 저와 방문자 모두를 올바른 정보로 인도할 수 있습니다.
감사합니다. -현록