前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker on CentOS for beginners

Docker on CentOS for beginners

作者头像
绿巨人
发布2018-05-16 17:46:13
6230
发布2018-05-16 17:46:13
举报
文章被收录于专栏:绿巨人专栏绿巨人专栏

Introduction

The article will introduce Docker on CentOS.

Key concepts

Docker

Docker is the world's leading software containerization platform. Docker is using union file systems which is a layered file system. When docker run a container, every image consists of a serials of read-only layers, and the container provides a read-write layer, and Docker combines these layers into one image, and forms a single coherent file system on the host OS.

Q: What is the relationship between the docker host OS and the container base image OS? A: The container's kernel is going to be the one from ubuntu, but nothing more.

Docker for Windows and Docker for Mac

Docker only support applications on Linux. Docker cannot run on Windows and Mac directly. Docker will create a VM via Virtual Box, and run command in the VM.

Docker engine

Docker engine is a light weight Docker, is used to create Docker images and run Docker containers.

Docker daemon and Docker client

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers.

Docker registries

Docker has a good ecological environment, there are some registries that store images and other materials on the Internet.

Docker Hub is a public registry and stores images.

images

An image is a file and is a read-only template, Docker can use it to create containers. Docker images can be built from 2 ways.

  • Build it from Dockfile
  • Build it from a container.

containers

Docker containers are similar to a directory. A Docker container holds everything that is needed for an application to run. Each container is created from a Docker image. Docker containers can be run, started, stopped, moved, and deleted. Each container is an isolated and secure application platform. Docker containers are the run component of Docker.

Dockerfile

Dockerfile files can be used to create images by Docker. Dockerfiles are files whose names are 'Dockerfile'. A Dockerfile is a scripts to create an image. In general, a Docker file is based on another image. For example: a image inheritance chain is:

spark : hadoop : centos : scratch.

Base images like 'centos' are images that are from the scratch image which is an explicitly empty image from the official Docker. We can consider 'scratch' is a reversed word by Docker. (You cannot pull, push an image named as 'scratch'.)

In fact, base images are not always OS images. like hello-world image, its Dockerfile is

代码语言:javascript
复制
FROM scratch
COPY hello /
CMD ["/hello"]

Why Docker is better than VMs

  • Performance It is said that the performance of running applications in Docker is same as running them on native machine. Meanwhile, VMs performance is slower twice times than native machines.

Installation & configuration the Docker repository file

Method 1: Run the Docker installation scripts

代码语言:javascript
复制
curl -fsSL https://get.docker.com/ | sh

Method 2: From scripts

  • Install Docker $ sudo yum install docker or light weight docker $ sudo yum install docker-engine
  • Create Docker repository file The configuration can help Docker to pull images from a registry. 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

Configuration Docker service

代码语言:javascript
复制
# configure load the Docker service at boot
sudo chkconfig docker on
# start Docker
sudo service docker start
# create a Docker group
sudo groupadd docker
# alow non-root user to run Docker
sudo usermod -aG docker $(whoami)

Verify Docker is installed

代码语言:javascript
复制
docker run hello-world
docker version
docker info

Docker key commands

Docker help

代码语言:javascript
复制
docker --help
docker [command] --help

Search an image

You may go to Docker Hub, search an image you want to use. or

代码语言:javascript
复制
# search images related to 'spark'
docker search spark

Output;

INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/sequenceiq/spark An easy way to try Spark 287 [OK] docker.io docker.io/gettyimages/spark A debian:jessie based Spark container 27 [OK] docker.io docker.io/singularities/spark An Apache Spark development Docker image 12 [OK] docker.io docker.io/shopkeep/spark Docker container with Spark, Scala, SBT, a... 7 [OK]

Pull an image

代码语言:javascript
复制
# pull the image named 'sequenceiq/spark'
docker pull sequenceiq/spark

List local images

代码语言:javascript
复制
docker images

Output:

REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/hello-world latest c54a2cc56cbb 8 weeks ago 1.848 kB docker.io/shopkeep/spark latest 4be7b9be182e 12 months ago 1.693 GB

Create a container

代码语言:javascript
复制
# create a container from image name
docker create -t -i shopkeep/spark

# Or create a container from image id
docker create -t -i c54

Output:

5bb2d0aaa227154920733fb9f13e1571fe40254dd1b3373b617550cdde71e57e

List local containers

代码语言:javascript
复制
# Show all containers
docker ps -a

Output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5bb2d0aaa227 shopkeep/spark "/bin/bash" 15 minutes ago Created insane_kowalevski

代码语言:javascript
复制
# Show running containers
sudo docker ps

Run a container

代码语言:javascript
复制
docker start -a -i 5bb

Output:

root@5bb2d0aaa227:/opt/spark#

Note: Docker is smart enough to match partial container id 5bb with the container id 5bb2d0aaa227...

Enter a running container with the existing TTY

代码语言:javascript
复制
# in most cases, the running container was started with a command 'docker start 5bb'
docker attach 5bb

Enter a running container with new TTY

代码语言:javascript
复制
docker exec -ti 5bb bash

Exit from a running container

代码语言:javascript
复制
root@5bb2d0aaa227:/opt/spark# exit

NOTE: the exit command from the last TTY will stop the container.

Stop a container

代码语言:javascript
复制
docker stop 5bb

Save a container as an image

代码语言:javascript
复制
docker commit 5bb my/image1

Output:

sha256:5cf350490e9e8b7495acb753f4041e34788e4881300b28ffc958db06d45fb4b3

Save an image to a tar archive

代码语言:javascript
复制
docker save my/image1 > ~/my_image1.tar

Load an image from a tar archive

代码语言:javascript
复制
docker load -i ~/my_image1.tar

Docker images/containers locations

  • /var/lib/docker
  • C:\Users[user name].docker

References

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-09-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Introduction
  • Key concepts
    • Docker
      • Docker for Windows and Docker for Mac
    • Docker engine
      • Docker daemon and Docker client
        • Docker registries
          • images
            • containers
              • Dockerfile
              • Why Docker is better than VMs
              • Installation & configuration the Docker repository file
                • Method 1: Run the Docker installation scripts
                  • Method 2: From scripts
                    • Configuration Docker service
                      • Verify Docker is installed
                      • Docker key commands
                        • Docker help
                          • Search an image
                            • Pull an image
                              • List local images
                                • Create a container
                                  • List local containers
                                    • Run a container
                                      • Enter a running container with the existing TTY
                                        • Enter a running container with new TTY
                                          • Exit from a running container
                                            • Stop a container
                                              • Save a container as an image
                                                • Save an image to a tar archive
                                                  • Load an image from a tar archive
                                                  • Docker images/containers locations
                                                  • References
                                                  相关产品与服务
                                                  容器镜像服务
                                                  容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
                                                  领券
                                                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档