BlueXIII's Blog

热爱技术,持续学习

0%

Docker学习笔记

基本操作

docker for mac

https://docs.docker.com/docker-for-mac/
HyperKit VM

helloworld

docker –version
docker-compose –version
docker-machine –version
docker ps
docker run hello-world
docker run -d -p 80:80 –name webserver nginx
docker run -it ubuntu bash
docker run docker/whalesay cowsay boo
docker run docker/whalesay cowsay boo-boo

build image

vi Dockerfile
FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay

docker build -t docker-whale .
docker images
docker run docker-whale

tag & push

docker tag 038cc8845778 bluexiii/docker-whale:latest
docker login
docker push maryatdocker/docker-whale
docker rmi -f docker-whale
docker run bluexiii/docker-whale

容器命名

docker run –name bob_the_container -i -t ubuntu /bin/bash

重新启动已停止的容器

docker start bob_the_container

附着到容器上

docker attach bob_the_container

创建守护式容器

docker run –name daemon_dave -d ubuntu /bin/sh -c “while true; do echo hello world; sleep 1;done”

查看日志

docker logs -ft daemon_dave

查看容器中的进程

docker top daemon_dave

查看统计信息

docker stats daemon_dave

停止守护式容器

docker stop daemon_dave

自动重启容器

–restart=always
–restart=on-failure:5
docker run –restart=always –name daemon_dave2 -d ubuntu /bin/sh -c “while true; do echo hello world; sleep 1;done”

获取容器详情

docker inspect daemon_dave2

删除容器

docker rm

删除所有容器

docker rm docker ps -a -q

本地镜像位置

$HOME/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2

拉取镜像到本机

docker pull fedora

查找镜像

docker search fedora

构建镜像

使用commit构建镜像

基于Ubuntu构建

docker run -i -t ubuntu /bin/bash

内网环境下设置apt代理

cat > /etc/apt/apt.conf.d/10proxy
Acquire::http::Proxy “http://134.32.32.13:31315/";
^d

安装基础包

apt update
apt install nginx

commit提交

docker commit cb051b49a4cd bluexiii/mynginx
or
docker commit -m ‘message’ -a ‘author’ cb051b49a4cd bluexiii/mynginx:tag
类似git,只提交差异部分,速度很快

docker inspect bluexiii/mynginx

使用Dockfile构建镜像

vi Dockerfile
FROM ubuntu
RUN echo ‘Acquire::http::Proxy “http://IP:PORT/";' > /etc/apt/apt.conf.d/10proxy
RUN apt-get -y update && apt-get install -y nginx
RUN echo ‘hello world’ > /usr/share/nginx/html/index.html
EXPOSE 80
docker build -t bluexiii/mynginx2 .

运行镜像

docker run -d -p 40080:80 –name mynginx2 bluexiii/mynginx2 nginx -g “daemon off;”

查看端口映射情况

docker port mynginx2 80