docker
常用命令
docker ps
docker ps
docker ps -a
view latest created
docker ps -l
docker run
docker run -p=2368:2368 IMAGE
example: run ghost docker
docker run --name ghost-blog -d ghost
docker run --name ghost-blog -p 8081:2368 -d ghost
docker run -p=2368:2368 ghost
启动 image 并进入 bash
https://stackoverflow.com/questions/18497688/run-a-docker-image-as-a-container
$ docker images
root@dockertest:~# docker images
REPOSITORY TAG ID CREATED SIZE
ubuntu 12.04 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
# 下面两种都可以
$ docker run -i -t ubuntu:12.04 /bin/bash
$ docker run -i -t 8dbd9e392a96 /bin/bash
docker exec
进入 docker
docker exec -it <mycontainer> bash
http://stackoverflow.com/questions/30172605/how-to-get-into-a-docker-container
docker attach will let you connect to your Docker container, but this isn't really the same thing as ssh. If your container is running a web server, for example, docker attach will probably connect you to the stdout of the web server process. It won't necessarily give you a shell.
The docker exec command is probably what you are looking for; this will let you run arbitrary commands inside an existing container. For example:
docker cp
从 docker 内部拷贝文件
docker cp <containerId>:/file/path/within/container /host/path/target
Docker 容器和主机如何互相拷贝传输文件 https://docs.docker.com/compose/compose-file/#command
docker stop
kill all running containers
docker stop $(docker ps -a -q)
docker container
https://docs.docker.com/engine/reference/commandline/container/
docker container start CONTAINER
docker container rm CONTAINER
image 相关
list images
docker images
docker run image
docker start image-id
docker build
build with image name
docker build -t image_name:tag .
移除 container, image
https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes
-f force
# remove container
docker rm id1 id2
# remove all containers
docker rm $(docker ps -a -f status=exited -q)
# remove image
docker rmi id1 id2
# remove all images
docker rmi $(docker images -a -q)
docker-compose
进入某个 container 的 bash
此 container 已经运行
docker-compose exec app_name bash
进入 pg 命令行
docker-compose exec pg psql -U admin -d mock_server_plus
一些数据库命令
select * from migrations;
列出数据库
\l
列出表
\dt
Misc
安装编辑器
As in the comments, there's no default editor set - strange - $EDITOR env variable is empty. You can login into container with:
docker exec -it <container> bash
and run:
apt-get update
apt-get install vim
or use the following Dockerfile:
FROM confluent/postgres-bw:0.1
RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "vim"]
Docker images are delivered trimmed to bare minimum - so no editor is installed with the shipped container. That's why there's a need to install it manually.
Dockerfile
override the default command
command: bundle exec thin -p 3000
the command can also be a list, in a manner similar to dockerfile:
command: [“bundle”, “exec”, “thin”, “-p”, “3000”]
合并镜像
https://github.com/moby/moby/issues/3378
centos 卸载 docker
https://my.oschina.net/chen1988/blog/897348
yum list installed | grep docker
sudo yum -y remove docker-engine.x86_64
rm -rf /var/lib/docker
ports
-p 8080:80/udp Map UDP port 80 in the container to port 8080 on the Docker host.
-p 8080:80/tcp -p 8080:80/udp Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.
禁止 container 自动重启
https://stackoverflow.com/questions/37599128/docker-how-do-you-disable-auto-restart-on-a-container
docker update --restart=no my-container
磁盘管理
列出镜像
docker images
delete an image
docker rmi hello-world
查看磁盘空间占用
https://docs.docker.com/engine/reference/commandline/system_df/
docker system df
Remove all dangling images. If -a is specified, will also remove all images not referenced by any container.
docker image prune -a
使用 image name 移除 image
docker rmi \$(docker images | grep 'image_name')