[IBM Cloud] Docker Essential

2021. 1. 21. 09:01IBM C:LOUDERs

728x90

  • cgroup : control groups


Docker File

FROM python:3 //알파인 리눅스에서 파이썬 3 환경을 사용해라.

WORKDIR /usr/src/app //Docker 이미지 있는 곳

## Install packages 
COPY requirements.txt ./ //requirements.txt를 Docker 이미지 있는 위치(./)
RUN pip install -r requirements.txt 

## Copy all src files 
COPY . . 

## Run tests 
RUN apt update; apt install default-jdk -y //실행
#RUN python all_tests.py STAGING

## Run the application on the port 8080 
EXPOSE 8000 //포트 8000번 열기

## Config settings 
#ENV ENV='STAGING' DATABASE='trueshort_staging' DB_HOST='staging-mysql.cj5v1k6zfree.ap-northeast-2.rds.amazonaws.com' 

#CMD ["python", "./setup.py", "runserver", "--host=0.0.0.0", "-p 8080"] 
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "whattowear.wsgi:application"] 
//CMD부터는 이미지 파일 생성후에 작동하는 코드임.

 

Run a container

docker run -t ubuntu top
  • -t flag : Allocates a pseudo-TTY
  • run을 시작하면 top 라는 프로세스를 pull download 를 실행합니다.

 

Container ID 확인

$ docker container list 
CONTAINER ID IMAGE		COMMAND				 CREATED		     STATUS			  PORTS 							 NAMES 
2689acf7e190 mysql:5.7 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql

 

실행

$ docker container exec - it [ContainerID] [생성할자식프로세스]

 

Run multiple containers

docker container run --detach --publish 8080:80 --name nginx nginx
  • -d : detach - run container in the background (demon)
  • -p : publish 
    • 80 : publish 할 container의 port는 80
    • 8080 : on your host

 

Remove : remove containers that are already stopped

 $ docker system prune.

Create Docker image

docker build -f Dockerfile

cat dockerfile

From ubuntu
Add myapp /
Expose 80
Entrypoint /myapp

  • 도커는 이미지를 통째로 생성하지 않고, 바뀐 부분만 생성한 뒤 부모 이미지를 계속 참조하는 방식으로 동작하는데, 이것을 레이어 라고 부릅니다
  • 이미지는 여러개의 RO Layer로 구성되고, 파일이 변경되면 새로운 Layer가 생깁니다.

FROM python:3.6.1-alpine
RUN pip install flask
CMD ["python","app2.py"]
COPY app2.py /app2.py


Create your first swarm

$ docker swarm init --advertise-addr eth0
Swarm initialized: current node (vq7xx5j4dpe04rgwwm5ur63ce) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join \
    --token SWMTKN-1-50qba7hmo5exuapkmrj6jki8knfvinceo68xjmh322y7c8f0pj-87mjqjho30uue43oqbhhthjui \
    10.0.120.3:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
7x9s8baa79l29zdsx95i1tfjp     node3               Ready               Active
x223z25t7y7o4np3uq45d49br     node2               Ready               Active
zdqbsoxa6x1bubg3jyjdmrnrn *   node1               Ready               Active              Leader

Deploy your first service

$ docker service create --detach=true --name nginx1 --publish 80:80  --mount source=/etc/hostname,target=/usr/share/nginx/html/index.html,type=bind,ro nginx:1.12
pgqdxr41dpy8qwkn6qm7vke0q
$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
pgqdxr41dpy8        nginx1              replicated          1/1                 nginx:1.12          *:80->80/tcp

 Scale your service

$ docker service update --replicas=5 --detach=true nginx1
nginx1
$ docker service logs nginx1

Apply rolling updates

$ docker service update --image nginx:1.13 --detach=true nginx1
  • service update 명령어 사용

Reconcile problems with containers

$ watch -n 1 docker service ps nginx2