【注】:如果启动失败可能的问题,1:重装一下docker; 2:还是不行,启动报docker: relocation error: docker: symbol dm_task_get_info_with_deferred_remove, version Base not defined in file libdevmapper.so.1.02 with link time reference,更新yum upgrade device-mapper-libs,然后启动service docker start(具体描述见文章末)
$ docker version
$ docker run -d -P training/webapp python app.py
$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000/tcp nostalgic_morse
# docker run -d -p 6379 -v /home/hadoop/redis-2.8.13:/opt/redis-2.8.13 learn/tutorial /opt/redis-2.8.13/src/redis-server
be0b410f3601ea36070b3e519d9cc7cbe259caa2392f468c2dd2baebef42c4a8
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be0b410f3601 learn/tutorial:latest /opt/redis-2.8.13/sr 10 seconds ago Up 10 seconds 0.0.0.0:49153->6379/tcp sad_colden
# /home/hadoop/redis-2.8.13/src/redis-cli -p 49153
127.0.0.1:49153> keys *
(empty list or set)
127.0.0.1:49153>
-P flag is new and tells Docker to map any required network ports inside our container to our host. This lets us view our web application.
-l tells the docker ps command to return the details of the last container started.
-a the docker ps command only shows information about running containers. If you want to see stopped containers too use the -a flag.
-p Network port bindings are very configurable in Docker. In our last example the -P flag is a shortcut for -p 5000 that maps port 5000 inside the container to a high port (from the range 49153 to 65535) on the local Docker host. We can also bind Docker containers to specific ports using the -p flag。
-v flag you can also mount a directory from your own host into a container.
123456789101112131415
[root@docker redis-2.8.13]# docker run -d -p 6379:6379 -v /home/hadoop/redis-2.8.13:/opt/redis-2.8.13 learn/tutorial /opt/redis-2.8.13/src/redis-server
2c50850c9437698769e54281a9f4154dc4120da2e113802454f1a23c83ab91fe
[root@docker redis-2.8.13]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c50850c9437 learn/tutorial:latest /opt/redis-2.8.13/sr 29 seconds ago Up 28 seconds 0.0.0.0:6379->6379/tcp naughty_yonath
[root@docker redis-2.8.13]# docker port naughty_yonath 6379
0.0.0.0:6379
[root@docker redis-2.8.13]# docker logs -f naughty_yonath
...
[1] 27 Sep 13:48:12.192 * The server is now ready to accept connections on port 6379
[1] 27 Sep 13:50:33.228 * DB saved on disk
[1] 27 Sep 13:50:43.730 * DB saved on disk
-f This time though we’ve added a new flag, -f. This causes the docker logs command to act like the tail -f command and watch the container’s standard out. We can see here the logs from Flask showing the application running on port 5000 and the access log entries for it.
$ mkdir sinatra
$ cd sinatra
$ touch Dockerfile
# This is a comment
FROM ubuntu:14.04
MAINTAINER Kate Smith <ksmith@example.com>
RUN apt-get update && apt-get install -y ruby ruby-dev
RUN gem install sinatra
$ docker build -t="ouruser/sinatra:v2" .
$ docker run -t -i ouruser/sinatra:v2 /bin/bash
docker run -d -P training/webapp python app.py
docker ps nostalgic_morse
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000/tcp nostalgic_morse
docker run -d -p 5000:5000 training/webapp python app.py
docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py
docker run -d -p 127.0.0.1::5000 training/webapp python app.py
# The -p flag can be used multiple times to configure multiple ports.
docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
docker port nostalgic_morse 5000
127.0.0.1:49155
$ docker run -d -P --name web training/webapp python app.py
$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aed84ee21bde training/webapp:latest python app.py 12 hours ago Up 2 seconds 0.0.0.0:49154->5000/tcp web
$ docker inspect -f "" aed84ee21bde
/web
容器互通:
123456789
$ docker run -d --name db training/postgres
$ docker rm -f web
$ docker run -d -P --name web --link db:db training/webapp python app.py
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
349169744e49 training/postgres:latest su postgres -c '/usr About a minute ago Up About a minute 5432/tcp db, web/db
aed84ee21bde training/webapp:latest python app.py 16 hours ago Up 2 minutes 0.0.0.0:49154->5000/tcp web
You can see that Docker has created a series of environment variables with useful information about the source db container. Each variable is prefixed with DB_, which is populated from the alias you specified above. If the alias were db1, the variables would be prefixed with DB1_.
# Adding a data volume
docker run -d -P --name web -v /webapp training/webapp python app.py
# Mount a Host Directory as a Data Volume
docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
# 只读
docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py
# Mount a Host File as a Data Volume
docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash
# Creating and mounting a Data Volume Container
docker run -d -v /dbdata --name dbdata training/postgres echo Data-only container for postgres
docker run -d --volumes-from dbdata --name db1 training/postgres
docker run -d --volumes-from dbdata --name db2 training/postgres
docker run -d --name db3 --volumes-from db1 training/postgres
# Backup, restore, or migrate data volumes
docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar
[root@localhost ~]# docker -d
INFO[0000] +job serveapi(unix:///var/run/docker.sock)
INFO[0000] WARNING: You are running linux kernel version 2.6.32-431.el6.x86_64, which might be unstable running docker. Please upgrade your kernel to 3.8.0.
INFO[0000] Listening for HTTP on unix (/var/run/docker.sock)
docker: relocation error: docker: symbol dm_task_get_info_with_deferred_remove, version Base not defined in file libdevmapper.so.1.02 with link time reference
[root@localhost ~]# docker start 5ed45ce5ad3d
Error response from daemon: Cannot start container 5ed45ce5ad3d: [8] System error: write /cgroup/freezer/docker/5ed45ce5ad3d085fe3c004f90eef7c774a722e84cf0c9d18c197cc5900bbc8ae/cgroup.procs: invalid argument
FATA[0000] Error: failed to start one or more containers
[root@bigdata1 ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@bigdata1 ~]# yum install epel-release
[root@bigdata1 ~]# yum install docker-io
[root@bigdata1 ~]# docker -d
INFO[0000] Listening for HTTP on unix (/var/run/docker.sock)
WARN[0000] You are running linux kernel version 2.6.32-431.el6.x86_64, which might be unstable running docker. Please upgrade your kernel to 3.10.0.
docker: relocation error: docker: symbol dm_task_get_info_with_deferred_remove, version Base not defined in file libdevmapper.so.1.02 with link time reference
https://github.com/docker/docker/issues/12108
[root@bigdata1 ~]# yum install device-mapper-devel
[root@bigdata1 ~]# service docker start
Starting docker: [确定]
[root@bigdata1 ~]# docker version
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d/1.7.1
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d/1.7.1
OS/Arch (server): linux/amd64