Detailed basic usage of docker

Source: Internet
Author: User
Tags docker ps docker cp docker run tty mode

1. Install docker

Debian7 install docker

Reference address: http://www.webmaster.me/server/installing-docker-on-debian-

Wheezy-in-60-seconds.html

Echo Deb http://get.docker.io/ubuntu docker main | sudo

Tee/etc/APT/sources. List. d/docker. List
Sudo apt-key adv -- keyserver keyserver.ubuntu.com -- Recv-keys

36a1d7869245c8950f966e92d8576a8ba88d21e9
Sudo apt-Get update
Sudo apt-Get install-y lxc-docker

# Install docker with four lines of commands. Create a Ubuntu virtual system:

Docker pull Ubuntu # Here we pull the image named Ubuntu from the official website. You can also manually

Search for the desired image on the https://index.docker.io.
Docker run-I-t Ubuntu/bin/bash # create a container.-T is a temporary terminal.

Ubuntu 12.04, windows, and MACOs install docker

Reference docker Chinese Document http://www.widuu.com/docker/

2. docker usage practices

2.1 start the container on the test machine and install SSH

Docker run-I-t Ubuntu/bin/bash # containers running in this mode. After exiting, the container will

Disable.
Apt-Get install OpenSSH-server # Install SSH
# Modify the content in the/etc/sshd/sshd_config file
Permitrootlogin Yes
Usepam No

2.2 start SSH and run the container later

Docker run-D-P 50001: 22 <container ID>/usr/sbin/sshd-d
# The container ID can be viewed through docker PS-A, and the top is the latest.

2.3 connect to the container through SSH to install software

SSH [email protected] 50001
# You can use exit to exit the container and run it in the background if you want to install the container.

2.4 stop the container after the service is installed.

Docker stop <container ID> # the container that stops running

2.5 submit the container to generate the latest image.

Docker commit <container ID> debian02 # submit this container to generate a new debian02 image (

This image is the integration of the original image and the container)

2.6 package Images

Docker save debian02>/root/debian02.tar # debian02 image Packaging

2.7 import an image to another machine

Docker load <debian02.tar # import an image
Docker images # view existing images

2.8 start the container

Docker run-H = "redis-test" -- name redis-test-D-P 51000: 22-

P51001: 3306-P 51003: 6379-P 51004: 6381-P 51005: 80-P 51006: 8000-P

51007: 8888 debian02/etc/rc. Local
# Here is the startup command of the test machine, specifying the host name and port ing.
# After startup, a program is installed later. The self-starting command after startup can be placed in the/etc/rc. Local file.
The migration of docker containers is simple and convenient. You can copy and deploy docker containers at will, and you will not be afraid of the new deployment environment any more.

, A bunch of dependent packages.

3. Port ing of docker containers

Since the IP address of the docker container changes every time it is started, it is not applicable to manually adding port ing (isn't it

Can I check the container IP address every time I restart it ?), Therefore, the docker program automatically adds

Add a NAT rule to configure the port to be mapped when creating the container as much as possible in the early stage, as shown below:

Docker run-H = "activemq" -- name activemq-D-P 51000: 22-P

51001: 3306-p 51003: 6379-P 51004: 6381-P 51005: 80-p 51006: 8000-P

51007: 8888 Debian/base/etc/rc. Local
# Here I map MySQL, redis, nginx, and SSH.

To manage docker containers in the future, remember the container name. If the above name is activemq, use

Docker stop and start to control the container process.

Docker stop activemq
Docker start activemq

Of course, you can also prevent docker from changing the container IP address every time you start the container. refer to the following:

Docker network configuration: http://www.open-open.com/lib/view/open1404896485747.html

4. About the automatic running of docker container multi-program upon startup

Each time a docker container is started, the self-starting command must be specified before the container is started. Such as docker

Run-I-t Debian/bin/bash command, only the/bin/bash program will be run, other programs will not

Running is especially difficult for containers that want to run multiple programs.

Automatic Running of multiple programs upon startup:

Replace the preceding startup command with dockerrun-I-t Debian/etc/rc. Local.

Put all the startup commands that need to be started on/etc/rc. Local to enable the multi-program startup

Moved.

Run docker run-D-P 50001: 22 Debian/etc/rc. Local in the background. Note: Run

A command is used to create a new container. To start a container that has been run, run docker

Find the corresponding container ID in PS-A, and then use docker start <container ID>.

5. Relationship between docker containers and Images

No matter what operations are performed in the container, write and delete files. The basic image of the container will not be changed.

This is because docker creates an incremental image from the parent image and only stores changes to each container. Therefore, if you have

A mb parent image. If you have installed 50 MB of additional applications or services in the container, your container only

There are 50 MB, and the parent image is still 300 MB.

However, you can use the dockfile or commit command to generate a new mirror with the incremental image and the parent image.

Image.

Use commit:

Docker commit <container ID> <new image name>

Dockfile usage:

[Email protected]:/data # Cat dockerfile
Fromubuntu/TESTA # This is the basic image
CMD ["/root/start. Sh"] # This is the startup command
[Email protected]:/data # docker build-T <new image name> ./

For more dockfile parameters, refer:

Http://www.tuicool.com/articles/FRvAbe

Http://www.colorscode.net/2014/01/04/howto-build-image-with-automatic-

Startup-ssh-service-from-dockerfile/

6. docker Parameters

Docker
Useage of docker
-D: The default value is false. The debug mode is allowed)
-H the default value is UNIX: // var/run/docker. Sock TCP: // [host [: Port] to bind or

UNIX: // [/path/to/socket] (for binary files). When the Host IP address is host =

[0.0.0.0], (port) Port = [4243] Or Path = [/var/run/docker. Sock] is the default value.

Is the default value.
-API-enable-CORS default flase allows CORS Header Remote API
-B is null by default and attached to an existing bridge. If the 'none' parameter is used, the container is disabled.

Network
-Bip is empty by default. Use the provided CIDR (classlessinter-Domain Routing-No type

Inter-Domain Routing) mark the address to dynamically create a bridge (dcoker0) and conflict with the-B Parameter
-D: The default value is false. The process mode is allowed (daemonmode)
-DNS is empty by default, so docker uses the specified DNS Server
-G: "/var/lib/docker" by default: the root path used as docker
-ICC is true by default, allowing inter-iner to communicate
-Default IP Address "0.0.0.0": the default IP address bound to the Container Port
-Iptables: true by default. Disable docker to add iptables rules.
-MTU: 1500 by default: sets the maximum unit (MTU) for container network transmission)
-P is the file path used by the/var/run/docker. PID process PID by default.
-R: The default value is true.
-S is empty by default, which means docker runs with a specified storage drive
-V: false by default. Print version information and exit.

7. docker Run Command details

Usage: docker run [Options] image [: Tag] [command] [arg...]
Run a command in a new container
-A = map []: adds standard input, output, or error output.
-C = 0: Shared CPU format (relatively important)
-Cidfile = "": writes the container ID to a file.
-D = false: In separation mode, run the container in the background and print the container ID
-E = []: Set Environment Variables
-H = "": Container Host Name
-I = false: Keep the input stream open even if no input stream is attached.
-Privileged = false: permissions extended to the container
-M = "": Memory limit (Format: <number> <optional unit>, unit = B, K, M or

G)
-N = true: allows the image to use the network.
-P = []: match the network port number in the image
-Rm = false: the container is automatically deleted when the container exits (cannot be used with-D)
-T = false: allocate a forged terminal input.
-U = "": User Name or ID
-DNS = []: the DNS server of the custom container
-V = []: Create a mount binding: [host-Dir]: [container-Dir]: [RW | Ro]. If the container

The directory is lost. docker creates a new volume.
-Volumes-from = "": Mount all volumes of the container
-Entrypoint = "": overwrite the default image entry point
-W = "": containers in the working directory
-Lxc-conf = []: add custom-lxc-conf = "lxc. cgroup. cpuset. CPUs = 0, 1"
-Sig-proxy = true: the agent receives all process signals (even in non-tty Mode)
-Expose = []: Leave your host with no open port
-Link = "": connect to another container (Name: alias)
-Name = "": name of the container to be allocated. If this parameter is not specified, a random container name is generated.
-P = false: publish all exposed ports to thehost Interfaces

Port host interface

8. Summary of common docker commands

Docker pull <image name: Tag> # Pull an image from the official website
Docker search <image name> # search for Online available image names

8.1 query containers, images, and logs

Docker top <container> # display processes running in the container
Docker images # query all images. By default, the most recent images are created.
Docker PS # view running containers
Docker PS-L # view the ID of the last exited container
Docker PS-A # view all containers, including those that exit.
Docker logs {container ID | container name} # query all operation records of a container.
Docker logs-f {container ID | container name} # view easy operation records in real time.

8.2 Delete containers and Images

Docker RM $ (docker PS-a-q) # delete all containers
Docker Rm <container name or ID> # delete a single container
Docker RMI <ID> # delete a single image
Docker RMI $ (docker images | grep none | awk '{print $3}' | sort-R)

# Deleting all images

8.3 start and stop the container

Docker stop <container name or ID> # Stop a container
Docker start <container name or ID> # Start a container
Docker kill <container name or ID> # Kill A Container

8.4 container migration

Docker export <container ID>/home/export.tar # Export
CAT/home/export.tar | sudo docker import-busybox-1-export: Latest

# Import the export.tar File
Docker save Debian>/home/save.tar # package the Debian container
Docker load
Comparison between SAVE and export:

Http://www.fanli7.net/a/bianchengyuyan/C__/20140423/452256.html

8.5 run a new container

# Run a new container and name and port ing it. Debian02 image is used as an example.
Docker run-H = "redis-test" -- name redis-test-D-P 51000: 22-

P51001: 3306-P 51003: 6379-P 51004: 6381-P 51005: 80-P 51006: 8000-P

51007: 8888 debian02/etc/rc. Local

# Copy a file from the container. when the container is closed, you can copy the file in it.

Bay.
Sudo docker CP 7bb0e258aefe:/etc/debian_version. # Put

/Etc/debian_version to the current directory.

8.6 create a docker dockfile Image

[Email protected]:/data # Cat dockerfile
From Ubuntu/TESTA # This is the basic image
CMD ["/root/start. Sh"] # This is the startup command
[Email protected]:/data # docker build-T <new image name>./# generate a new image

For more dockfile parameters, refer:

Http://www.tuicool.com/articles/FRvAbe

Http://www.colorscode.net/2014/01/04/howto-build-image-with-automatic-

Startup-ssh-service-from-dockerfile/

Detailed basic usage of docker ()

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.