Docker
Docker用途
Docker在各种操作系统上作了一层Docker Engine,https://docs.docker.com/engine/docker-overview/ ,
使得开发人员可以不再关注操作系统层面的操作,比如安装一个应用,如Redis,在Mac上要用brew install redis
,
Ubuntu上要用apt-get ...
, CentOS上要用yum install ...
。
在Docker上作,我们只要面向docker,向docker hub上下载一个Redis image,这个image会生成实例Container,Container中安装着可被运行的Redis。
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
https://docs.docker.com/engine/reference/builder/
Helpful skills
docker image ls
docker image prune
docker image rm <image_name> # Remove an image
docker-compose build
docker-compose up
docker-compose ps
docker-compose down
docker build -t sometagname . # build a image from Dockerfile
docker run --publish 80:8000 --detach --name container0 sometagname # Will create a container from image sometagname.
--env-file .env
docker ps
docker container ls -a
<tag or id, etc.>
docker container rm container_name_or_id # Remove a container
docker exec -it container_name_or_id bash # Enter a shell of a container. 'container_name' is the name of the container or can be the container id.
docker stop container_name_or_id
docker start container_name_or_id
docker system prune -a # When you build images, it will take your disk space for storing the cache. You can use this command to reclaim your space.
docker tag 8f804fe0084d harley/great-repo1:latest # 8f804fe0084d is an existing `IMAGE ID`, you run it to create a new image which REPOSITORY is `harley/great-repo1` and TAG is `latest`. In this way, you don't need to build again and again.
docker push harley/great-repo1:latest # push it to Docker hub
- For Django user, the service named
db_name
indocker-compose.yml
should be referred properly in DATABASES = { ‘default’: { ‘HOST’: ‘db_name’ } }settings.py
.$ docker-compose run db_name bash then you can run belew command in a new container created from image 'db_name' $ psql
- click
docker - preferences - Resources - File Sharing +
can make a local folder shared for docker. - Change
Dockerfile
will causedocker image build
created an new image.
Docker上示例文章重点解读
解读 https://docs.docker.com/get-started/part4/#create-a-cluster
当前我在Mac上,安装了完整的Docker,并且用Virtual Box创建了两个Linux的VM(其实是docker-machine create --driver virtualbox myvm1
帮我自动创建的)。
And made myvm1
as the active machine. Then when I type docker ....
, actually I was at the myvm1
’s env.
E.g: docker image ls
, I see nothing. Good, this means I was really in myvm1
env. When I switch to a new Terminal tab and type docker image ls
, I see 4 images created at by Mac OS.
Other important articles
https://docs.docker.com/engine/docker-overview/
https://docs.docker.com/compose/rails/
Here we can see how to cache ruby gems to speed up the image building (Cut 90% time off!)
Just look at my Dockerfile to see how to make gems cached
OS
Docker’s generic base OS is Ubuntu.
遗留问题和我的解答
- 常规应用(如Redis)的Base OS没有吗?那么运行它的操作系统是什么?配置文件如何修改?
Answer: https://github.com/docker-library/redis/blob/7be79f51e29a009fefdc218c8479d340b8c4a5e1/5.0/Dockerfile
For Redis, its base OS is Debian.
If you want a Redis running on CentOS, just search ‘docker Redis CentOS’ by Google. You’ll get https://hub.docker.com/r/centos/redis/ .
- 如何打包项目文件,生成项目image并push到docker hub上? Answer:
1 Choose a base OS and programming language which you want. Default base OS is Ubuntu.
2 docker build
your project’s image.
3 docker push
your project’s image.