Start a container in a particular docker network

The following script can be used to start a container inside a particular network.

Usage example:

1
2
./start-in-network redis
./start-in-network vad1mo/hello-world-rest 5050
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/usr/bin/zsh

if [ $# -eq 0 ]
then
    echo "syntax: ./start-in-network container-name [container port binding]"
    echo "You must provide at least container name"
    exit 1
fi

docker_container_id=""
if [ $# -eq 1 ]
then
    docker_container_id=$(docker run -d $1)
else
    docker_container_id=$(docker run -d -p $2:$2 $1)
fi

docker network connect custom_network_name docker_container_id

Read Next

I’m running an experiment for better content recommendations. These are the 3 posts that are most likely to be interesting for you:

All content is licensed under CC BY-NC-SA 4.0. Copying is an act of love - please copy!
More cool websites: Prev | Hotline Webring | Next
Built with Hugo
Theme Stack designed by Jimmy