一、安装docker
1.1 centos7.2安装docker
环境:centos7.2
安装方法:https://docs.docker.com/engine/installation/linux/centos/
1)Make sure your existing yum packages are up-to-date.
CentOS使用yum update更新时不升级内核
cp /etc/yum.conf /etc/yum.confbak 方法一、修改yum的配置文件 vi /etc/yum.conf 在[main]的最后添加 exclude=kernel* 方法二、直接在yum的命令后面加上如下的参数: yum --exclude=kernel* update 查看系统版本 cat /etc/issue 查看内核版本 uname -a
下面的命令,生产环境不要随便使用!
sudo yum --exclude=kernel* update
2)Add the yum repo.
sudo tee /etc/yum.repos.d/docker.repo <<-'EOF' [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7/ enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF
3)Install the Docker package.
sudo yum -y install docker-engine
4)Start the Docker daemon.
sudo service docker start
5)Verify docker is installed correctly by running a test image in a container.
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
a8219747be10: Pull complete
91c95931e552: Already exists
hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d
Status: Downloaded newer image for hello-world:latest
Hello from Docker.
This message shows that your installation appears to be working correctly.
。。。。。。
6)Run docker ps -a to show all containers on the system.
[root@bogon ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30973426a4de hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago adoring_mestorf
1.2 创建docker用户组
docker用root权限运行,所以普通用户总是加sudo来执行命令,所以我们创建docker用户组,有root的权限,然后把普通用户添加到docker组里,这样普通用户执行docker命令就不用加sudo 了。
1)Create the docker group.
sudo groupadd docker
2)Add your user to docker group.
sudo usermod -aG docker your_username
3)Log out and log back in.
This ensures your user is running with the correct permissions.
4)Verify your work by running docker without sudo.
$ docker run hello-world
1.3 uninstall
1)List the package you have installed.
$ yum list installed | grep docker yum list installed | grep docker docker-engine.x86_64 1.7.1-1.el7 @/docker-engine-1.7.1-1.el7.x86_64.rpm
2)Remove the package.
$ sudo yum -y remove docker-engine.x86_64 This command does not remove images, containers, volumes, or user-created configuration files on your host.
3)To delete all images, containers, and volumes, run the following command:
$ rm -rf /var/lib/docker
4)Locate and delete any user-created configuration files.
1.4 Learn about images & containers
An image is a filesystem and parameters to use at runtime. It doesn’t have state and never changes.
A container is a running instance of an image.
When you ran the command, Docker Engine:
- checked to see if you had the hello-world software image
- downloaded the image from the Docker Hub (more about the hub later)
- loaded the image into the container and “ran” it
二、Find and run the whalesay image
Step 1: Locate the whalesay image
浏览器打开:https://hub.docker.com
搜索:whalesay,回车
进入界面,就可以看到用法了。
https://hub.docker.com/r/mendlik/docker-whalesay
Usage:
# Print random fortune cookie message
$ docker run mendlik/docker-whalesay
# Print custom message
$ docker run mendlik/docker-whalesay "Your message"
# Let's see what's inside the container
$ docker run -it --entrypoint /bin/bash mendlik/docker-whalesay
Step 2: Run the whalesay image
我们在容器里,运行whalesay镜像:
docker run mendlik/docker-whalesay cowsay boo
查看本地所有的镜像:
[root@bogon ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB mendlik/docker-whalesay latest 552104437e78 5 months ago 172.3 MB
Take a moment to play with the whalesay container a bit.
Try running the whalesay image again with a word or phrase. And you type a lot to get whalesay to talk..
[root@bogon ~]# docker run mendlik/docker-whalesay boo-boo
_________
< boo-boo >
---------
\
\
\
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/
三、Build your own image
Step 1: Write a Dockerfile
1)创建目录,这个目录包含所有你要自建的镜像
mkdir mydockerbuild cd mydockerbuild
2)编辑文件内容
vi Dockerfile 添加内容: FROM mendlik/docker-whalesay:latest
FROM关键字表示你的镜像是基于哪个镜像来做的。
现在我们把fortunes程序加入到镜像里,fortunes 程序会有一个命令,让whale说话。所以我们要安装它。
这行安装软件到镜像里。
Dockerfile添加内容:
RUN apt-get -y update && apt-get install -y fortunes
一旦镜像有需要的软件,你就指定:当镜像加载的时候,运行这个软件。
这一行表示fortune程序,把一个漂亮的quote传递给cowsay程序
CMD /usr/games/fortune -a | cowsay
所以,你的文件全部内容为:
$ cat Dockerfile FROM mendlik/docker-whalesay:latest RUN apt-get -y update && apt-get install -y fortunes CMD /usr/games/fortune -a | cowsay
Step 2: Build an image from your Dockerfile
注意,一定要加点“.”
docker build -t docker-whalesay .
这个命令使用当前目录下的Dockerfile文件。
然后在你本机创建一个叫"docker-whalesay "的镜像
我们看看命令执行输出:
1 [root@bogon mydockerbuild]# docker build -t docker-whalesay . 2 Sending build context to Docker daemon 2.048 kB # docker检查他需要创建的东西。 3 Step 1 : FROM mendlik/docker-whalesay:latest # docker下载mendlik/docker-whalesay镜像,因为之前已经下载,就不用下载了。 4 ---> 552104437e78 5 Step 2 : RUN apt-get -y update && apt-get install -y fortunes # 先更新apt-get包 6 ---> Running in ecb33156fc42 7 Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB] 8 Get:2 http://security.debian.org jessie/updates/main amd64 Packages [385 kB] 9 Ign http://httpredir.debian.org jessie InRelease 10 Get:3 http://httpredir.debian.org jessie-updates InRelease [142 kB] 11 Get:4 http://httpredir.debian.org jessie Release.gpg [2373 B] 12 Get:5 http://httpredir.debian.org jessie-updates/main amd64 Packages [17.6 kB] 13 Get:6 http://httpredir.debian.org jessie Release [148 kB] 14 Get:7 http://httpredir.debian.org jessie/main amd64 Packages [9032 kB] 15 Fetched 9790 kB in 4min 47s (34.0 kB/s) 16 Reading package lists... 17 W: Size of file /var/lib/apt/lists/httpredir.debian.org_debian_dists_jessie_main_binary-amd64_Packages.gz is not what the server reported 9031837 9034031 18 Reading package lists... 19 Building dependency tree... 20 Reading state information... 21 fortunes is already the newest version. 22 0 upgraded, 0 newly installed, 0 to remove and 25 not upgraded. 23 ---> e78b54d7f981 24 Removing intermediate container ecb33156fc42 # docker再安装fortunes程序 25 Step 3 : CMD /usr/games/fortune -a | cowsay # docker 完成创建镜像,并打印输出。 26 ---> Running in 5ca72e7209e3 27 ---> 7c8d26884c76 28 Removing intermediate container 5ca72e7209e3 29 Successfully built 7c8d26884c76