Linux Learning Summary (71) docker-3

Source: Internet
Author: User
Tags curl docker compose docker run

Dockerfile creating a mirrored –dockerfile format
  1. From//specifies which base image is based
    Format from <image> or from <image>:<tag>
    From CentOS
    From Centos:latest
  2. Maintainer//Specify author information
    Format maintain <name>, such as
    Maintainer LV [email protected]
  3. RUN//Mirror operation instructions
    The format is run <command> or run ["Executable", "param1", "param2"], such as
    RUN Yum Install httpd
    RUN ["/bin/bash", "-C", "echo Hello"]
  4. CMD//Three formats:
    CMD ["Executable", "param1", "param2"]
    CMD command param1 param2
    CMD ["param1", "param2"]
    Run and Cmd look quite similar, but CMD is used to specify the command to use when the container starts, only one. Like what
    CMD ["/bin/bash", "/usr/local/nginx/sbin/nginx", "-C", "/usr/local/nginx/conf/nginx.conf"]
  5. EXPOSE
    The format is EXPOSE <port> [<port> ...], such as
    EXPOSE 22 80 8443
    This is used to specify the port to be mapped out, such as inside the container we started the sshd and nginx, so we need to 22 and 80 port burst out. This needs to work with-p (uppercase), which means that when you start the container, you need to add-p to automatically assign it. If you want to specify a specific port, you can also use-p (lowercase) to specify.
  6. Env
    Format ENV <key> <value>, for example
    ENV Path/usr/local/mysql/bin: $PATH
    It mainly provides an environment variable for subsequent run directives, and we can also define some custom variables
    ENV mysql_version 5.6
  7. Add format add <src> <dest>
    Copy a local file or directory to a directory in the container. Where SRC is the relative path to the directory where Dockerfile resides, it can also be a URL. Like what
    ADD <conf/vhosts> </usr/local/nginx/conf>
  8. COPY
    Format with ADD
    Using the same method as add, the difference is that it does not support URLs
  9. entrypoint format similar to cmd
    The command to execute when the container starts, it is similar to CMD, and only one entry takes effect if more than one of the last bars is written. Different from CMD is:
    CMD can be overridden by the Docker Run command, and entrypoint cannot be overwritten. For example, the container name is aming
    We specify the following cmd in the Dockerfile:
    CMD ["/bin/echo", "Test"]
    The command to start the container is the Docker run aming, which outputs the test
    If the command to start the container is Docker run-it aming/bin/bash nothing will output
    EntryPoint will not be overwritten and will be executed before the command specified by CMD or Docker run
    entrypoint ["echo", "test"]
    Docker run-it aming 123
    The test 123 is output, which is equivalent to executing the command echo Test 123
  10. VOLUME
    Format VOLUME ["/data"]
    Create a mount point that can be mounted from a local host or other container.
  11. USER
    Format USER Daemon
    Specify the user who runs the container
  12. Workdir
    Format Workdir/path/to/workdir
    Specify working directory for subsequent run, CMD, or entrypoint two dockerfile create a mirror –dockerfile build Nginx instance

    1 Edit Dockerfile file

      Vim Dockerfile#set the base image to Centosfrom centos# File author/maintainermaintainer lv# Install Necessar Y toolsrun yum install-y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel# install Nginxadd Http://nginx. Org/download/nginx-1.8.0.tar.gz. RUN tar zxvf nginx-1.8.0.tar.gzrun mkdir-p/usr/local/nginxrun cd nginx-1.8.0 &&/configure--prefix=/usr/local /nginx && make && make Installrun rm-fv/usr/local/nginx/conf/nginx.confcopy. nginx_conf/usr/local/ NGINX/CONF/NGINX.CONF//Prepare a configuration file in the current directory. # Expose Portsexpose 80# Set The default command to execute when creating a new containerentrypoint/usr/local/nginx/sbin/ Nginx && tail-f/etc/passwd  

    Note: Here tail-f/etc/passwd function is to allow the container to continue to operate. If not added, the container will exit just after it is started.
    2 Creating the Image:
    docker build -t centos_nginxCreate a mirror.
    docker imagesYou can see our new image.
    docker run -itd -p 81:80 centos_nginx bashStart container
    3 Simple test
    If the nginx in the container, the configuration file, the default virtual host is correct, we will be able to access the Web directly on the host
    curl 127.0.0.1:81The default page is accessed.

    Three-use Docker compose Deployment Services

    Docker compose allows us to quickly and efficiently manage the start, stop, restart, and other operations of the container, similar to the shell script under Linux, based on YAML syntax, in which we can describe the architecture of the application, such as what image, data volume, network mode, listening port and other information. We can define a multi-container application (such as Jumpserver) in a compose file, and then start the application with that compose.
    Install the Compose method as follows

    curl -L https://github.com/docker/compose/releases/download/1.17.0-rc1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-composechmod 755 !$

    docker-compose versionView version Information
    Compose distinguishes between version 1 and version 2 (Compose 1.6.0+,docker Engine 1.10.0+). Version 2 supports more directives. Version 1 does not declare that the versions default to "Version 1". Version 1 will be deprecated in the future.
    Vim DOCKER-COMPOSE.YML//

    version: "2"     //定义使用的命令版本services:app1:    // 定义第一个应用。名字没有实际含义image: centos_nginx   // 定义要使用的镜像ports:  - "8080:80"    // 定义容器和宿主机的端口映射关系networks:  - "net1"   //定义容器使用的网络volumes:  - /data/:/data   // 定义数据卷容器共享目录及和宿主机目录映射关系,相当于-v选项app2:image: centosnetworks:  - "net2"volumes:  - /data/:/data1entrypoint: tail -f /etc/passwd  //启动容器时附加执行一条命令networks:net1:driver: bridge  //定义容器使用的网络模型为桥接net2:driver: bridge

    Note: The Tail-f/etc/passwd here works the same as the Centos_nginx created above. It is also intended to keep the container running. App1 inside the mirror Centos_nginx already have this sentence, no longer repeat. There are no similar statements in the CentOS image in App2, so it needs to be added to the entrypoint.
    docker-compose up -dcan start two containers//up equivalent to create again start,-D drop into the background.
    Docker-compose--help//View available options
    Docker-compose PS/DOWN/STOP/START/RM
    Reference document on the Docker-compose syntax http://www.web3.xin/index/article/182.html

Linux Learning Summary (71) docker-3

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.