start
一、get startted
docker run -d -p 80:80 docker/getting-started
some flags:
-d detached mode分离模式-p port 端口 主机:container
docker/getting-started 镜像image combine single character flags to shorten the command: 什么是容器镜像 container: isolated filesystem image: provide filesystem
image | 包含 |
---|
| all dependencies | | configuration | | scripts | | binaries | | environment variables | | a default command to run | | other metadata |
image | 其他 |
---|
| layering | | best practices |
the docker dashboard
todo list manager
get the app
- docker build -t getting-started . tag
- docker run -dp 3000:3000 getting-started
二、application
dockerfile(创建镜像)
文件 | dockfile |
---|
位置 | 与package.json同目录– | 内容 | FROM RUN WORKDIR COPY COPY RUN CMD | 命令 | docker build -t getting-started . |
其中:
-t 代表tag. 代表在当前文件夹寻找dockerfile
start container(开启容器)
docker run -dp 3000:3000 getting-startted 其中:
三、update the application
update the source code
replace the old container
- get the coontainer ID
·docker ps docker stop <the-container-id> docker rm <the-container-id> docker build -t getting-started . docker run -dp 3000:3000 getting-started
四、share the application
create the repo
- 重命名镜像
docker tag getting-started YOUR-USER-NAME/getting-started docker push YOUR-USER-NAME/getting-started
五、persist the DB数据持久化
named volumes
- 创建volumes
docker volume create todo-db - 挂载mount
docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started - 查看named vollumes地址
docker volume inspect todo-db
bind mounts(挂载点)
volume type comparisons
应用场景:
" With bind mounts, we control the exact mountpoint on the host. We can use this to persist data, but it’s often used to provide additional data into containers. When working on an application, we can use a bind mount to mount our source code into the container to let it see code changes, respond, and let us see the changes right away. "
创建dev container思路:
- Mount our source code into the container
- Install all dependencies, including the “dev” dependencies
- Start nodemon to watch for filesystem changes
实现步骤:
- 挂载 创建
docker run -dp 3000:3000 \ -w /app -v "$(pwd):/app" \ node:12-alpine \ sh -c "yarn install && yarn run dev" - 查看logs
docker logs -f <container-id>
五、Multi container apps多容器软件
each container should do one thing and do it well.
container networking(talking to each other)
There are two ways to put a container on a network:
- Assign it at start or
- connect an existing container.
- Create the network.
docker network create todo-app - 网络别名
docker run -d \ --network todo-app --network-alias mysql \ -v todo-mysql-data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=secret \ -e MYSQL_DATABASE=todos \ mysql:5.7 环境变量: -e Environment Variable
- [ ] mysql 是什么意思
docker exec -it <mysql-container-id> mysql -u root -p
mysql> SHOW DATABASES;
- 连接mysql
docker run -it --network todo-app nicolaka/netshoot dig mysql
六、Use Docker Compose
定义和分享多容器app define and share multi-container applications
yaml文件 | define services |
---|
command | spin everything up or tear it all down |
version
docker-compose version
Create the Compose file
docker-compose.yml
version: "3.7"
services:
Define the app service
define container
docker run -dp 3000:3000 \
-w /app -v "$(pwd):/app" \
--network todo-app \
-e MYSQL_HOST=mysql \
-e MYSQL_USER=root \
-e MYSQL_PASSWORD=secret \
-e MYSQL_DB=todos \
node:12-alpine \
sh -c "yarn install && yarn run dev"
|