Cloud Native 8 min read

Step-by-Step Docker Installation and First Container on CentOS and Ubuntu

This tutorial walks through installing Docker on RedHat/CentOS (both 6.6 and 7) and Ubuntu 14.04, configuring it to start automatically, and then demonstrates running a simple hello‑world container, an interactive bash session, and a long‑running daemon container with detailed command explanations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Step-by-Step Docker Installation and First Container on CentOS and Ubuntu
本系列教程由旺旺知识库授权进行发布。继上篇文章对docker的基本介绍之后,接下来我们进行实战演练,光说不练,纯属扯淡~

一、docker容器安装

1.1 RedHat/CentOS下的docker安装:
RedHat/CentOS必须要6.6版本以上,或者7.x才能安装docker,建议在RedHat/CentOS 7上使用docker,因为RedHat/CentOS 7的内核升级到了kernel 3.10,对lxc容器支持更好。
CentOS 6.6下安装docker:

安装

rpm -ivh http://mirrors.yun-idc.com/epel/6Server/x86_64/epel-release-6-8.noarch.rpm

yum install docker-io

设置自启动:

chkconfig docker on

service docker start
CentOS 7下安装docker:

安装:

centos7下默认yum源已经包含了docker,因此无需其他yum仓库支持,直接安装即可: yum install docker 设置自启动:

systemctl enable docker

systemctl start docker
1.2 Ubuntu下docker安装:

Ubuntu下我仅测试了14.04版本,其他版本未测试

安装: sudo apt-get install docker.io 设置自启动:

设置开机启动:运行sysv-rc-conf,如果没有该工具,则自行安装即可

启动: service docker.io start *注:我不建议初学者采用编译安装docker,因为这样耗时耗力,还有很多不可预知的问题出现,对初始学习docker并没有多大用处。

二、运行第一个docker容器

2.1 hello world

安装好docker环境并启动docker服务后,我们就可以跑一个“hello world!”试试了

命令如下:

[root@localhost ~]# docker run centos echo "Docker,hello world"
Unable to find image 'centos:latest' locally
latest: Pulling from centos
... (image layers pulled) ...
Docker,hello world

命令说明:

docker run  :标准容器启动命令
centos :镜像名称
echo及后面的内容  :容器启动后执行的命令
2.2 启动一个交互式容器

当然我们也可以再复杂一点,增加 -it 参数,可以启动一个 bash,实现与容器的交互:

[root@localhost ~]# docker run -it centos /bin/bash

[root@110baabc10bc /]# echo "Docker,hello world"
Docker,hello world

*注:-t 标示在容器内指定一个伪终端,-i 标示允许我们对容器内的 STDIN 进行交互。

在容器中执行 ps 命令,可以看到容器只运行了少数进程:

[root@110baabc10bc /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 02:05 ?        00:00:00 /bin/bash
root        17     1  0 02:06 ?        00:00:00 ps -ef
2.3 以服务方式启动一个docker容器

普通的 hello world 容器执行完后会退出,使用 -d 参数可以让容器以守护进程方式长期运行:

演示如下:

[root@localhost ~]# docker run -d centos /bin/bash -c "while true; do echo Docker,hello world; sleep 2; done"
5ff7a2ac73469a4ff30d3709ceaa4d9ee14a87bf075fdf5ba4cb751b7077edf3

[root@localhost ~]# docker logs 5ff7a2ac73469a4ff30d3709ceaa4d9ee14a87bf075fdf5ba4cb751b7077edf3
Docker,hello world
Docker,hello world
Docker,hello world
...

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              NAMES
5ff7a2ac7346        centos              "/bin/bash -c 'while   21 seconds ago      Up 20 seconds       elegant_jang

这里的长字符串是容器 ID,用于唯一标识容器,可用于查看日志、停止或删除容器等操作。

使用死循环是因为如果容器内的唯一进程结束,容器也会停止;服务必须以守护进程方式运行。

容器的操作是不是很简单的呢?下文我们将探讨docker镜像。
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DevOpscontainerizationLinuxCentOSUbuntu
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.