Docker
Install
Basic keywords
docker ps
docker info
docker images
docker version
docker run hello-world
- docker : 시스템에 있는 docker 사용
- run : 서브명령, 컨테이너 실행
- hello-world : 컨테이너에 실을 이미지 이름
- 컨테이너는 아무것도 꾸미지 않은 버전의 리눅스 운영체제
- 고래가라사대
docker run -d -p 80:80 --name webserver nginx
- stop
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92d58318f84e nginx "nginx -g 'daemon off" 27 hours ago Up 27 hours 0.0.0.0:80->80/tcp, 443/tcp webserver
0235bd537f03 nginx "nginx -g 'daemon off" 27 hours ago Up 27 hours 80/tcp, 443/tcp boring_hypatia
➜ ~ docker stop nginx
Error response from daemon: No such container: nginx
➜ ~ docker stop 92d58318f84e
92d58318f84e
실행중인 도커 접속
docker exec -it 92d58318f84e /bin/bash
이미지 불러오기
컨테이너 전체 삭제
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
이미지 만들기
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Flask
Redis
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
docker build -t friendlyhello .
docker run -p 4000:80 friendlyhello
curl localhost:4000
docker tag friendlyhello kenu/get-started:part2
이미지 업로드
docker push kenu/get-started:part2
원격 이미지 로컬에서 실행
docker run -p 4000:80 kenu/get-started:part2
curl localhost:4000
업로드 이미지 삭제
export USERNAME=myuser
export PASSWORD=mypass
export ORGANIZATION=myorg (if it's personal, then it's your username)
export REPOSITORY=myrepo
export TAG=latest
curl -u $USERNAME:$PASSWORD -X "DELETE" https://cloud.docker.com/v2/repositories/$ORGANIZATION/$REPOSITORY/tags/$TAG/
네트워크
AWS EC2
sudo yum update -y
sudo yum install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
# for ec2-user permission
sudo reboot
docker ps
Cases
- copy file
docker cp d362659da5fc:/opt/app/app.log .
- d362659da5fc: container id
참고
docker ps
docker images
docker images ubuntu
docker run -i -t ubuntu:12.10 /bin/bash
ps faxw
ls
rm -rf /var /usr /lib
ls /var
exit
ssh dockerdev
sudo -s
docker ps
docker diff 7b882b11bc8e
docker commit 7b882b11bc8e shykes/broken-ubuntu
docker run -i -t shykes/broken-ubuntu /bin/bash
docker push shykes/broken-ubuntu
https://index.docker.io
ref
deprecated
brew install docker docker-compose docker-machine xhyve docker-machine-driver-xhyve
sudo chown root:wheel /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
sudo chmod u+s /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
sudo chown root:wheel $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve\nsudo chmod u+s $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
docker-machine create default --driver xhyve --xhyve-experimental-nfs-share
eval $(docker-machine env default)
docker run hello-world