首页 > 代码库 > Ubuntu 16.04 安装Docker,部署NetCore项目
Ubuntu 16.04 安装Docker,部署NetCore项目
国庆节前最后一篇。
准备工作:
检查版本,因为Docker官方明确说了一下,必须是64位的Ubuntu,并且kernel版本大于3.10;如果你不能满足这个版本,恐怕是不能如愿了。
hager@hager-virtual-machine:~$ uname -r
4.4.0-34-generic
如果你版本过低,可以采用下面的命令升级:
sudo apt-get update
sudo apt-get upgrade
或者使用root权限处理都可以。
安装Docker
其实应该只要一句指令即可
hager@hager-virtual-machine:~$ wget –qO - https://get.docker.com/ | sh
请注意,这里的-q后面跟的是大写的O字母
执行上面这个命令后,将可能是一段时间的等待(可能是网速问题吧),正常安装的话,会出现类似这样的界面:
Need to get 23.3 MB of archives.
After this operation, 128 MB of additional disk space will be used.
Get:1 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 aufs-tools amd64 1:3.2+20130722-1.1ubuntu1 [92.9 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 cgroupfs-mount all 1.2 [4,970 B]
Get:3 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 liberror-perl all 0.17-1.2 [19.6 kB]
Get:4 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 git-man all 1:2.7.4-0ubuntu1 [735 kB]
Get:5 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 git amd64 1:2.7.4-0ubuntu1 [3,006 kB]
Get:6 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 docker-engine amd64 1.12.1-0~xenial [19.5 MB]
Fetched 23.3 MB in 9min 19s (41.6 kB/s)
Selecting previously unselected package aufs-tools.
(Reading database ... 240386 files and directories currently installed.)
Preparing to unpack .../aufs-tools_1%3a3.2+20130722-1.1ubuntu1_amd64.deb ...
Unpacking aufs-tools (1:3.2+20130722-1.1ubuntu1) ...
Selecting previously unselected package cgroupfs-mount.
Preparing to unpack .../cgroupfs-mount_1.2_all.deb ...
Unpacking cgroupfs-mount (1.2) ...
Selecting previously unselected package docker-engine.
Preparing to unpack .../docker-engine_1.12.1-0~xenial_amd64.deb ...
Unpacking docker-engine (1.12.1-0~xenial) ...
Selecting previously unselected package liberror-perl.
Preparing to unpack .../liberror-perl_0.17-1.2_all.deb ...
Unpacking liberror-perl (0.17-1.2) ...
Selecting previously unselected package git-man.
Preparing to unpack .../git-man_1%3a2.7.4-0ubuntu1_all.deb ...
Unpacking git-man (1:2.7.4-0ubuntu1) ...
Selecting previously unselected package git.
Preparing to unpack .../git_1%3a2.7.4-0ubuntu1_amd64.deb ...
Unpacking git (1:2.7.4-0ubuntu1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu7) ...
Setting up aufs-tools (1:3.2+20130722-1.1ubuntu1) ...
Setting up cgroupfs-mount (1.2) ...
Setting up docker-engine (1.12.1-0~xenial) ...
Setting up liberror-perl (0.17-1.2) ...
Setting up git-man (1:2.7.4-0ubuntu1) ...
Setting up git (1:2.7.4-0ubuntu1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for systemd (229-4ubuntu7) ...
Processing triggers for ureadahead (0.100.0-19) ...
+ sudo -E sh -c docker version
Client:
Version: 1.12.1
API version: 1.24
Go version: go1.6.3
Git commit: 23cf638
Built: Thu Aug 18 05:33:38 2016
OS/Arch: linux/amd64
Server:
Version: 1.12.1
API version: 1.24
Go version: go1.6.3
Git commit: 23cf638
Built: Thu Aug 18 05:33:38 2016
OS/Arch: linux/amd64
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 hager #把非root角色的账户加入到docker group,这样就可以在非root下用该账户运行docker相关命令了
Remember that you will have to log out and back in for this to take effect!
hager@hager-virtual-machine:~$
验证Docker是否正常安装
hager@hager-virtual-machine:~$ docker version
Client:
Version: 1.12.1
API version: 1.24
Go version: go1.6.3
Git commit: 23cf638
Built: Thu Aug 18 05:33:38 2016
OS/Arch: linux/amd64
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
hager@hager-virtual-machine:~$
#如果没有docker group,可以添加一个,如果已经存在,则跳过这一步,直接添加用户到该组
hager@hager-virtual-machine:~$ sudo groupadd docker
groupadd: group ‘docker‘ already exists
#把当前用户加入docker group组内,然后退出系统,再重新进入,以让设置生效
hager@hager-virtual-machine:~$ sudo usermod -aG docker $USER
# 此时,再次查询docker版本,
hager@hager-virtual-machine:~$ docker version
Client:
Version: 1.12.1
API version: 1.24
Go version: go1.6.3
Git commit: 23cf638
Built: Thu Aug 18 05:33:38 2016
OS/Arch: linux/amd64
Cannot connect to the Docker daemon. Is the docker daemon running on this host? # 不知这句是啥意思,可能是没生效?,那就重启一下试试看
hager@hager-virtual-machine:~$ sudo reboot
# 等待重启后,再试试,果然可以了
hager@hager-virtual-machine:~$ docker version
Client:
Version: 1.12.1
API version: 1.24
Go version: go1.6.3
Git commit: 23cf638
Built: Thu Aug 18 05:33:38 2016
OS/Arch: linux/amd64
Server:
Version: 1.12.1
API version: 1.24
Go version: go1.6.3
Git commit: 23cf638
Built: Thu Aug 18 05:33:38 2016
OS/Arch: linux/amd64
hager@hager-virtual-machine:~$
#万事皆helloworld
hager@hager-virtual-machine:~$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
至此,说明docker环境可以正常运行了。
接下来,我们准备一个Netcore 项目,然后发布到Docker中
准备工作:
创建一个WebNetCore程序,比如VSCode玩的不熟悉,就用VS2015 创建吧。
需要注意的是,需要增加一个Dockerfile文件放到publishoutput发布的根目录下:
FROM microsoft/dotnet:latest COPY . /app WORKDIR /app EXPOSE 5000/tcp ENTRYPOINT ["dotnet", "PublishDockerCoreDemo.dll"]
运行发布,然后,将发布后的publishoutput文件夹复制到Ubuntu上。
运行docker构建镜像的命令:(构建一个名为hager@hager-virtual-machine:/opt/DotNetCoreProject/dockerdemoproject/PublishDockerCoreDemo$ docker build -t hager:PublishDockerCoreDemoimage .
Sending build context to Docker daemon 9.629 MB
Step 1 : FROM microsoft/dotnet:latest
latest: Pulling from microsoft/dotnet
6a5a5368e0c2: Pull complete
7b9457ec39de: Pull complete
ff18e19c2db4: Pull complete
9899b4d47ca6: Pull complete
2518101eff98: Pull complete
afcc52fc1b06: Pull complete
Digest: sha256:0bce09eb94bdba4e841f1937b19db7beb38d555bf5ad278c6d9a63dbf8993cf4
Status: Downloaded newer image for microsoft/dotnet:latest
---> 2de5a4ff8cc2
Step 2 : COPY . /app
---> 695efcf4c111
Removing intermediate container 114f4b720ae5
Step 3 : WORKDIR /app
---> Running in ba347eb5e3a3
---> 7456daa36483
Removing intermediate container ba347eb5e3a3
Step 4 : EXPOSE 5000/tcp
---> Running in 62620b8f67af
---> d871972f083a
Removing intermediate container 62620b8f67af
Step 5 : ENTRYPOINT dotnet PublishDockerCoreDemo.dll
---> Running in cfa7993fc67d
---> d7236b593ade
Removing intermediate container cfa7993fc67d
Successfully built d7236b593ade
hager@hager-virtual-machine:/opt/DotNetCoreProject/dockerdemoproject/PublishDockerCoreDemo$
#上面这几个玩意儿,下载比较慢,如果你网速给力,或许很快就完事儿了。
下一步,就是运行容器了,铺垫了那么久就是为了让她能跑一跑;
docker run -it -p 8080:5000 hager:PublishDockerCoreDemoimage
容器跟镜像的关系,一个镜像可以创建多个容器。
如上面的,我可以再创建一个 docker run -d -p 8090:5000 hager:PublishDockerCoreDemoimage
-d 参数也就是后台运行,然后通过docker ps 可以看到容器运行的状态,通过docker stop 容器id 停止。
箱子里面的东西弄好了,那么怎么搬运到其他地方呢。想搬运你得打包吧,总不能赤果果的搬运吧。
万一发现了不该看到的东西,怎么办呢。
保存和加载镜像(save、load)
语法:docker save image_name -o file_path
# 保存镜像到一个tar包; -o, --output="" Write to an file
$docker save image_name -o file_path
# 加载一个tar包格式的镜像; -i, --input="" Read from a tar archive file
$docker load -i file_path
# 机器a
$docker save image_name > /home/save.tar
# 使用scp将save.tar拷到机器b上,然后:
$docker load < /home/save.tar
语法知道了,那就照抄执行呗,运气好的话,一遍就过了。把它保存在了 /opt/文件夹下了。话说这个包还挺大。
目前,因为我的虚拟机里安装了很多别的,所以,现在米有正常的看到浏览的程序。应该是5000端口做了处理,有可能是nginx给拦截了。等回头换端口试试。
换成端口9527 ,然后再次发包!节后再试吧,目前nginx干扰。
疑问:
1、是不是每次更新站点,都要重新用docker build一下镜像呢?这样的话,是不是得每次都要同步到别的服务器上呢。如果是,感觉好麻烦;
2、目前还没有弄好如何向docker容器中的站点,自动运行;是不是得安装supervisor守护。
参考:
http://www.cnblogs.com/linezero/p/docker.html
http://blog.chinaunix.net/uid-10915175-id-4443127.html
http://blog.chinaunix.net/uid-10915175-id-4442826.html
Ubuntu 16.04 安装Docker,部署NetCore项目
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。