Docker Container Literacy

Source: Internet
Author: User
Tags commit mysql in centos docker stop container docker ps docker run
Centos 6.5 Installation and use of Docker

Based on my usual habits, about "what is something" such a problem, please Baidu Bar, there will be more professional people, will be more detailed than I said, and here I only give the experience of the installation and use of the process. 1. Installation

Check the server environment first, Docker requires operating system CentOS6 above, kernel version must be 2.6.32-431 or higher, that is >=centos 6.5, when running Docker actual prompt 3.8.0 and above, must 64bit,32bit not support Docker.

[root@201 ~]# uname-r
2.6.32-642.1.1.el6.x86_64

Docker's storage driver is device Mapper, see if your driver meets

[root@201 ~]# grep device-mapper/proc/devices
253 device-mapper

It appears that we meet the above requirements and then start the installation.

For CentOS 6.5 You need to install the source first

[root@201 ~]# RPM-UVH http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Note that the CentOS 6 installation is docker-io from the Fedora Epel Library, which is a third-party library, so make sure to install the Epel first

[root@201 ~]# rpm-q epel-release
epel-release-6-8.noarch

OK, we have installed Epel, then Yum, wait a moment to install successfully

[root@201 ~]# Yum Install docker-io-y

If the system is CentOS 7 then the above steps are not necessary and the installation package is Docker instead of Docker-io

[root@201 ~]# Yum Install docker-y
2. Start

Start the daemon process

[root@201 ~]# Service Docker start

View startup information

[root@201 data]# Docker info
containers:0
images:0
Storage driver:devicemapper
 Pool name:docker-253 : 0-286421-pool
 Pool blocksize:65.54 kB
 backing filesystem:extfs
 Data file:/dev/loop0
 Metadata file: /DEV/LOOP1
 data Space used:305.7 MB
 data space total:107.4 GB
 data space available:3.536 GB
 Metadata  Space used:729.1 KB
 Metadata space total:2.147 GB
 Metadata space available:2.147 GB
 Udev Sync Supported: True
 Deferred removal enabled:false
 Data loop file:/var/lib/docker/devicemapper/devicemapper/data
 Metadata Loop file:/var/lib/docker/devicemapper/devicemapper/metadata
 Library version:1.02.117-rhel6 ( 2016-04-01)
execution driver:native-0.2
Logging driver:json-file Kernel version:2.6.32-642.1.1
. el6.x86_64
Operating System: <unknown>
cpus:1 Total
memory:996.2 MiB
name:201. liberalman.cn
Id:bhfo:eycf:xvaz:j637:2luv:rr6y:ur7z:mz43:pr4n:cgud:7eaj:5sbi

Set boot up

[root@201 ~]# Chkconfig Docker on

Docker Log Path

/var/log/docker
3. Example Test Test 1

We download a CentOS image

[root@201 data]# Docker pull CentOS
latest:pulling from the CentOS
3690474eb5b4:pull complete
342c399b9d0e: Pull complete
915b9d3c92fc:downloading 23.78 mb/70.58 mb
915b9d3c92fc:downloading 70.58 mb/70.58 MB
D5c89a2047c8:download Complete 
digest:sha256 : 14bc8ca808518a2703b6eff1a5f3b7065d4b5d4b388b575ae6a27db8791ab19b
status:downloaded Newer image for CentOS: Latest

List images, specify CentOS keywords to see the images we downloaded

[root@201 data]# Docker images CentOS
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
CentOS              Latest              D5c89a2047c8        hours ago        196.7 MB

We downloaded a nearly 200M image, OK, run container

[root@201 data]# Docker run-i-T Centos/bin/bash

-T is passed to the container TTY terminal,-I is interactive, which indicates that it can be interacted with. Can see my terminal also by root@201 data into root@f41d95a52943, indicating that entered the container terminal, to exit the input exit can be. If you want to open a new terminal into the container we started before, first query the container ID

[root@201 data]# Docker ps-a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f41d95a52943        centos              "/bin/bash"         , minutes ago      up, minutes                           

The ID is f41d95a52943 and then logged in in the new terminal

Docker exec-it F41d95a52943/bin/bash

This will allow you to log in.

We set up a CentOS minimum system in Docker, log in to this system [root@f41d95a52943/], view its IP address, find no ifconfig command, use yum install net-tools after installation

[root@f41d95a52943/]# ifconfig eth0:flags=4163<up,broadcast,running,multicast> MTU inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 INET6 fe80::42:acff:fe11:1 Prefixlen SCO Peid 0x20<link> ether 02:42:ac:11:00:01 txqueuelen 0 (Ethernet) RX packets 10375 bytes 12736213 (  12.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5643 bytes 309098 (301.8 KiB) TX Errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo:flags=73<up,loopback,running> MTU 65536 inet  127.0.0.1 netmask 255.0.0.0 inet6:: 1 prefixlen scopeid 0x10

Can see our Docker in the CentOS system, assigned an IP 172.17.0.1, ping Baidu and other websites can ping. Is this the end of it, when the restart container, you will find that the ifconfig command is not, the default is not for us to save the changes, so we need to submit a change to the image. Query the container ID on the host and commit the modifications

View the container you just ran
[root@201 ~]# Docker ps-l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
4e108c90fa11        centos              "/bin/bash"         2 minutes ago up       2 minutes                                  ecstatic_ Franklin 
[root@201 ~]# Docker start 4e108c90fa11
4e108c90fa11
then connect up
[root@201 ~]# Docker exec-it 4e108c90fa11/bin/bash
Install Net-tools pack
[root@4e108c90fa11/]# yum install net-tools-y
exit, submit a comment on the host
[root@201 ~]# docker commit-m= "add net-tools packages,nclude ifconfig command" 4e108c90fa11
129d59168480299c786d0585e9eb74fdbde3dcd00ee2cd2815d11b7749e53ac7

OK, now restart the container, even go up and try Ifconfig, you will find Ifconfig has been saved in the image. Test 2

Host, we run a Web server on the container

[root@201 data]# Docker run-d-P Training/webapp python app.py
Unable to find image ' Training/webapp:latest ' locally< C1/>latest:pulling from Training/webapp
e9e06b06e14c:pull complete 
02a8815912ca:download 
complete status:downloaded newer image for Training/webapp:latest
F0b7172ab3a14e6f078e7b01310a3b027e28234baf44f4f6f1aa68465e854f60

This web application for the official Docker, not local, so automatically go to the warehouse download, easy to implement Image,code sharing, and run everywhere.
The image is layered, so the build is faster after the change.

[root@201 ~]# Docker PS
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                     NAMES
272a09b3f713        training/webapp     "python app.py" about     a minute ago up about   a minute   0.0.0.0:32768->5000/tcp   

See that the Web server maps the 5000 ports inside Docker to the 32768 ports on our host, while we view IP information on the host

[root@201 ~]# ps-ef|grep docker-proxy|grep-v "grep" root 3163 3041 0 10:56 pts/0 00:00:00 docker-proxy-proto
Tcp-host-ip 0.0.0.0-host-port 32768-container-ip 172.17.0.1-container-port [root@201 ~]# netstat-npl|grep 32768 TCP 0 0::: 32768:::* LISTEN 3163/docker-proxy [root@201 ~]# I Fconfig Docker0 Link encap:ethernet HWaddr 82:ea:95:80:a9:f3 inet addr:172.17.42.1 bcast:0.0.0.0 mask:255  .255.0.0 Inet6 addr:fe80::c057:bbff:fe36:55cf/64 scope:link up broadcast RUNNING multicast mtu:1500
          Metric:1 RX packets:9 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:612 (612.0 b) TX bytes:468 (468.0 b) eth0 Link encap:ethernet HWaddr 08:00:27:dd:0d:82 inet A ddr:192.168.1.201 bcast:192.168.1.255 mask:255.255.255.0 inet6 addr:fe80::A00:27FF:FEDD:D82/64 scope:link up broadcast RUNNING multicast mtu:1500 metric:1 RX packets:469 Erro
          rs:0 dropped:0 overruns:0 frame:0 TX packets:304 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:46223 (4 5.1 KiB) TX bytes:67929 (66.3 KiB)

Host IP is 192.168.1.201, we visit this Web server, through the host browser access http://192.168.1.201:32768/, see the output

Hello world!
4. Make Your own image

Whim, make a MySQL image, and then open a bunch of MySQL service on their own server, provide different ports to access, do a cluster or something, hehe

First create a directory/data/mysql, and create a dockerfile file with the following file contents
Vim Dockerfile

From CENTOS6:CENTOS6
maintainer Liberalman "361509573@qq.com"

run yum install-y mysql-server

Run service Mysqld start &&\
        mysql-e "Grant all privileges on * * to ' socho ' @ '% ' identified by ' Looks137 ';" &&\
        mysql-u root-e "show databases;"

VOLUME ["/data/volume1"]
EXPOSE 3306

Note: At the time of writing this article, the online pull of the MySQL image is still 5.6 version, is now 5.7, there are readers to reflect that the above configuration run does not pass, because MySQL 5.7, the password will be randomly saved to install the MySQL user root directory to create a. Mysql_ In the secret file, the 5.7.6 is saved to Mysqld.log. So at present this configuration has not been used, and so I found 5.7 after the solution to update, sorry.
Run the build command in the directory where the Dockerfile is located to generate the image file, specifying the file name Mysql_test

[root@201 mysql]# Docker build-t mysql_test./
...
Starting mysqld:  [  OK  ]
Database
information_schema
mysql
test
...
Successfully built AC45BC85BE5E
created successfully, view mirror
[root@201 ~]# docker images mysql_test
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
mysql_test          latest              ac45bc85be5e about        a minute ago   374.3 MB

This will create a MySQL server installed on the CENTOS6 system, the image name is Mysql_test, and the CENTOS6 system will be downloaded locally. Okay, start down.

[root@201 ~]# Docker run--name=mysqlserver-d-P mysql_test
a903be8b69fe540395e61a6cff3db16cf944ca1177cb581f7e591247a12b0737
[root@201 ~]# Docker PS
CONTAINER ID        IMAGE               COMMAND                CREATED              STATUS              PORTS                     NAMES
a903be8b69fe        mysql_test         "/usr/bin/mysqld _saf about a   minute ago up about   a minute   0.0.0.0:32769->3306/tcp   

The database in the container has been started and mapped to host 32769 port, connected

[root@201 mysql]# mysql-hlocalhost-uroot-p 32769            
Welcome to the MySQL monitor.  Commands End With; or \g.
Your MySQL Connection ID is 4
Server version:5.1.73 Source distribution

Copyright (c) +, Oracle and/or I TS affiliates. All rights reserved.

Oracle is a registered trademark of the Oracle Corporation and/or its
affiliates. Other names trademarks of their respective
owners.

Type ' help ', ' or ' \h ' for help. Type ' \c ' to clear the current input statement.

Mysql>

The connection is OK, MySQL can use it. Since the use of Docker, we have to reflect its good, so I want to start more than 2 MySQL container, later used to do database cluster also can ah, haha

[root@201 ~]# Docker run-p 3307:3306--name=mysqlserver1-d-P mysql_test1 
8126d375d2f832a0381704424b45669178bc718598c1c8fa8868587455df6fed
[root@201 ~]# Docker run-p 3308:3306--name= Mysqlserver2-d-P mysql_test  
ad920abde38b249e35600bf75f64bc44e7a007b399995e4e0fa17e7c9e5ed1a2
[root@201 ~ ]# Docker PS
CONTAINER ID        IMAGE               COMMAND                CREATED              STATUS              PORTS                     NAMES
ad920abde38b        mysql_test         "/usr/bin/mysqld_saf   seconds ago up      seconds       0.0.0.0:3308->3306/tcp    mysqlserver2
8126d375d2f8        mysql_test         "/usr/bin/mysqld_saf about   a minute ago   up About a minute   0.0.0.0:3307->3306/tcp    mysqlserver1        
a903be8b69fe        mysql_test         "/usr/bin /mysqld_saf   minutes ago up       minutes       0.0.0.0:32769->3306/tcp   

See, start three containers, respectively, by 32769, 3307, 33,083 different port connections are OK, the database independent of each other does not affect.

It is not enough to do the above, if you really want to use multiple MySQL in the production environment, it is best to set up the container in the Dockerfile file in the MySQL datadir directory mount host directory, in order to write the actual generated data to the host, or the container shut down all the data is lost , of course, different containers attached to the host directory must be different, do not confuse the data

VOLUME ["/var/lib/mysql"]

Well, to write a lot of this, first write here, then study how to do elastic scaling and expansion. 5.docker Command

Close container
-Docker Stop container name
-Docker Kill container Name

Kill all the containers that are running
-Docker kill $ (Docker ps-a-Q)

Remove all containers that have stopped
-Docker RM $ (Docker ps-a

Remove all images that have not been hit dangling tags
-Docker RMI $ (Docker images-q-F Dangling=true)

Remove image with Image ID
-Docker Rmi-f

Remove all mirrors
-Docker RMI $ (Docker images-q)

Search and CentOS-related mirrors

[root@

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.