Docker 명령어

실행되고 있는 Docker컨테이너를 보고 싶다면

docker ps
docker ps --all

컨테이너가 이유없이 죽거나 하면 로그를 보자

docker-compose logs 서비스명

모든 서비스를 종료하고 싶다면

docker-compose down

사용하지 않고 있는 Docker container를 삭제하고 싶다면

docker container prune

컨테이너 안으로 들어가고 싶다면

# 우선 현재 컨테이너 목록을 보고, 접속할 컨테이너의 이름을 획득합니다
docker ps

# 아래 명령어를 사용하여 획득한 이름의 컨테이너에 접속합니다
docker exec -it 컨테이너이름 bash
docker exec -it 컨테이너이름 sh

# 컨테이너 안에서 필요한 명령어를 실행한다
root@a2018e567fc7:/# mysql -h127.0.0.1 -p
Enter password: ****
mysql> use mydatabase;

docker attach

Shell로 접근

# 컨테이너 실행
$ docker run --name test -d -it debian
Unable to find image 'debian:latest' locally
latest: Pulling from library/debian
e4c3d3e4f7b0: Pull complete
Digest: sha256:8414aa82208bc42...6b8a8c4e7b5c84ca2d04bb244
Status: Downloaded newer image for debian:latest
811ddfb7e514c6cddcfefd14f...2ac6251d45abaf159a8a77c3b9f

# 아이디 획득
$ docker ps
CONTAINER ID    IMAGE    COMMAND    CREATED
811ddfb7e514    debian   ...        1 mins ago

# 아래 명령어를 통해 shell에 접근
$ docker attach 811ddfb7e514
root@811ddfb7e514:/# 

로그가져오기

# docker-compose.yaml에 stdin_open과 tty를 true로 설정한다.
    test_container:
        ...
        stdin_open: true
        tty: true

# 컨테이너 실행
$ docker-compose run test_container

$ docker ps
CONTAINER ID    IMAGE    COMMAND    CREATED
811ddfb7e514    debian   ...        1 mins ago

# 로그보기
docker attach 811ddfb7e514

Debug

debugging tests

# sh모드로 컨테이너 실행
$ docker-compose run --entrypoint sh test_container
/opt/docker/app $

# 디버깅코드 삽입
/opt/docker/app $ vi ./test_cases.py
...
import pdb; pdb.set_trace()
...

# 테스트 실행
/opt/docker/app $ pytest ./test_cases.py

debugging app during the test

it basically attaches your shell to the running process in the container, so if you

  1. increase the value of harakiri in uwsgi.ini
  2. add stdin_open: true and tty: true to the container in docker-compose.yaml
  3. add import pdb; pdb.set_trace() in the code
  4. run the app
  5. execute docker attach with the app container id
  6. run the test container
$ /myapp/uwsgi.ini
harakiri            = 10000000

$ vi docker-compose.yaml
    myapp:
        ...
        stdin_open: true
        tty: true

$ vi /myapp/app.py
...
import pdb; pdb.set_trace()
...

$ docker-compose run myapp

$ docker ps
CONTAINER ID    IMAGE    COMMAND    CREATED
811ddfb7e514    myapp    ...        1 mins ago

$ docker attach 811ddfb7e514

# in another terminal:
$ docker-compose run mytest

when you run the test, the attached container will be stopped by the request.

run tests inside the container

$ docker-compose run --entrypoint sh test_container
/opt/docker/app $ ./run_tests.sh

Container에 없는 Tool은 api-get으로 설치

apt-get update
apt-get install vim