Docker Learning Notes

开启Interactive terminal

➜  ~ docker run --rm -i -t ubuntu bash
root@9b6cc67f1a6d:/# 

Run command

➜  ~ docker run --rm -i -t ubuntu /bin/bash -c "apt-get install -y vim && vim"

List container

// 所有未 kill的 container
docker ps -a

// last run container
docker ps -lq

Inspect container内的各参数

// returns a json string
docker inspect <container_id>

Container Log

// Follow logs live
docker logs -f <container_id>

detached container

docker run -d ubuntu ping google.com

Stop Container

// stop 
docker stop <container_id>

// kill
docker kill <container_id>     // you could skip docker stop for this command

// remove all containers including running
docker ps -aq | xargs docker rm -f

Restart Container

$ docker start <container_id>

// attach to it
$ docker attach `docker ps -q -l`

remove images

docker rmi

connect to running container

➜  ~ docker exec -it flamboyant_bose bash

volumn

docker run --rm -it -v $(pwd):/mnt ubuntu bash
// shared volumn
// docker container 1
docker run -d -v /var/vol1 ubuntu bash

// docker container 2
docker run --rm -it --volumes-from <continer_1_id> ubuntu bash

自动分配并 expose 一个 port

docker run -d -P redis

绑定 IP

docker run -d -p 127.0.0.1:6379:6379 redis

Linking container

docker run -d --name=redis redis

docker run --rm -it --link redis:db ubuntu bash

commit

// 比如在 ubuntu 中运行了一系列的 command 后,安装了各种 software
// 只要 run commit, 就可以将此时的 container 的状态, 保存为一个 image.
// 下次再创建 以 ubtuntu为基础的 container, 就不需要重新安装这些 software 了


docker commit -m="my commit" -a="my name" <container_id> <my_new_image_name>

Copy and past images

docker save -o my_image.tar <my_image_name> // e.g. docker save -o my_ubuntu.tar nino_ubutun

// copy my_image.tar to my new Mac

// on new mac, run
docker load -i /path/to/my_image.tar
docker images    // 将见到复制过来的 image 了

build

// prevent container during build
docker build --rm 

docker build --no-cache -t mongo .       	// --no-cache means 不用 local 的 cache, build from scratch

Example - sshd Ubuntu image

NIN: https://docs.docker.com/engine/examples/running_ssh_service/#clean-up. And ignore below


https://hub.docker.com/r/rastasheep/ubuntu-sshd/~/dockerfile/

docker run -it ubuntu bash
root@2024aa0233c3:/# apt-get update
root@2024aa0233c3:/# apt-get install -y openssh-server
root@64704ecfb936:/# sshd
sshd re-exec requires execution with an absolute path
root@64704ecfb936:/# /usr/sbin/sshd
Missing privilege separation directory: /var/run/sshd

root@2024aa0233c3:/# mkdir /var/run/sshd
root@2024aa0233c3:/# echo 'root:demo' | chpasswd

// allow root to login
root@2024aa0233c3:/# sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/g' /etc/ssh/sshd_config

// allow root to use password
root@2024aa0233c3:/# sed -ri 's/UserPAM yes/UsePAM no/g' /etc/ssh/sshd_config

root@2024aa0233c3:/# exit

// save changes
docker commit `docker ps -lq` nino/sshd-example


docker run -d -p 2230:22 nino/sshd-example /usr/sbin/sshd -D
Show Comments