[Docker] docker commands

실행 중인 docker process (machine) 보기

docker ps

실행 중 + 실행 중이 아닌 docker process 보기

docker ps -a

docker 이미지 보기

docker images

docker process 삭제

docker rm -rf [container name]

docker 이미지 삭제

docker rmi [image name]

docker 이미지 가져오기

docker pull [image name]

docker process 시작

docker start [container name]

docker process 중지

docker stop [container name]

실행 중인 모든 컨테이너 강제 종료

docker kill $(docker ps -q -f status=running)

실행 중인 모든 컨테이너 중지

docker stop $(docker ps -q -f status=running)

종료된 모든 컨테이너 삭제

docker rm $(docker ps -q -f status=exited)

시스템 상의 모든 이미지 삭제

docker image rm -f $(docker image ls -q)

docker 로그 보기

docker logs [container name]

docker network 삭제

docker network prune -f

alias해서 간편히 사용하기

alias dockerkill='docker kill $(docker ps -q -f status=running)'
alias dockerstop='docker stop $(docker ps -q -f status=running)'
alias dockerrm='docker rm $(docker ps -q -f status=exited)'
alias dockerimgrm='docker image rm -f $(docker image ls -q)'
alias dockernetprune='docker network prune -f'

docker cli 실행

$ docker exec -it cli /bin/bash

[OpenSSL] command line 명령으로 TLS 서버 실행하기

기본적으로 s_server 인자와 -accept 를 사용한다. cert파일과 key파일은 자체 인증서를 생성하든지, letsencrypt에서 얻든지 한다. https에 사용되는 인증서를 letsencrypt로부터 얻는 방법은 [TLS] Letsencrypt로부터 https 사용을 위한 인증서 얻기를 참고한다.

$ openssl s_server -accept 443 -key /etc/letsencrypt/live/blog.dasomoli.org/privkey.pem -cert /etc/letsencrypt/live/blog.dasomoli.org/fullchain.pem

-msg 를 사용하면 TLS 관련 메시지를 볼 수 있다. decrypted된 text만 보고 싶다면 -quiet 옵션을 주면 된다. stdout으로 출력된다.

[TLS] Letsencrypt로부터 https 사용을 위한 인증서 얻기

80 포트나 443 포트로 현재 서비스 중인 것이 있다면 멈춘다.

# service apache2 stop

certbot을 이용해서 사용할 도메인의 인증서를 얻는다.

# certbot certonly --standalone -d blog.dasomoli.org

얻어온 인증서는 /etc/letsencrypt/live/도메인명/ 아래에 저장된다. 위의 경우는 /etc/letsencrypt/live/blog.dasomoli.org/ 아래에 저장된다. key 파일은 privkey.pem, cert 파일은 fullchain.pem이 주로 사용된다.

[Python] matplotlib.pyplot 으로 그래프 그리기

import 부터 하자.

import matplotlib.pyplot as pyplot

하나는 x 축, 다른 하나는 y 축으로 리스트 두개를 가지고 그린다. 예를 보자. scatter()를 썼다.

names = ['Bamtol', 'Mong', 'Justin', 'Jay']
bomb = [8, 1, 12, 10]
pyplot.title('Super Mario Cart 8 Deluxe Score')
pyplot.xlabel('Name')
pyplot.ylabel('Score')
pyplot.scatter(names, bomb, color='green')
pyplot.show()

위의 그래프는 막대 그래프가 더 나아보인다. bar()로 막대 그래프를 그래보자. 각각의 색도 바꿔보자.

names = ['Bamtol', 'Mong', 'Justin', 'Jay']
bomb = [8, 1, 12, 10]
pyplot.title('Super Mario Cart 8 Deluxe Score')
pyplot.xlabel('Name')
pyplot.ylabel('Score')
pyplot.bar(names, bomb, color=[ 'blue', 'red', 'gray', 'green' ])
pyplot.show()

barh()를 쓰면 누은 그래프가 나온다. xlabel()과 ylabel() 바꿔주자.

names = ['Bamtol', 'Mong', 'Justin', 'Jay']
bomb = [8, 1, 12, 10]
pyplot.title('Super Mario Cart 8 Deluxe Score')
pyplot.xlabel('Score')
pyplot.ylabel('Name')
pyplot.barh(names, bomb, color=[ 'blue', 'red', 'gray', 'green' ])
pyplot.show()

plot()을 쓰면 선 그래프가 나온다.

weigh = [ 3.26, 4.5, 5.6, 7.2, 8.9, 10.4, 12.6, 14.2, 16.1, 17.4, 16.8, 16.3, 16.5, 17.0]
pyplot.plot(weigh)
pyplot.title("Bamtol's weigh")
pyplot.xlabel('Days')
pyplot.ylabel('Kg')
pyplot.show()

그려진 그래프의 시작이 1이 아닌 0 부터임에 주의하자. 이걸 1부터로 바꾸고, x 축 찍는 위치도 바꾸고, 마커도 넣어보자.

weigh = [ 3.26, 4.5, 5.6, 7.2, 8.9, 10.4, 12.6, 14.2, 16.1, 17.4, 16.8, 16.3, 16.5, 17.0]
axis_x = list(range(1, 15))
pyplot.title("Bamtol's weigh")
pyplot.xlabel('Days')
pyplot.ylabel('Kg')
pyplot.plot(axis_x, weigh, marker = '.', color = 'green')
pyplot.xticks([1, 4, 7, 10, 13])
pyplot.show()

파이 차트를 그릴 때는 pie()를 이용한다.

names = ['Bamtol', 'Mong', 'Justin', 'Jay']
bomb = [8, 1, 12, 10]
pyplot.title('Results: Score', fontdict={'fontsize': 24})
pyplot.pie(bomb, labels=names, autopct='%1.1f%%', colors=['lightblue','pink','yellow','lightgreen'])

[Python] sorted() 설명

sorted() 함수는 iterable로 정렬한 list를 만든다. doc string을 보자.

Return a new list containing all items from the iterable in ascending order.

A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.

설명에서 보이는대로 key로 function을 받아 그 function의 기준에 따라 정렬 가능하다. 순서를 작아지는 순서로 하고 싶다면, reverse 를 True로 주면 된다. 예를 들면 다음과 같다.

names = [ 'Bamtol', 'Mong', 'Justin', 'Jay']
sorted(names)
['Bamtol', 'Jay', 'Justin', 'Mong']

key를 len()으로 주고, 작아지는 순서대로 해보자.

sorted(names, key=len, reverse=True)
['Bamtol', 'Justin', 'Mong', 'Jay']

[Python] lambda 함수 설명

lambda 함수는 코드를 막 작성하던 중에 그 코드 내에 간단한 함수를 만들 때 흔히 사용한다. 다음과 같은 형식이다.

lambda x, y: x + y

filter나 map 내에서 간단한 함수를 작성한다던가 할 때 유용하게 쓰인다. 예를 들어 0 ~ 999 사이의 값 중 2나 3의 배수들의 합을 구하고 싶으면 다음과 같이 하면 된다.

sum(filter(lambda x: x % 2 == 0 or x % 3 == 0, range(1000)))
333167

[Python] filter() 설명

filter() 함수는 iterables 안의 어떤 조건(함수)에 맞는 것만 골라내는 함수다. doc string을 보자.

filter(function or None, iterable) –> filter object

Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.

어떤 iterables내의 item들 중 function에 참인 것만 골라서 iterator를 만든다. function에 None을 주면 true인 item들만 고른다. 예를 들면 다음과 같다.

names = [ 'Bamtol', 'Mong', 'Justin', 'Jay']
list(filter(lambda x: len(x) == 6, names))
['Bamtol', 'Justin']

[Python] map() 설명

map() 함수의 doc string부터 보자.

map(func, *iterables) –> map object

Make an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted.

설명 그대로 func 함수를 iterables내의 각 argument에 적용한 iterator를 만든다. 예를 들면 다음과 같다. 일부러 for loop를 돌렸다. 쓰인 len이 length를 return하는 len()임에 주의하자.

names = [ 'Bamtol', 'Mong', 'Justin', 'Jay' ]
for length in map(len, names):
    print(length)
6
4
6
3

iterator를 return하므로 list로 만들고 싶다면 list로 변환한다.

list(map(len, names))
[6, 4, 6, 3]