Skip to content

Is it possible to use a non-default bridge network when running CodeBuild locally?

0

When I run codebuild locally it creates the default docker network and volumes as in the below output. Is there a way to use different bridge network and volumes instead of these default ones? I tried modifying the docker command in codebuild_build.sh to add a network (below build command output shows --network mylocaltestingnetwork) but that didn't help as such.

The reason I am trying to use a different network and volumes is because I am using localstack along with it, and the bridge network and volumes need to be accessible from the codebuild local container.

If I configure my localstack to use the agent-resources_default network created by local codebuild then codebuild is able to access localstack. But, I would like to keep the dependency external to both codebuild and localstack by using a separate bridge network.

$ ./codebuild_build.sh -i public.ecr.aws/codebuild/amazonlinux2-x86_64-standard:3.0 -a codebuild-output/ -b buildspec-local.yml -c -p localstack

Build Command:

docker run -it -v /var/run/docker.sock:/var/run/docker.sock -e "IMAGE_NAME=public.ecr.aws/codebuild/amazonlinux2-x86_64-standard:3.0" -e "ARTIFACTS=<repo path>/codebuild-output/" --network mylocaltestingnetwork -e "SOURCE=<repo path>" -e "BUILDSPEC=<repo path>/buildspec-local.yml" -e "AWS_CONFIGURATION=<homedir>/.aws" -e "AWS_PROFILE=localstack" -e "INITIATOR=<user>" public.ecr.aws/codebuild/local-builds:latest


Removing network agent-resources_default
Removing volume agent-resources_source_volume
Removing volume agent-resources_user_volume
Creating network "agent-resources_default" with the default driver
Creating volume "agent-resources_source_volume" with local driver
Creating volume "agent-resources_user_volume" with local driver
Creating agent-resources_agent_1 ... done
Creating agent-resources_build_1 ... done
Attaching to agent-resources_agent_1, agent-resources_build_1
agent_1  | [Container] 2022/01/14 15:57:15 Waiting for agent ping
agent_1  | [Container] 2022/01/14 15:57:17 Waiting for DOWNLOAD_SOURCE
agent_1  | [Container] 2022/01/14 15:57:23 Phase is DOWNLOAD_SOURCE

asked 5 years ago306 views

1 Answer
0

When running AWS CodeBuild locally, it automatically creates default Docker resources such as networks and volumes to facilitate the build process. However, if you need to use a different bridge network and volumes, you typically need to ensure that the containers in your local environment, including localstack, can communicate with each other over the desired network.

Here are steps and considerations to configure CodeBuild to use a different bridge network and volumes:

Using a Different Bridge Network

  1. Create a Docker Network: Before running CodeBuild locally, create a custom Docker bridge network that both CodeBuild and localstack containers can connect to. For example:
docker network create mylocaltestingnetwork

  1. Modify CodeBuild Invocation: When invoking CodeBuild locally using the codebuild_build.sh script, ensure to specify the custom network with the --network option. Update your command to include this network specification:
./codebuild_build.sh \
    -i public.ecr.aws/codebuild/amazonlinux2-x86_64-standard:3.0 \
    -a codebuild-output/ \
    -b buildspec-local.yml \
    -c \
    -p localstack \
    --network mylocaltestingnetwork
  1. Ensure Network Accessibility: Verify that both CodeBuild and localstack containers can access the mylocaltestingnetwork. This might involve configuring your localstack services or any other containers to connect to this network explicitly.

Using Different Volumes If you need to use different volumes:

Create Docker Volumes: Similarly, create Docker volumes as needed before running CodeBuild:

docker volume create my_source_volume
docker volume create my_user_volume

Modify CodeBuild Invocation: Update your codebuild_build.sh script or command to mount these volumes:

./codebuild_build.sh \
    -i public.ecr.aws/codebuild/amazonlinux2-x86_64-standard:3.0 \
    -a codebuild-output/ \
    -b buildspec-local.yml \
    -c \
    -p localstack \
    -v my_source_volume:/path/to/source \
    -v my_user_volume:/path/to/user

Volume Accessibility: Ensure that the paths and permissions for these volumes are correctly set up and accessible by both CodeBuild and localstack containers.

Additional Considerations to take note of :- Network and DNS Resolution: Ensure that containers in your custom network can resolve each other's hostnames or IPs as needed, especially if there are dependencies or services that rely on name resolution.

Security Groups and Firewalls: Check if any security groups or firewall rules on your host machine or Docker daemon might restrict network communication between containers.

By following these steps, you should be able to configure AWS CodeBuild to use a custom Docker bridge network and volumes for your local development environment, ensuring compatibility with localstack or other services as required. Adjust configurations as necessary based on specific networking requirements and constraints of your local setup.

EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.