记录前端同学学习 Docker 的笔记。
# verified cli can talk to engine
docker verison
# most config values of engine
docker info
# docker command line structure
# docker <command> <sub-command> (options)
# --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>
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>
# 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
nginx
, a mysql
, and a httpd
server--detach
, name them with --name
--env
or -e
to pass in MYSQL_RANDOM_ROOT_PASSWORD=yes
docker container logs
on mysql to find the password it created on startupdocker container stop
and docker container rm
docker container ls
to ensure everything is correct before and after cleanupDocker 镜像类似虚拟机镜像,可以理解为一个只读的模版。
镜像是创建 Docker 容器的基础。
Docker 容器类似一个轻量级的沙箱,利用容器来运行和隔离应用。
容器是从镜像创建的应用运行实例,可以启动、开发、停止、删除。容器间是彼此隔离、互不可见的。
可以把容器看作一个简易版的 Linux 系统环境(包括 root 用户权限、进程空间、用户空间和网络空间等),以及运行在其中的应用程序打包而成的盒子。
Docker 公开服务,是一个集中的存储、分发镜像的服务。开放给用户使用、允许用户管理镜像的 Registry 服务。一般这类公开服务允许用户免费上传、下载公开的镜像,并可能提供收费服务供用户管理私有镜像。 一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个 标签(Tag);每个标签对应一个镜像。
注意:
docker [container] commit [options] "Docker Newee" [REPOSITORY[:TAG]]
cat unbuntu.tar.gz | docker import - ubuntu:18.04
先准备 Dockerfile,在路径中执行:
docker [image] build -t myImage:1.0
docker save -o xxx.tar TARGET_IMAGE:TAG_ID
docker load > dockerImageFile.tar
docker tag nginx:lastest wyy/nginx:1.0
docker push wyy/nginx:1.0
# 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
docker quiz from kodekloud.com
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
flask
commandFROM 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