1.开始
docker run -d -p 80:80 docker/getting-started
docker ps #查看容器
docker images #查看images
docker inspect 容器#查看容器信息
docker exec -it 容器 /bin/sh #交换进入容器
ps 查看进程
nginx位置 :etc/nginx
nginx配置文件 nginx.conf conf.d/default.conf
docker getting-started 位置: /usr/share/nginx/html/tutorial
2、创建image
- 复制app到usr/share/
- 在app下构建Dockerfile
FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
-
构建image
docker build -t getting-started . docker images docker ps docker stop ** docker ps docker run -dp 3000:3000 getting-started curl localhost:3000
3、升级应用
docker ps #显示正在运行的容器
docker ps -a #显示所有容器
docker rm id #删除容器
docker images #显示镜像
docker rmi id #删除镜像
docker build -t getting-started . #重新打包镜像
docker run -dp 3000:3000 getting-started
5 持久化数据(named volume)
docker run -dp 3000:3000 getting-started
docker ps
docker stop id
docker ps -a
docker rm -f id
docker volume create todo-db
docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
docker volume inspect todo-db
6、use bind mounts
#进入app目录
docker run -dp 3000:3000 -w /app -v "$(pwd):/app" node:12-alpine sh -c "yarn install && yarn run dev"
docker logs -f <container-id>
7、多容器app 使用mysql
- each container should do one thing and do it well
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
docker volume inspect todo-mysql-data
docker ps
docker exec -it <mysql-container-id> mysql -p
mysql> SHOW DATABASES;
docker run -it --network todo-app nicolaka/netshoot
dig mysql
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"
docker logs -f <container-id>
8、使用 docker compose
-
安装docker compose
pip3 install --upgrade pip pip3 install docker-compose -i https://pypi.douban.com/simple docker-compose --version
-
创建compose file
vim docker-compose.yml #------------- version: "3.7" services: app: image: node:12-alpine command: sh -c "yarn install && yarn run dev" ports: - 3000:3000 working_dir: /app volumes: - ./:/app environment: MYSQL_HOST: mysql MYSQL_USER: root MYSQL_PASSWORD: secret MYSQL_DB: todos mysql: image: mysql:5.7 volumes: - todo-mysql-data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: secret MYSQL_DATABASE: todos volumes: todo-mysql-data: ####-------- docker-compose up -d docker-compose logs -f