Docker is an open source application container engine that allows developers to pack their applications and rely on a transplant container, and then publish them on any popular Linux machine to achieve virtualization. -Baidu Encyclopedia
docker structure is as follows
The core of
is to use LXC to achieve the function of similar virtual machines to use hardware resources more efficiently.
docker ispect docker_container_id
Installation
- Install docker
This article is mainly for Mac installation, similar to Linux.- Checking system version
To install Docker Toolbox, you need OSX version> = 10.8 “mountain lion”, the viewing method isClick Apple icon in the upper left corner-> About this mac
If the system does not meet the requirements, please update the system first. - GetInstallation file
- Double -click to open the installation mirror, the installation wizard will display the list of program to be installed
Press the ‘Continue‘ to provide custom installation options. What programs do you choose to install:
It is recommended to install all.
ClickInstall
and enter the password will start installation. - Installation is completed
Among them, Docker Quickstart Terminal is equivalent to entering the Docker’s shortcut through the terminal, eliminating the operation and other operations. KITEMATIC is the graphical interface of the docker, which can be used to download the image and directly use it from the Docker Hub. Below are command behavior interfaces
- Checking system version
Basic operation
- Install Image
sudo docker pull ubuntu:12.04
equivalent
sudo docker pull registery.hub.docker.com/ubuntu:12.04
is downloaded from the default warehouse; but sometimes the official warehouse download is slow, you can use the following command to specify the warehouse:
sudo docker pull dl.dockerpool.com:5000/ubuntu:12.04 -
docker run commonly used parameters
- ‘-i’:Keep STDIN open even if not attached
- ‘-t’: Allocate a PSEUDO-TTY allocated a console interaction with container
For interactive operations in the container, such as shell script. We must use -i -t to apply for a console and container for data interaction. But when interacting with the same container through the pipeline, you cannot use -t. For example, the following command
echo test | docker run -i busybox cat
- ‘-d’ to run the container in the background mode (DETACHED MODE). The current terminal window is no longer monitored by the container.
--rm
: The container will be deleted immediately after the container is terminated- ‘-a = []’. What streams are mounted (‘stdin’, ’sTDOUT’, ’STDERR’) when you do n’t bring this parameter
- –name ’: Naming the container
docker run –name wei -itd busybox
- docker PS lists the docker container during operation
- ’-A ‘lists all containers
- ‘q” lists the container ID of all processes,
- Function: For example, kill all processes
docker kill $(docker ps -q)
- docker mirror
- Modify existing mirror
- Use the downloaded mirror to start the container first
sudo docker run -it training/sinatra /bin/bash
Remember easy ID, add JSON and GEM applications to containers
[email protected]:/#: gem install json
Use EXIT to exit the updated copy after exiting
sudo docker commit -m “Add json gem” -a “Docker Newbee” 0b2616b0e5a8 ouruser/sinatra:test
‘-m’ parameter description information, ’-A’ specify the updated user information, and then the container ID used to create a mirror image. Finally, the target mirror warehouse name and TAG information are specified. - Use dockerfile to create a mirror image
- New directory and dockerfile
mkdirsinatra
$ touch Dockerfile - Dockerfile Each specified to create a mirroring layer,up to 127 layers
# This is a comment
FROM ubuntu:14.04
MAINTAINER Docker Newbee [email protected]# Author
RUN apt-get -qq update
RUN apt-get -qqy install ruby ruby-dev
RUN gem intall sinatra
# put my local web site in myApp folder to /var/www
ADD myApp /var/WWW
# expose httpd port
EXPOSE 80
# the command to run
CMD [“/usr/sbin/appachectl”, “-D”, “FOREGROUND”] - Build generated mirror image
sudo docker build -t=”ouruser/sinatra:v2”
‘-t’ s mark add tag, specify the new mirror user information - Use a new mirror to start the container
sudo docker run -it ouruser/sinatra:v2 /bin/bash
docker tag 5db5f8471261 ouruser/sinatra:devel
Change the mirror label - Upload mirror image
sudo docker push ouruser/sinatra - Store and load
- sudo docker save -o ubuntu_14.04.tar ubuntu:14.04
- load
sudo docker load –input ubuntu_14.04.tar
or
sudo docker load < ubuntu_14.04.tar - Remove
docker rmi training/sinatra
Note:Before deleting the image, you should firstdocker rm
All containers depend on the image
- docker container
- New and startup: Docker Run
sudo docker run -it ubuntu:14.04 /bin/bash
Start a bash terminal to allow users to interact - Started procedures for termination: Docker Start
- Guardian State Operation: ’-D’
Obtain the output information of the container:
sudo docker logs insane_babbage
Enter the guardian container:
docker attach container_name - Termination container:
docker stop container_name
Restart the container:
docker start container_name
Restart container:
docker restart container_name
The operating container will be terminated and then restarted - Export and import container
- Export container snapshot to local:
docker export container_id > container_name.tar - import container from local express photos:
docker import container_path example/imagerepo
- Export container snapshot to local:
- Delete container
docker rm container_id
Delete the front container needs to stop
- New and startup: Docker Run
- Docker Hub
- Login:
docker login
After entering the user name and password, the authentication information is stored in the local user directory.dockercfg
File - Basic operation
- search mirror:
docker search centos - download mirror
docker pull centos - Upload mirror image
docker push image_name - automatic creation strategy
- search mirror:
- Private warehouse
- Login:
-
docker data management
-
two ways:
- data volume (data volumes)
- Data Volumes Container
-
-
data volume
- Features
- Data volume can be shared between containers
- The modification of the data volume will take effect immediately
- The update of the data volume will not affect the mirror
The
- volume will always exist until there is no container to use it
- Create data volume
Use whendocker run
command-v
Parameters create and mount the data volume into the container. You can use multiple data volumes multiple times in a RUN
docker run -d -P –name web -v /webapp training/webapp python app.py
Note:can be used in dockerfileVOLUME
Add one or more new rolls to the container created by the mirror. - Mount a host directory to the container
docker run -d -P –name web -v /src/webapp:/opt/webapp python app.py
will be the host/src/webapp
Directory Mount it to the container/opt/wepapp
Last add:ro
Mount it to read only
DockerfileDo not support this usage - Mount a host file to the container
- Data volume container
If there are some continuous updated data that needs to be shared between the containers, it is best to create a data volume container. The data volume container is a container for providing data volume for other containers.
- Creation:
docker run -d -v /dbdata –name dbdata training/postgres echo Data-only Container
# 11
docker run -d –volumes-from dbdata –name db1 training/postgres - Delete
If the container (DBDATA, DB1) that is mounted will not be automatically deleted, you need to delete the last one that is still hung on itdocker rm -v
- Backup
docker run –volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar … - recovery
# Create a container dbdata2 with a data volume
docker run -v /dbdata –name dbdata2 ubuntu /bin/bash
# Create another container, mount DBDATA2 container, and use Untar to decompress the backup file to the container volume loaded
docker run –volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar - docker network management
- external access container
Specify port mapping through -P or -P parameters-P
: Randomly map a network port of 49000-49900 to the interior container open-p
: Specify the port, support format withip:hostPort:containerPort|ip::containerPort|hostPort:containerPort
also use UDP mark to specify UDP ports-p 127.0.0.1:5000:5000/udp
- Use
docker port
to view the port configuration of the current mapping
docker port nostalgic_morse 5000
127.0.0.1:49155
- container interconnection
# Create a database container
docker run -d –name db training /postgres
# 1
docker run -d -P –name web –link db:db training/webapp python app.py
Sug container DB and web containers to establish an interconnection relationship,--link
Format:--link name:alias
,name
is the name of the container to be linked. Alias is the alias of this link. The connected container can bedocker ps
- View connection information
- Environment variable
docker run –rm –name web2 –link db:db training /webapp env
get environmental variables
…
DB_NAME=/web2/db
DB_PORT=tcp://172.17.0.5:5432
DB_PORT_5000_TCP=tcp://172.17.0.5:5432
…
prefix uses an upper -writing connection alias - Father container web2
/etc/hoists
File
docker run -it –rm –link db:db training/webapp /bin/bash
# cat /etc/hosts
172.17.0.7 aed84ee21bde
…
172.17.0.5 db
Here are two hosts. The first is the web container. The web container uses ID as its host name. The second is the IP and host name of the DB container.ping db
to test the connection status of Web2 and DB containers
- external access container
- Advanced network configuration
- network structure
- Configuration DNS
Docker By mounting 3 related configuration files to the new container to make the host host DNS update all the container DNS passes/etc/resolv.conf
File is immediately updated-h HOSTNAME | --hostname=HOSTNAME
The host name of the specified container will be written to the container/etc/hosts
and/etc/hostname
--link=CONTAINER_NAME:ALIAS
: Add a host name of another containerIn
/etc/hosts
, the process of allowing the new container can be connected to it with the host name Aliasdns=IP_ADDRESS
: Let the container analyze not there/etc/hosts
host namedns-search=DOMAIN
: Set the search domain of the container
- container access external network
container access to the external network requires local forwarding support, through
sysctl net.ipv4.ip_forward(Mac: sysctl net.inet.ip.forwarding)
View whether there is any turnover forwarding, 1 is to open 0 to close, open through the following command
sysctl -w net.ipv4.ip_forward=1(Mac:sysctl net.inet.ip.forwarding=1)
- network structure