Docker学习笔记

docker version: 1.10.1

 

Installing docker on CentOS 7
1. Make sure your existing yum packages are up-to-date
    sudo yum update


2. Add the yum repo
    vim /etc/yum.repos.d/docker.repo


[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg


3. Install the Docker package
    yum -y install docker-engine


4. Start the Docker daemon
    systemctl start docker.service


5. Start the docker daemon at boot
    systemctl enable docker.service


6. Verify docker is installed correctly by running a test image in a container.
    docker run hello-world
    docker info

 

Container

Running an interactive shell
    docker run -i -t --name first_container centos:6 /bin/bash

The -i flag starts an interactive container. The -t flag creates a pseudo-TTY that attaches stdin and stdout.

 

If docker can't find the image on our local Docker host, it will reach out to the Docker Hub registry run by Docker, Inc., and look for it there. Once Docker had found the image, it downloaded the image and stored it on the local host.

 

Working with our first container

# Checking the container's hostname

root@f7cbdac22a02:/# hostname

 

# Checking the container's /etc/hosts

root@f7cbdac22a02:/# cat /etc/hosts

 

# Installing vim

root@f7cbdac22a02:/# yum install -y vim

 

# exit

root@f7cbdac22a02:/# exit

 

The container only runs for as long as the command we specified, /bin/bash, is running.Once we exited the container, that command ended, and the container was stopped.


To detach the tty without exiting the shell, use the escape sequence Ctrl-p + Ctrl-q

 

List containers

    docker ps -a

 

-a Show all containers. Only running containers are shown by default.

 

Starting a stopped container

# by name
docker start first_container

# by ID
docker start f7cbdac22a02

 

Attaching to a container
docker attach first_container

You might need to hit Enter to bring up the prompt

 

Creating detached container

# Starting a long-running worker process

# run the container in the background
docker run -d --name daemon_dave ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done"

 

Fetching the logs of our detached container
    docker logs daemon_dave

 

Opening a shell to your running container
    docker exec -it daemon_dave bash


Inspecting the container's processes
    docker top daemon_dave

 

Finding out more about our container
    docker inspect daemon_dave

 

Stopping a detached container
    docker stop daemon_dave

Deleting a container
    docker rm daemon_dave


Deleting all containers
    docker rm `docker ps -a -q`

 

Image

Download a pre-built image

# Download an ubuntu image

docker pull ubuntu

 

This will find the ubuntu image by name on Docker Hub and download it from Docker Hub to a local image cache.

 

Listing Docker images
    docker images


local images live on our local Docker host in the /var/lib/docker directory.


Searching for images

    docker search centos

 

Updating and committing an image

To update an image you first need to create a container from the image you’d like to update.

    docker run -t -i training/sinatra /bin/bash

 

Inside our running container let’s add the json gem.
    root@0b2616b0e5a8:/# gem install json

 

Once this has completed let’s exit our container using the exit command.

 

You can then commit a copy of this container to an image using the docker commit command.

    docker commit -m "Added json gem" -a "Kate Smith" \
    0b2616b0e5a8 ouruser/sinatra:v2

 

The -m flag allows us to specify a commit message. The -a flag allows us to specify an author for our update.

 

Building an image with a Dockerfile

First, create a directory and a Dockerfile.

$ mkdir sinatra
$ cd sinatra
$ touch Dockerfile

 

Each instruction creates a new layer of the image.

# This is a comment
FROM ubuntu:14.04
MAINTAINER Kate Smith <[email protected]>
ENV REFRESHED_AT 2016-02-22
RUN apt-get update && apt-get install -y ruby ruby-dev
RUN gem install sinatra

 

Build the image:

    docker build -t ouruser/sinatra:v2 .

 

In this case, I've specified the ENV instruction to set an environment variable called REFRESHED_AT,showing when the template was last updated. when I want to refresh the build, I change the date in my ENV instruction.

 

Bypassing the Dockerfile build cache:

    docker build --no-cache -t ouruser/sinatra:v2 .

 

Creating a container from the last successful step
    docker run -t -i 997485f46ec4 /bin/bash

Launching a container from our new image
    docker run -t -i ouruser/sinatra:v2 /bin/bash

 

Viewing our new image
    docker images ouruser/sinatra:v2

 

Docker Hub提供了大量的image,大家可以查看学习,比如jboss/wildfly, mysql

 

Pushing images to the Docker Hub
    docker push ouruser/sinatra:v2


Deleting an image
#This only deletes the image locally
docker rmi ouruser/sinatra:v2

 

Registry

Running a registry from a container

    docker run -p 5000:5000 registry

 

This will launch a container running the registry application and bind port 5000 to the local host.

 

Testing the new registry

# Tagging our image for our new registry
docker tag 8dbd9e392a96 docker.example.com:5000/ouruser/sinatra:v2

# push it to the new registry
docker push docker.example.com:5000/ouruser/sinatra:v2

# Building a container from our local registry
docker run -t -i docker.example.com:5000/ouruser/sinatra:v2 /bin/bash

 

Reference

The Docker book

Docker Docs

Best practices for writing Dockerfiles

Dockerfile reference

你可能感兴趣的:(image,Install,docker,container,Registry)