docker容器删除了,link怎么还在?

dyllen
  • 306

web-server容器是通过run命令创建了,link了db容器,我现在正常停止了web-server容器,然后删除了。
我改成用docker-compose管理,up的时候报错:

ERROR: Service 'web-server' has a link to service 'db-mysql' which is undefined.

这是怎么回事,是说link的东西不会自动删除?我现在需要怎么做呢?

~$ sudo docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                                                          NAMES
4904797541ed        mysql:latest              "docker-entrypoint..."   6 months ago        Up 25 minutes       3306/tcp                                                       db-mysql
version: '2'

services:
    web-server:
        build: .
        hostname: web-server
        restart: always
        container_name: web-server
        links:
            - db-mysql
        ports:
            - "80:80"
            - "443:443"
        volumes:
            - ./htdocs:/var/web/htdocs
            - ./default-root:/var/www/html
            - ./nginx.config:/etc/nginx
回复
阅读 4.2k
3 个回答
✓ 已被采纳

你的db-mysql容器没有,一般如果没有这个容器,就会根据配置对应的image生成容器。该容器对应的image是不是也没有。

你最好把你的docker-compose的内容贴出来看看

这个问题没人知道吗?没人遇到过吗?我擦。

楼上的回答已经讲的很清楚了,db服务没有定义,在compose中link并不是链接容器,而是服务,并且compose启动的时候会单独create network ,也并不是和db-mysql在同一个网络中

$docker ps
$docket network ls 
$docker-compose ps

解决这个问题,2点
1.把db也使用compose管理,定义在compose.yml文件中

db-mysql
    .....

2.把webserver服务加入到和dbmysql一个网络中

例如:
db-backup.yml

version:  '3.0'
services:
# 启动一次备份服务 ./docker-compose restart backup
  backup:
    image: mysql:5.7
    environment:
      MYSQL_HOST: mysql
      MYSQL_DATABASE: package_v1
      MYSQL_USER: root
      MYSQL_PASSWORD: zhaojun
    external_links:
      - dockerprintproject_mysql-db_1:mysql
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./data/backup:/backup:rw
      - ./dockerfiles/backup/:/app:ro
    entrypoint:
      - /bin/bash
    command: /app/backup-default.sh

启动备份

[root@docker docker-print-project]# docker-compose -f db-backup.yml up

产品docker-compose.yml 部分服务

version:  '3.0'
services:
  nginx:
    image: nginx:1.13.3
    volumes:
#网站目录
      - ./app:/var/www/html
      - ./dockerfiles/nginx/conf.d:/etc/nginx/conf.d:ro
      - /etc/localtime:/etc/localtime:ro
      - ./logs/nginx:/var/log/nginx
    ports:
      - "80:80"
    deploy:
      restart_policy:
        condition: any
    command: nginx -g 'daemon off;'

  mysql-db:
    image: mysql:5.7
    volumes:
      - ./data/mysql:/var/lib/mysql:rw
      - /etc/localtime:/etc/localtime:ro
      - ./logs/mysql:/var/lib/mysql-logs:rw
      - ./dockerfiles/mysql/conf.d:/etc/mysql/conf.d:ro
    environment:
        MYSQL_ROOT_PASSWORD: zhaojun
        MYSQL_DATABASE: package_v1
        MYSQL_USER: zhaojun
        MYSQL_PASSWORD: zhaojun
    deploy:
      restart_policy:
        condition: any

docker ps

clipboard.png

宣传栏