One: the use of containers

  • The Docker service is enabledsudo service docker start
  • Run an application inside the containerDocker run Ubuntu /bin/echo "Hello world"Output helloWorld in the Ubuntu image (if there is a local image, the local image will be used, if not, the corresponding image execution instructions will be automatically downloaded from the repository)
  • Run container interactiondocker run -i -t ubuntu /bin/bashInteraction with ubuntu images -t Specifies a terminal -i in the new container that allows you to interact with standard input within the container
  • Container startupdocker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done", Docker background start (-d) an Ubuntu container, the container continuously output helloWorld returns a long string for the container id after the docker ps can view the running container find the container ID and name and passdocker logs id(or name)View the output hello World
  • The container to stopdocker stop id(or name)
  • Run a Web applicationdocker run -d -P -p 5000:5000 training/webapp python app.py-d lets the container run in the background. -P maps the network port the container uses to the host we use
  • Docker top ID (or name)
  • docker inspect id (or name)Viewing the underlying information of a container returns a JSON for the details of the container
  • docker stop id(or name)Stopping a containerdocker rm id(or name)Delete a container (must be stopped)
  • Enter container interactiondocker attach id

Two: the use of mirror

  • View the Docker imagedocker images(List of local mirrors)
  • An image may have multiple tags such as Ubuntu 14.04 15.04 if you want to use the specified tag versionDocker run Ubuntu :15.04 /bin/echo "Hello world"The latest version is used without specifying the location
  • Get a new image for exampleDocker pull ubuntu: 13.10
  • Search image can go todocker hubSearches can also be made using the Docker search command for exampledocker search httpd
  • Drag take mirrordocker pull httpd
  • Create a new image
    1. Update from and create the container, and commit the changed image
    2. Use the Dockerfike directive to create a new image
  • Update image
    1. Create a container using the imageDocker run -t -i Ubuntu :15.10 /bin/bash
    2. Used in container interactionsapt-get updateAfter the update, type exit to exit the container
    3. Use commit to commit the updated imagedocker commit -m="has update" -a="root" name(or id) root/ubuntu:v2 -mA Specifies the image author ID container ID root/ Ubuntu :v2 Specifies the name of the target image to be created. After the update, docker images can be used to view the new image root/ Ubuntu :v2docker run -t -i root/ubuntu:v2 /bin/bashCreate a container using the image we just updated and start the new container interaction
  • The command to build the image is docker build
    1. Create a Dockerfile and write the creation rules
    2. Create a mirror imageDocker build -t root/ Ubuntu: 0.0.xxx-t Specifies the name of the target image to be created.XXX Specifies the directory where the Dockerfile file resides. You can specify the absolute path of the Dockerfile file and view it through Docker images after the file is created successfully
  • Setting a Mirror Labeldocker tag id root/centos:devUser name, repository source name, and new tag name

Three: container links

  • Network port mapping creates a container for Python applicationsdocker run -d -P training/webapp python app.py -p 5000:5000-p indicates that a container port is bound to a specified host port. -P is a high-end port randomly mapped from a container port to a host. Alternatively, we can specify the network address for the container binding, such as binding 127.0.0.1. For example,Docker run -d -p 127.0.0.1:5001:5002 training/ webApp Python app.pyThis allows us to access port 5002 in the container by accessing port 5001 locally
  • We can specify the container name by –namedocker run -d -P --name lifei training/webapp python app.pyThe name of the created container is not randomly assigned by the system

Dockerfile writing specification

  • Although instructions ignore case, it is recommended to use uppercase. All formats are instructions + Spaces + arguments
  • MAINTAINERUsed to specify the name and contact information of the maintainer
  • FROM <image>/<image>:<tag>Set the basic mirror for subsequent command use should be written at the top for exampleFROM ubuntu
  • RUNThis will execute the command in the “from” image above and commit the result to be used laterRUN <command>(This command runs a shell – ‘/bin/sh -c’) orRUN ["executable", "param1", "param2"]thisRUNThe command is equivalent to being executed outsidedocker run image_name command
  • CMD and ENTRYPOINT, sets the command to be executed when the container starts,ENTRYPOINT: indicates the command to be executed during the initialization of the mirror. The command cannot be overwritten.CMD: indicates the default parameter for running a mirrorENTRYPOINT/CMDCan only exist once in the file, and the last valid multiple exist, only the last valid, the other invalid! Multiple commands need to be initialized, separated from each other by ampersand, but the last command needs to be run for infinity, remember!
  • USERFor example, if you specify the user to run memcached, you can use the aboveENTRYPOINT or CMDTo achieve:ENTRYPOINT ["memcached", "-u", "daemon"]There’s a better way to do itENTRYPOINT ["memcached"] USER daemon
  • EXPOSEThe –link command sets a port to be exposed in the running image, which docker uses to link two containers
  • ENVUse this to set up environment changesdockerfileThe generatedimagenewcontainer, can be accessed throughdocker inspect CONTAINER ID

    See this environment variable can also be passed in thedocker runTo set or modify environment variables
  • ADDCopy files from SRC tocontainerthedestPath:ADD <src> <dest> <src>Is a path relative to the source directory being built, which can be a file or directory path, or a remote file URL<dest>containerAbsolute path in
  • VOLUMECreate a mount point for shared directories such asVOLUME ["/data"] Docker run - it - v/home/lifei/PyProjects: / SRC/PyProjects - p, 5000:5000 lifei/ubuntu: 0.1 / bin/bashSRC is the mount point, and local PyProjects are mounted to PyProjects of Docker and shared
  • WORKDIR WORKDIR /path/to/workdirconfigurationRUN.CMD.ENTRYPOINTCommand to set the current working path. You can set the current working path multiple timesWORKDIRThe command For example,WORKDIR /a WORKDIR b WORKDIR c RUN pwdIs actually/a/b/cperformpwd