How to link docker containers on build?

前端 未结 4 579
悲哀的现实
悲哀的现实 2021-02-07 01:44

I linked my app container to postgres on run:

docker run --link postgres:postgres someproject/develop

and it worked fine.

相关标签:
4条回答
  • 2021-02-07 02:27

    I got the answer from the docker contributor Brian Goff:

    docker run -d --name mydb postgres
    docker run --rm --link mydb:db myrailsapp rake db:migrate
    docker run -d --name myapp --link mydb:db myrailsapp
    

    This is going to fire up postgres. Fire up a container which does the db migration and immediately exits and removes itself. Fires up the rails app.

    Think of the build process like compiling an application. You don't seed data into a database as part of the compilation phase.

    0 讨论(0)
  • 2021-02-07 02:29

    I had a similar issue. I wanted to speed up image builds with the help of apt-cacher. It runs in its own container and somehow other images, which I built, had to communicate with it.

    The solution was to publish apt-cacher port on all interfaces. This includes e.g. docker0, which is available to intermediate containers spawned during image build.

    Example Dockerfile:

    FROM debian:8
    
    RUN ping -c 2 172.17.0.1
    

    And this is how it builds:

    $ docker build - <dock
    Sending build context to Docker daemon 2.048 kB
    Step 1 : FROM debian:8
     ---> 47af6ca8a14a
    Step 2 : RUN ping -c 2 172.17.0.1
     ---> Running in 4f56ce7c7b63
    PING 172.17.0.1 (172.17.0.1): 56 data bytes
    64 bytes from 172.17.0.1: icmp_seq=0 ttl=64 time=0.117 ms
    64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.130 ms
    --- 172.17.0.1 ping statistics ---
    2 packets transmitted, 2 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 0.117/0.123/0.130/0.000 ms
     ---> 5c73a36a0a6a
    Removing intermediate container 4f56ce7c7b63
    
    0 讨论(0)
  • 2021-02-07 02:39

    True, but docker build does accept the --network option.

    You can put your prerequisite containers on a named / custom network, e.g.:

    docker network create whatever
    docker run --network whatever --name postgres [etc.] someproject/develop
    

    Then build on that network:

    docker build --network whatever [etc.]
    

    Works well.

    0 讨论(0)
  • 2021-02-07 02:45

    You can not do this. You could either build a child image of postgres, or update the database every time you start the container.

    0 讨论(0)
提交回复
热议问题