简介
advantage
# container advantage
- easy to develop
- easy to deploy
# What is container?
- Layers of images:only different layers are downloaded
- Mostly Linux Base Image, small in size
- Application image on top
# Difference between Docker Image and Docker Container?
Docker Image
- the actual package
- artifact,that can be moved around
- not running
Docker Container
- actually start the application
- container environment is created
- running
# Docker vs Virtual Machine
Application — OS Kernel — Hardware
Docker
- Application layer, it uses host OS Kernel
- docker image’s size smaller than VM
- running speed faster
Virtual Machine
- Both Application And its own OS kernel.
- good Compatibility than Docker
# Installing Docker
- doc url (opens new window)
- pre-requirement
- docker-toolbox that brige enable OS to run docker
# Common Command
- docker pull [name]:[version] if no version, then will get the latest version.
- docket images : show list images you have pulled
- docker run [xxx]: to run an image ; -d daemon; -p bind host port to container port. —name:specify a name.
docker run -d -p6000:6379 —name redis redis
1
- docker ps : show you have running containers; -a: show history you have running.
- docker stop [containerId]:gracelly stop the running container.
# Debug docker
- docker log [containerId]/[container name]:show logs when docker running an images.
- docker exec -it(inside terminal) [containerId/containerName] /bin/bash:check running container’s running state
docker log redis
docker exec -it redis /biin/bash
1
2
2
docker run vs docker start
docker run: create a new container
docker start: restart a stopped container
1
2
2
# Docker compose
- specify the docker run command in yaml file.
- every config can map to the docker run command
# Docker file
- start by basing it on another image
- optionally define environment variables(recommend)
- run — execute any linux command
- copy — execute command on the HOST machine
- CMD[”node” , “server.js”] — execute node server.js command
- CMD = entrypoint command
- docker file is blueprint of any docker image, that means any docker image is on the docker hub should be built on its own docker file.
- when you adjust the Dockerfile, you MUST rebuild the Image
docker build -t name:tag
1
# docker registry
author use A.W.S for example.
docker tag name:tage xxx/cloudserver/name:tag -- 重命名
docker push xxx/cloudserver/name:tag -- 推送docker to docker repository
1
2
2
# Deploy containered image
The Server needs to login to pull from PRIVETE repository;Login not needed for PUBLIC DockerHub.
# Docker Volumes
When do we need Docker Volumes?
Data is gone! When restarting or removing the container. The container has virtual file system which is mapping the host’s physical file system.
3 Volume Types
- Host Volume:you decide where on the host file system the reference is made.
docker run -v host file dir : container file dir
1
- Anonymous Volumes:for each container a folder is generated that gets mounted.
docker run -v /var/lib/mysql/data --> /var/lib/docker/volumes/random-hash/_data
1
- Named Volumes:you can reference the volume by name; (👍👍👍)
docker run -v name:/var/lib/mysql/data
1
# how to use it to persite docker data?
- config it on docker-compose.yaml
version:'3'
services:
mongodb:
images: mongo
ports:
- 27017:27017
volumes:
- mongo-data:/data/db [mysql:var/lib/mysql / postgres: var/lib/postgresql/data]
xxx-other config
volumes:
mongo-data:
driver: local
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
- Docker Volume Locations
- Windows:C:\ProgramData\docker\volumes
- Linux:/var/lib/docker/volumes
- Mac:/var/lib/docker/volumes
上次更新: 2025/03/09, 15:45:50