Notes for Docker
6 min
记录前端同学学习 Docker 的笔记。
2020.03
command
# verified cli can talk to engine
docker verison
# most config values of engine
docker info
# docker command line structure
# docker <command> <sub-command> (options)
start a Nginx Web Server
# --publish 80:80 -> <host port>:<container port>
docker container run --publish 80:80 nginx
# --detach, run the container background
# you'll get a container id, which is unique
docker container run --publish 80:80 --detach nginx
# list containers that are active
docker container ls -a
# --name, name your container
docker container run --publish 80:80 --detach --name webhost nginx
# top command to see the process inside the container
docker container top webhost
# logs
docker container logs webhost
# stop a container
docker container stop <container id>
# remove a container
docker container rm <container id>
- downloaded image nginx from Docker Hub
- started a new container from that image
- opened port 80 on the host IP
- routes that traffic to the container IP, port 80
what happens when we run a container
- looks for that image locally in image cache, doesn’t find anything
- then looks in remote image repo (defaults to Docker Hub)
- downloads the latest version by default
- creates new container based on that image and prepares to start
- gives it a virtual IP on a private network inside docker engine
- opens up port 80 on host and forwards to port 80 in container
- starts container by using the CMD in the image Dockerfile
container vs VM
Container is just a process, you can find the process with a process tool.
# if the container is running, you can find it.
ps aux | grep <pid or process name>
what’s going on in containers
# process list in one container
docker container top
# details of one container config
docker container inspect
# performance stats for all containers, CPU/memory/IO/...
docker container stats
Assignment
- run a
nginx
, amysql
, and ahttpd
server - run all of them
--detach
, name them with--name
- nginx should listen on 80, httpd on 8080, mysql on 3306
- when running mysql, use the
--env
or-e
to pass inMYSQL_RANDOM_ROOT_PASSWORD=yes
- use
docker container logs
on mysql to find the password it created on startup - clean it all up with
docker container stop
anddocker container rm
- use
docker container ls
to ensure everything is correct before and after cleanup
2019.10
核心概念
- Docker 镜像(Docker Image)
Docker 镜像类似虚拟机镜像,可以理解为一个只读的模版。
镜像是创建 Docker 容器的基础。
- Docker 容器(Docker Container)
Docker 容器类似一个轻量级的沙箱,利用容器来运行和隔离应用。
容器是从镜像创建的应用运行实例,可以启动、开发、停止、删除。容器间是彼此隔离、互不可见的。
可以把容器看作一个简易版的 Linux 系统环境(包括 root 用户权限、进程空间、用户空间和网络空间等),以及运行在其中的应用程序打包而成的盒子。
- Docker 公开服务(Docker Registry)
Docker 公开服务,是一个集中的存储、分发镜像的服务。开放给用户使用、允许用户管理镜像的 Registry 服务。一般这类公开服务允许用户免费上传、下载公开的镜像,并可能提供收费服务供用户管理私有镜像。 一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个 标签(Tag);每个标签对应一个镜像。
注意:
- 镜像自身是只读的
- 容器从镜像启动时,会在镜像的最上层创建一个可写层
创建镜像
1. 基于已有容器创建
docker [container] commit [options] "Docker Newee" [REPOSITORY[:TAG]]
- -a, —author="", 作者信息
- -c, —change=[], 提交时执行 Dockerfile 指令
- -m,—message="", 提交消息
- -p,—pause=true,提交时暂停容器运行
2. 基于本地模版导入
cat unbuntu.tar.gz | docker import - ubuntu:18.04
3. 基于 Dockerfile 创建
先准备 Dockerfile,在路径中执行:
docker [image] build -t myImage:1.0
4. 存出和载入镜像
- 存出 save 把镜像导出到本地文件
docker save -o xxx.tar TARGET_IMAGE:TAG_ID
- 载入 load 从本地文件中载入本地镜像列表
docker load > dockerImageFile.tar
5. 上传镜像
docker tag nginx:lastest wyy/nginx:1.0
docker push wyy/nginx:1.0
command
# start a container
docker run nginx
# list all containers
docker ps
# list containers that are active
docker ps -a
# stop a container
docker stop nginx
# remove a container
docker rm nginx
# list images
# docker image ls
# info: Repository | Tag | Image Id | Created | Size
docker images
# remove images
docker rmi nginx
# execute a command inside the container
docker exec name_of_a_container cat /etc/hosts
# run a container attach(frontend mode, which you can exec no commands)
docker run nginx
# run a container detach(backend mode, which you can exec commands while the container is active)
# output is the id of the process ${pid}
# when you want to stop in, exec ` docker attach ${pid}`
docker run -d nginx
# run tag `:4.0` specify the version of the image
# look up information about the image on docker hub
docker run redis:4.0
# run - STDIN, input for the container
docker run -i docker_image
# run - STDIN, input in a terminal for the container, interactive mode
docker run -it docker_image
# inspect container
docker inspect container_name
# container log
docker logs container_name
# 为本地镜像添加新的标签,image id 一致,指向同一个镜像源(本地 ubuntu:latest),只是别名
docker tag ubuntu:latest myubuntu:latest
# search from docker hub (order by stars)
docker search [option] nginx
# 清理临时镜像
docker image prune -f
PORT mapping

Volumn mapping

docker lab
docker quiz from kodekloud.com
ENV Variables in Docker
docker run -e APP_COLOR=blue docker_image
How to inspect the ENV Variables in a docker container?
docker inspect container_name
ENV Variables will be in the object Config.Env
How to create my own image
- OS - ubuntu
- update apt repo
- install dependencies using apt
- install python dependencies using pip
- copy source code to /opt folder
- run the web server using
flask
command
FROM Ubuntu
# start from a base OS
RUN apt-get update
RUN apt-get install python
RUN pip install flask
RUN pip install flask-mysql
COPY . /opt/source-code
ENTRYPOINT FLASK_APP=/opt/source-code/app.py flask run
# specify ENTRYPOINT, run the image as a container