3

I have 12 docker containers. When I run individually I can use --link to connect some of them, like web app link with mysql db. But when I run them as service in docker swarm (like docker create service)I can not link them because --link is not available with docker service create command. If I use docker-compose.yml file to run all container, I can link up. But here is another issue. Suppose I have 12 different containers (components)in docker-compose file or docker stack how can I update a single container or components? Do I have to redeploy whole docker stack?

Goforseeking
  • 385
  • 1
  • 3
  • 15
  • I use separate yml files and then use docker-compose -f file1.yml -f file2.yml etc. You can create a named network in Docker and then have your services all use that network and hence see each other. – Chris Cousins Aug 27 '18 at 01:28
  • Possible duplicate of [docker-compose: difference between network and link](https://stackoverflow.com/questions/41294305/docker-compose-difference-between-network-and-link) – David Maze Aug 27 '18 at 09:32
  • Yea link is depreciated. https://docs.docker.com/network/links/ – Bret Fisher Aug 29 '18 at 04:41
  • when you want to update a swarm stack, you use the same `docker stack deploy` command and it will only update the changed networks and services. – Bret Fisher Aug 29 '18 at 04:42

1 Answers1

3

You only need to put your containers in the same network in each docker-compose.yml file.

First you will need to create a network with docker:

docker network create -d bridge custom 

After you will need to change the network in your docker-compose files to the new network, and if you want you can use external_links like as the example:

example file 1:

version: '3'
services:
  php-server:
    container_name: myphp
    image: devilxatoms/taproject:latest
    ports:
     - "9000:9000"
    external_links:
      - mysql:mysql
    networks:
      - custom

networks:
  custom:
    external: true

example file 2:

version: '3'
    services:
      mysql:
        container_name: mydb
        image: mysql:latest
        restart: always
        environment:
          - MYSQL_ROOT_PASSWORD=root
        ports:
         - "3306:3306"
        networks:
          - custom

    networks:
      custom:
        external: true

To test it, i only accessed to the bash of my mysql container and send a ping to the another container:

MySQL Container:

# ping php-server
PING php-server (172.26.0.3) 56(84) bytes of data.
64 bytes from myphp.custom (172.26.0.3): icmp_seq=1 ttl=64 time=0.124 ms
64 bytes from myphp.custom (172.26.0.3): icmp_seq=2 ttl=64 time=0.368 ms
64 bytes from myphp.custom (172.26.0.3): icmp_seq=3 ttl=64 time=0.071 ms
64 bytes from myphp.custom (172.26.0.3): icmp_seq=4 ttl=64 time=0.136 ms
^C
--- php-server ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3094ms
rtt min/avg/max/mdev = 0.071/0.174/0.368/0.115 ms

PHP Container:

# ping mysql
PING mysql (172.26.0.2) 56(84) bytes of data.
64 bytes from mydb.custom (172.26.0.2): icmp_seq=1 ttl=64 time=0.075 ms
64 bytes from mydb.custom (172.26.0.2): icmp_seq=2 ttl=64 time=0.107 ms
64 bytes from mydb.custom (172.26.0.2): icmp_seq=3 ttl=64 time=0.109 ms
^C
--- mysql ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2094ms
rtt min/avg/max/mdev = 0.075/0.097/0.109/0.015 ms

For update a specific services you can update your docker-compose file with your changes and tell to docker-compose wich of your services need to update with this line:

docker-compose up -d --no-deps <service_name> 

The -d is Detached mode: Run containers in the background, print new container names.

The --no-deps will not start linked services.

references: https://docs.docker.com/compose/compose-file/#external_links

Brayan Caldera
  • 2,001
  • 2
  • 11
  • 20
  • Thank Brayan, Just consider one app consists of 12 containers. Off and On, we need to update some containers (components by building jars as CI/CD). What is best solution in this case. I mean how can I update a running service. – Goforseeking Aug 27 '18 at 06:10
  • For update a specific services you can update your docker-compose file with your updates and tell to docker-compose wich of your services need to update with this line: docker-compose up -d --no-deps The -d is Detached mode: Run containers in the background, print new container names. The --no-deps will not start linked services. – Brayan Caldera Aug 27 '18 at 12:32