docker系列一 docker 安装和常用命令
安装DockerDocker命令前置条件Docker 服务命令Docker 镜像命令查看images搜索dockerdocker命令来下载镜像删除镜像命令为docker rmiDocker 容器命令根据镜像创建容器并启动查看运行中的容器列表启动容器停止容器查看容器的日志登录容器1端口映射安装Dockersudo apt-get updatesudo apt-get
安装Docker
在Ubuntu
sudo apt-get update
sudo apt-get install docker
执行sudo docker version,如果看到如下信息则表示安装成功
hry@hry-VirtualBox:~$ sudo docker version
[sudo] password for hry:
Client:
Version: 1.12.6
API version: 1.24
Go version: go1.6.2
Git commit: 78d1802
Built: Tue Jan 31 23:35:14 2017
OS/Arch: linux/amd64
Server:
Version: 1.12.6
API version: 1.24
Go version: go1.6.2
Git commit: 78d1802
Built: Tue Jan 31 23:35:14 2017
OS/Arch: linux/amd64
Docker命令
前置条件
- 添加用户到docker用户组,这样在执行Docker时,就不需要每次都输入sudo
sudo gpasswd -a hry docker
# 重启系统
sudo reboot
- 由于docker被墙,国内访问困难。
为了解决这个问题可以通过DaoCloud加速:
- DaoCloud官网注册账号:http://www.daocloud.io/
- 注册帐号,登录DaoCloud后进入”控制台”—>”加速器”,复印页面内容在linux中执行
Docker 服务命令
- 查看docker信息
hry@hry-VirtualBox:~$ docker info
Containers: 3
Running: 0
Paused: 0
Stopped: 3
Images: 3
Server Version: 1.12.6
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 19
Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge overlay host null
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: apparmor seccomp
Kernel Version: 4.8.0-39-generic
Operating System: Ubuntu 16.04.2 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.953 GiB
Name: hry-VirtualBox
ID: ZY66:YXDS:GROT:LYZZ:SYSR:UUF7:KELB:MH3X:UDVA:27FZ:GCOS:NUNV
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Insecure Registries:
127.0.0.0/8
- 启动docker
sudo service docker start
- 关闭docker
sudo service docker stop
Docker 镜像命令:
查看images
sudo docker images
hry@hry-VirtualBox:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis-my latest cdc4ae18184e 22 hours ago 182.9 MB
redis latest 1a8a9ee54eb7 2 weeks ago 182.9 MB
dockercloud/hello-world latest 04b44316b6af 2 weeks ago 30.83 MB
列表参数如下
- REPOSITORY : 指明镜像来自于哪个仓库
- TAG : 镜像的标记。
- IMAGE ID : 镜像的ID值,该值是唯一的,有相同ID值的镜像是同一个镜像
- CREATED : 镜像的创建时间。
- VIRTUAL SIZE : 镜像的大小
搜索镜像
sudo docker search 镜像
hry@hry-VirtualBox:~$ sudo docker search jdk
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
openjdk OpenJDK is an open-source implementation o... 325 [OK]
jboss/base-jdk 26 [OK]
ingensi/oracle-jdk Official Oracle JDK installed on centos. 20 [OK]
airdock/oracle-jdk Docker Image for Oracle Java SDK (8 and 7)... 15 [OK]
n3ziniuka5/ubuntu-oracle-jdk Ubuntu with Oracle JDK. Check tags for ver... 11 [OK]
delitescere/jdk Minimal Zulu OpenJDK 8 [OK]
jtech/oracle-jdk A Docker image based on the smallest Linux... 3 [OK]
picoded/ubuntu-openjdk-8-jdk Ubuntu 16.04 LTS, with openjdk-8-jdk insta... 2 [OK]
jckrz/debian-oracle-jdk Vanilla Debian + Oracle JDK 1 [OK]
sbminimal/docker-ubuntu-jdk Docker Image containing Ubuntu with oracle... 1 [OK]
vaysman/jdk Latest Ubuntu with JDK 0 [OK]
pinterb/jdk Oracle JDK 0 [OK]
ping2ravi/jdk All Java/JDK Versions 0 [OK]
softgarden/jdk jdk 0 [OK]
下载镜像
sudo docker pull 镜像
hry@hry-VirtualBox:~$ sudo docker pull openjdk
Using default tag: latest
latest: Pulling from library/openjdk
5040bd298390: Already exists
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
c39ff935bbe4: Pull complete
6e03296a7302: Pull complete
Digest: sha256:1f49840bf143c4d6394b4db4fc5bf0526deac5afc5d738915d8a5b51e0b3e43f
Status: Downloaded newer image for openjdk:latest
删除镜像
sudo docker rmi 镜像
sudo docker rmi 8dde5631d4aa
Docker 容器命令
根据镜像创建容器,并启动
如果镜像把比作类,则容器就是对象。通过镜像创建容器
hry@hry-VirtualBox:~$ sudo docker run --name test_redis -itd 1a8a9ee54eb7
64024a8a158b276f6a8c5069343cf862add88d2e52e80537246449894a47e768
启动参数
- –name参数是为容器取得名称
- -i:表示以“交互模式”运行容器
- -t:表示容器启动后会进入其命令行
- -d:参数能让一个容器以守护态运行
查看运行中的容器列表
hry@hry-VirtualBox:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64024a8a158b 1a8a9ee54eb7 "docker-entrypoint.sh" 3 minutes ago Up 3 minutes 6379/tcp test_redis
7e7ee9d4ab30 04b44316b6af "/bin/sh -c 'php-fpm " 5 minutes ago Up 5 minutes 80/tcp teest_hellowold
列表说明
- CONTAINER ID:启动的时候生成的ID
- IMAGE:该容器使用的镜像
- COMMAND:容器启动时调用的命令
- CREATED:容器创建时间
- STATUS:容器的状态
- PORTS:容器系统使用的端口号
- NAMES:容器自定义名称
启动容器
sudo docker start container-name/container-id
sudo docker start 64024a8a158b
停止容器
sudo docker stop container-name/container-id
sudo docker stop 64024a8a158b
查看容器的日志
sudo docker logs container-name/container-id
hry@hry-VirtualBox:~$ sudo docker logs 64024a8a158b
1:C 02 Mar 11:51:27.851 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 02 Mar 11:51:27.873 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 02 Mar 11:51:27.873 # Server started, Redis version 3.2.8
1:M 02 Mar 11:51:27.873 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 02 Mar 11:51:27.873 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 02 Mar 11:51:27.873 * The server is now ready to accept connections on port 6379
登录容器
sudo docker exec -it container-name/container-id bash
hry@hry-VirtualBox:~$ sudo docker exec -it 64024a8a158b bash
root@64024a8a158b:/data#
使用exit可以退出容器
列出一个容器里面被改变的文件或者目录
$docker diff container-name/container-id
hry@hry-VirtualBox:~$ sudo docker diff 64024a8a158b
C /root
A /root/.bash_history
显示一个运行的容器里面的进程信息
docker top container-name/container-id
hry@hry-VirtualBox:~$ sudo docker top 64024a8a158b
UID PID PPID C STIME TTY TIME CMD
vboxadd 6061 6048 0 19:51 pts/2 00:00:01 redis-server *:6379
hry@hry-VirtualBox:~$
从容器里面拷贝文件/目录到本地一个路径
docker cp (container-name/container-id):/container_path path
hry@hry-VirtualBox:~$ docker cp 64024a8a158b:/root/.bash_history history
端口映射
因为容器中运行的软件所使用的端口,在本机和本机的局域网是不能访问的,所以将docker容器中的端口映射到当前的端口上。
映射容器的6379端口到本机的6378端口,如下
docker run -d -p 6378:6379 --name port-redis redis
#### 删除容器
删除单个容器
sudo docker rm container-id
删除所有容器
sudo docker rm `sudo docker ps -a -q`
更多推荐
所有评论(0)