Docker常用命令

0. Linux-centos7上的docker安装[系统先有pip]

centos7-get-docker.sh

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

sudo yum-config-manager --enable docker-ce-edge

sudo yum-config-manager --enable docker-ce-test

sudo yum install docker-ce

systemctl enable docker #设置开机启动

sudo systemctl start docker #启动docker

pip install docker-compose

1. 启动镜像

#以命令行方式启动镜像
docker run -it  image-name /bin/bash
#后台运行镜像
docker run -d  image-name
# 将本机的文件夹挂着到docker镜像的文件夹
docker run -v /home:/docker/home  -d  image-name

2.查看运行的容器

docker ps -a

3. 删除容器

docker ps -a | grep -v 'CONTAINER' | awk '{print $1}' | xargs docker stop
docker ps -a | grep -v 'CONTAINER' | awk '{print $1 }'|xargs docker rm

4. 删除镜像

docker rmi  image-name

5. docker-compose.yml 示例

version: '3.0'
services:
    web:
        image: nginx:latest
        volumes:
            - "./nginx/config/test:/etc/nginx/conf.d"
            - "./nginx/ssl:/etc/ssl"
            - "./web:/usr/share/nginx/html"
        ports:
            - "80:80"  # api http
            - "443:443" # api https
            - "8081:8081" # web http
            - "8082:8082" # web https
            - "8083:8083" # pony https
        restart: always
        depends_on:
            - php
    php:
        image: 10xjzheng/php-mongodb-mqtt-redis:latest
        ports:
            - "9000:9000" # php-fpm
            - "1883:1883" # mqtt
        restart: always
        volumes:
            - "./web:/usr/share/nginx/html"
            - "./mosquitto/config:/etc/mosquitto/"
            - "./mosquitto/log:/var/log/mosquitto"
    jenkins:
        image: jenkins:latest
        ports:
            - "8084:8080"
            - "50000:50000"
        restart: always
        volumes:
            - "./web/jenkins_home:/var/jenkins_home"

6. 在docker-compose.yml 所在的文件夹下执行:

# 1.启动
docker-compose up -d
# 2.创建或者再建服务
docker-compose build
# 3. 停止
docker-compose stop
# 4. 启动已经存在的容器作为一个服务
docker-compose start

7. 构建镜像

example :

  • 1.Dockerfile内容
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

FROM centos:7

RUN yum install -y java-1.8.0-openjdk-devel.x86_64 unzip gettext nmap-ncat openssl, which gnupg, telnet \
 && yum clean all -y

# FROM openjdk:8-jdk
# RUN apt-get update && apt-get install -y --no-install-recommends \
#       bash libapr1 unzip telnet wget gnupg ca-certificates \
#   && rm -rf /var/lib/apt/lists/*

ARG user=rocketmq
ARG group=rocketmq
ARG uid=3000
ARG gid=3000

# RocketMQ is run with user `rocketmq`, uid = 3000
# If you bind mount a volume from the host or a data container,
# ensure you use the same uid
RUN groupadd -g ${gid} ${group} \
    && useradd -u ${uid} -g ${gid} -m -s /bin/bash ${user}

ARG version

# Rocketmq version
ENV ROCKETMQ_VERSION ${version}

# Rocketmq home
ENV ROCKETMQ_HOME  /home/rocketmq/rocketmq-${ROCKETMQ_VERSION}

# Java home
ENV JAVA_HOME  /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64

WORKDIR  ${ROCKETMQ_HOME}

RUN set -eux; \
    curl -s https://dist.apache.org/repos/dist/release/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip -o rocketmq.zip; \
    curl -s https://dist.apache.org/repos/dist/release/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip.asc -o rocketmq.zip.asc; \
    #https://www.apache.org/dist/rocketmq/KEYS
    curl -s https://www.apache.org/dist/rocketmq/KEYS -o KEYS; \
    \
    gpg --import KEYS; \
    gpg --batch --verify rocketmq.zip.asc rocketmq.zip ; \
    unzip rocketmq.zip ; \
    mv rocketmq-all*/* . ; \
    rmdir rocketmq-all*  ; \
    rm rocketmq.zip rocketmq.zip.asc KEYS

# add scripts
COPY scripts/ ${ROCKETMQ_HOME}/bin/

RUN chown -R ${uid}:${gid} ${ROCKETMQ_HOME}

# expose namesrv port
EXPOSE 9876

# add customized scripts for namesrv
RUN mv ${ROCKETMQ_HOME}/bin/runserver-customize.sh ${ROCKETMQ_HOME}/bin/runserver.sh \
 && chmod a+x ${ROCKETMQ_HOME}/bin/runserver.sh \
 && chmod a+x ${ROCKETMQ_HOME}/bin/mqnamesrv

# expose broker ports
EXPOSE 10909 10911

# add customized scripts for broker
RUN mv ${ROCKETMQ_HOME}/bin/runbroker-customize.sh ${ROCKETMQ_HOME}/bin/runbroker.sh \
 && chmod a+x ${ROCKETMQ_HOME}/bin/runbroker.sh \
 && chmod a+x ${ROCKETMQ_HOME}/bin/mqbroker

# export Java options
RUN export JAVA_OPT=" -Duser.home=/opt"

# Add ${JAVA_HOME}/lib/ext as java.ext.dirs
RUN sed -i 's/${JAVA_HOME}\/jre\/lib\/ext/${JAVA_HOME}\/jre\/lib\/ext:${JAVA_HOME}\/lib\/ext/' ${ROCKETMQ_HOME}/bin/tools.sh

USER ${user}

WORKDIR ${ROCKETMQ_HOME}/bin
  • 2.Dockerfile所在目录执行命令:
docker build --no-cache -t rocketmqinc/rocketmq:4.5.0 --build-arg version=v1.0

8. 其它docker命令

docker --help
Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

其中docker execdocker inspect很常用。

停止并删除所有容器

 docker ps -a | grep -v 'CONTAINER' | awk '{print $1}' | xargs docker stop;
docker ps -a  | grep -v 'CONTAINER' | awk '{print $1 }'|xargs docker rm;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容