Docker:S1
(تغییرمسیر از سناریوی کار با Docker)
پرش به ناوبری
پرش به جستجو
O) Installing Docker on Ubuntu:
$ sudo apt-get install -y docker.io $ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker $ sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io - Verify Installation: $ sudo docker version
O) Docker Hub and Containers
- The Docker command: Usage: [sudo] docker [flags] [command] [arguments] - To list all the commands: $ sudo docker
- To view the usage sample: Usage: sudo docker [command] - create an account from the command line: $ sudo docker login
- Display Docker Images: $ sudo docker images
- Searching images from Docker command line: Search Syntax: Docker search [image name]
- Launching Docker containers: $ sudo docker run ubuntu:14.04 /bin/echo â??Hello worldâ?? - Launch On Interactive Mode: $ sudo docker run -t -i ubuntu:14.04 /bin/bash
- Creating a daemonized container: $ sudo docker run -d ubuntu:14.04 /bin/sh â??c â??while true; do sleep 4; date; doneâ?? is running: $ sudo docker ps $ sudo docker ps -q view logs output: $ sudo docker logs [name] $ sudo docker logs $(sudo docker ps -lq) stop container: $ sudo docker stop [name]
O) Connect Client To Remote Host:
On Docker Server: $ sudo service docker.io stop $ docker -H tcp://0.0.0.0:5000 -H unix:///var/run/docker.sock -d & On Docker Client: $ sudo docker -H 10.0.0.4:5000 ps $ docker -H <host-ip>:5000 run -it â??name test-container ubuntu
O) Python web application container:
- Default Randomized Port Mapping: $ sudo docker pull training/webapp | $ sudo docker run -d -P training/webapp pyhton app.py $ sudo docker ps -l knowing port data: $ sudo docker port [name] 5000
- Stop/Restart Container $ sudo docker stop [name] $ sudo docker restart [name]
- Remove Container: $ sudo docker stop [name] && sudo docker rm [name] | force $ sudo docker rm -f [name] - Manual Port Mapping: $ sudo docker run -d -p 5000:5000 training/webapp pyhton app.py view logs of running container: $ sudo docker logs -f [name]
view all proccess running inside: $ sudo docker top [name]
- Inspect Container: $ sudo docker inspect [name] $ sudo docker inspect -f 'الگو:.NetworkSettings.IPAddress' [name]
- Stop/Remove All: $ sudo docker stop $(sudo docker ps -a -q) $ sudo docker rm $(sudo docker ps -a -q)
O) Our own images:
- From preconfigured image: $ sudo docker run -t -i [image] /bin/bash Syntax: sudo docker commit -m=â??<your commit message hereâ?? -a=â??<maintainer name hereâ??\ <container id> <docker hub username>/<image name>:<tag>
Syntax: sudo docker run -t -i username/imagename:tag /bin/bash - Dockerfile: $ touch Dockerfile ----------------------- FROM ubuntu:14.04 MAINTAINER payam naderi <naderi.payam@gmail.com> RUN apt-get update && apt-get install -y ruby ruby-dev RUN gem install sinatra ----------------------- Built image: $ sudo Docker build â??t=â??username/imagename:tagâ?? . $ sudo docker run -t -i hcldevops/sinatra:v2 /bin/bash
- Image tagging: $ sudo docker tag b7065749275a hcldevops/sinatra:devel
- Upload Image To Docker Hub: $ sudo docker login Sysntax: docker push <username>/<imagename> - Removing images from Docker host: $ sudo docker rmi training/sinatra | remove all $ docker rmi $(docker images -q)
O) Container Linking: (inside webapp)
- Port Mapping: sudo docker run -d -P training/webapp python app.py sudo docker run -d -p 5000:5000 training/webapp python app.py sudo docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py sudo docker run -d -p 127.0.0.1::5000 training/webapp python app.py sudo docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
- Linking containers together: Naming Containers: $ sudo docker run -d -P â??name web training/webapp python app.py $ sudo docker ps â??l | $ sudo docker inspect -f â??الگو:.Nameâ?? ae6bcece4532
Link Containers: Syntax: â??link name:alias $ sudo docker run -d â??name db training/postgres $ sudo docker run -d -P â??name web â??link db:db training/webapp python app.py $ sudo docker ps
How Linking Containers Work: must be runned: $ sudo docker run -d â??name db training/postgres $ sudo docker run â??rm â??name web2 â??link db:db training/webapp env $ sudo docker run -t -i â??rm â??link db:db training/webapp /bin/bash $ cat /etc/hosts $ ping db
O) Data Management:
- Data Volume: Adding data volumes to containers: $ sudo docker run -d -P â??name web -v /webapp training/webapp python app.py Data volumes from host directories: -v /source-directory:/opt/directory $ sudo docker run -d -P â??name web1 -v /src/webapp:/opt/webapp training/webapp python app.py Creating read only data volume: $ sudo docker run -d -P â??name web2 -v /src/webapp:/opt/webapp:ro training/webapp python app.py
Data volume from host file: $ sudo docker run â??rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash
- Data Container: $ sudo docker run -d -v /dbdata â??name dbdata training/postgres echo data-only container for postgres $ sudo docker run -d â??volumes-from dbdata â??name db1 training/postgres $ sudo docker run -d â??name db2 â??volumes-from db1 training/postgres mount to same $ sudo docker rm -v dbdata
- Backing up data volumes: $ sudo docker run â??volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata