第一章
最简单的 hello_world/Dockerfile
FROM busybox:latest
CMD ["echo", "hello world"]
第二章
docker run --detach \
--name web \
nginx:latest
docker run -d \
--name mailer \
dockerinaction/ch2_mailer
docker run --interactive --tty \
--link web:web \
--name web_test \
busybox:1.29 /bin/sh
docker run -it \
--name agent \
--link web:insideweb \
--link mailer:insidemailer \
dockerinaction/ch2_agent
docker ps
#
docker run -d --name namespaceA \
busybox:1.29 /bin/sh -c "sleep 30000"
docker run -d --name namespaceB \
busybox:1.29 /bin/sh -c "nc -l 0.0.0.0 -p 80"
docker exec namespaceA ps
docker exec namespaceB ps
docker run --pid host busybox:1.29 ps
docker run -d --name webConflict nginx:latest
docker logs webConflict
docker exec webConflict nginx -g 'daemon off;'
# 解决方案
docker run -d --name webA nginx:latest
docker logs webA
docker run -d --name webB nginx:latest
docker logs webB
# 容器ID写入文件
CID=$(docker ps --latest --quiet)
echo $CID
CID=$(docker ps -l -q)
echo $CID
MAILER_CID=$(docker run -d dockerinaction/ch2_mailer)
WEB_CID=$(docker create nginx)
AGENT_CID=$(docker create --link $WEB_CID:insideweb \
--link $MAILER_CID:insidemailer \
dockerinaction/ch2_agent)
echo $MAILER_CID
echo $AGENT_CID
# wrong
docker start $AGENT_CID
docker start $WEB_CID
# right
docker start $WEB_CID
docker start $AGENT_CID
MAILER_CID=$(docker run -d dockerinaction/ch2_mailer)
WEB_CID=$(docker run -d nginx)
AGENT_CID=$(docker run -d \
--link $WEB_CID:insideweb \
--link $MAILER_CID:insidemailer \
dockerinaction/ch2_agent)
docker run -d --name wp --read-only wordpress:5.0.0-php7.2-apache
docker inspect --format "{{.State.Running}}" wp
docker logs wp
docker run -d --name wp_writable wordpress:5.0.0-php7.2-apache
docker container diff wp_writable
docker run -d --name wp2 --read-only -v /run/apache2/ --tmpfs /tmp wordpress:5.0.0-php7.2-apache
docker logs wp2
docker run -d --name wpdb -e MYSQL_ROOT_PASSWORD=ch2demo mysql:5.7
docker run -d --name wp3 --link wpdb:mysql -p 8000:80 --read-only -v /run/apache2/ --tmpfs /tmp wordpress:5.0.0-php7.2-apache
docker inspect --format "{{.State.Running}}" wp3
#!/bin/sh
DB_CID=$(docker create -e MYSQL_ROOT_PASSWORD=ch2demo mysql:5.7)
docker start $DB_CID
MAILER_CID=$(docker create dockerinaction/ch2_mailer)
docker start $MAILER_CID
WP_CID=$(docker create --link $DB_CID:mysql -p 80 \
--read-only -v /run/apache2/ --tmpfs /tmp \
wordpress:5.0.0-php7.2-apache)
docker start $WP_CID
AGENT_CID=$(docker create --link $WP_CID:insideweb \
--link $MAILER_CID:insidemailer \
dockerinaction/ch2_agent)
docker
#!/bin/sh
if [ ! -n "$CLIENT_ID" ]; then
echo "Client ID not set"
exit 1
fi
WP_CID=$(docker create \
--link $DB_CID:mysql \
--name wp_$CLIENT_ID \
-p 80 \
--read-only -v /run/apache2/ --tmpfs /tmp \
-e WORDPRESS_DB_NAME=$CLIENT_ID \
--read-only wordpress:5.0.0-php7.2-apache)
docker start $WP_CID
AGENT_CID=$(docker create \
--name agent_$CLIENT_ID \
--link $WP_CID:insideweb \
--link $MAILER_CID:insidemailer \
dockerinaction/ch2_agent)
docker start $AGENT_CID
# 容器的状态和依赖
# 清理
docker rm -vf $(docker ps -a -q)
题外话:3句话的魅力
docker create --name mysql_data_container \
-v /var/lib/mysql ubuntu
docker run --volumes-from mysql_data_container \
-v /var/lib/mysql:/var/lib/mysql \
-e MYSQL_USER=mysql \
-e MYSQL_PASSWORD=mysql \
-e MYSQL_DATABASE=test \
-e MYSQL_ROOT_PASSWORD=test -it \
-p 3306:3306 \
-d mysql
docker run -d \
--name wordpress \
--link mysql:mysql \
wordpress
可能 容器id小问题
命令小结
docker images
docker search nginx
docker run -d --name xxx1 nginx
docker run -it --name xxx2 nginx
docker run --link original_container_name:new_name xxx # 允许link多个
docker ps
docker ps -a
docker restart xxx
docker stop xxxx
docker logs xxx