Docker入门体验

1.安装Docker

通过执行如下命令来安装Docker:

sudo apt-get install curl   
curl -fsSL https://get.docker.com/ | sh

安装好后有如下提示:

If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
sudo usermod -aG docker guochongxin
Remember that you will have to log out and back in for this to take effect!

可根据实际需要执行上面提示中的命令,将当前用户加到docker用户组里,避免后期需要加sudo执行。

接下来执行sudo docker run hello-world命令来验证安装情况,如有下面提示则表示安装成功:

Hello from Docker.
 This message shows that your installation appears to be working correctly.
 To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
 To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
 Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com
 For more examples and ideas, visit:
 https://docs.docker.com/userguide/

按该提示,我们再执行sudo docker run -it ubuntu bash命令进行更多测试,运行后,会进入如下的命令行环境:

root@e6c6e1ac5349:/#

2.创建自己的image

1).编写Dockerfile

    cd ~
    mkdir mydockerbuild
    cd mydockerbuild/

    创建名为Dockerfile的文件,内容如下: 

FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay

2).根据Dockerfile编译镜像

sudo docker build -t docker-whale .

将当前目录编译为名叫docker-whale的镜像。

3).运行上面的docker-whale镜像

sudo docker run docker-whale

同时使用sudo docker images命令可以看到有如下新的本地仓库docker-whale:

guochongxin@slam:~/mydockerbuild$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED          VIRTUAL SIZE
docker-whale            latest              36e28bd26dd3        About a minute ago   255.6 MB
ubuntu                      latest              36248ae4a9ac        6 days ago                187.9 MB
hello-world               latest              0a6ba66e537a        4 months ago            960 B
docker/whalesay     latest              fb434121fc77          9 months ago             247 MB

3.创建Docker Hub帐号和仓库

网页浏览器打开https://hub.docker.com/?utm_source=getting_started_guide&utm_medium=embedded_Linux&utm_campaign=create_docker_hub_account,在打开的页面中注册一个帐号,邮箱验证后,登录该帐号并创建一个仓库,名叫docker-whale。

4.将步骤2创建的docker-whale镜像上传到步骤3创建的仓库中

步骤2可知创建的本地docker-whale镜像的IMAGE ID为36e28bd26dd3,此时使用docker tag命令为其创建一个TAG:

sudo docker tag 36e28bd26dd3 guochongxin/docker-whale:latest

其中,guochongxin为Docker Hub的帐号名,docker-whale为Docker Hub的仓库名。

此时再执行sudo docker images命令,可以看到如下信息:

guochongxin@slam:~/mydockerbuild$ sudo docker images
REPOSITORY               TAG                 IMAGE ID            CREATED       VIRTUAL SIZE
guochongxin/docker-whale   latest       36e28bd26dd3        13 minutes ago      255.6 MB
docker-whale               latest              36e28bd26dd3        13 minutes ago      255.6 MB
ubuntu                     latest              36248ae4a9ac        6 days ago          187.9 MB
hello-world                latest              0a6ba66e537a        4 months ago        960 B
docker/whalesay            latest              fb434121fc77        9 months ago        247 MB

多了一个guochongxin/docker-whale,接下来使用如下命令登录Docker Hub:

sudo docker login --username=guochongxin [email protected]

执行后会弹出Password:让你输入密码,登录成功后会有如下提示:

WARNING: login credentials saved in /home/guochongxin/.docker/config.json
Login Succeeded

接下来使用如下命令将本地的IMAGE上传到Docker Hub的仓库:

sudo docker push guochongxin/docker-whale

上传好后,我们把本地的删除掉:

sudo docker rmi -f 36e28bd26dd3

或者使用sudo docker rmi -f docker-whale命令即可删除,接下来运行如下命令:

sudo docker run guochongxin/docker-whale

此时会从服务器上提取我们刚上传的IMAGE并运行。

至此,Docker初体验告一段落。

你可能感兴趣的:(Docker入门体验)