Goforseeking
Goforseeking

Reputation: 435

How to link one docker service with other docker service?

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?

Upvotes: 3

Views: 4863

Answers (1)

Brayan Caldera
Brayan Caldera

Reputation: 2199

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

Upvotes: 3

Related Questions