Docker Community Forums

Share and learn in the Docker community.

Docker-compose down - what does it exactly do?

So I have a littlebit confusion in understanding what docker-compose down does.

As per the docs, it says that it removes all the containers, networks, volumes, and images created by up.
But it doesn’t delete volumes and networks if they are defined as external.

Does this means if I write below code, it defines volumes and networks as external?

volumes:
  wordpress:
  db:
networks:
  wp_network:

Below is my docker-compose.yml file code. I managed to create it by following documentation and also some tutorials and this is working pretty nicely. As I did docker-compose down, it removed the images and containers. But when I did docker-compose up again, I had my wordpress site data intact and not deleted. Which means the down command ignored the volumes and networks?

version: '3'
services:
  wordpress:
    image: wordpress
    depends_on:
      - db
    restart: always
    ports:
      - 80:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: admin
      WORDPRESS_DB_PASSWORD: admin
      WORDPRESS_DB_NAME: wordpressdb
    volumes:
      - wordpress:/var/www/html
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    container_name: wp_site
    networks:
      - wp_network
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: wordpressdb
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - db:/var/lib/mysql
    container_name: wp_mysql
    networks:
      - wp_network
  phpmyadmin:
    image: phpmyadmin
    depends_on:
      - db
    restart: always
    ports:
      - 8080:80
    environment:
      MYSQL_ROOT_PASSWORD: root
      UPLOAD_LIMIT: 256mb
    container_name: wp_phpmyadmin
    networks:
      - wp_network
volumes:
  wordpress:
  db:
networks:
  wp_network:

It’s just I am still new to this, I am trying to clear few doubts.

By default, the only things removed are:

* Containers for services defined in the Compose file
* Networks defined in the  `networks`  section of the Compose file
* The default network, if one is used

Networks and volumes defined as  `external`  are never removed.
    -v, --volumes           Remove named volumes declared in the `volumes`
                            section of the Compose file and anonymous volumes
                            attached to containers.

Stops containers and removes containers, networks, volumes, and images created by up . By default, the only things removed are: Containers for services defined in the Compose file.

If you want define a network as ‘external’. First yo must create it with another compose or docker command.
Then, at the docker-compose file that you want to use that network; you must setup that network with the ‘external’ param in true.