Reproducible Builds with Web Servers

Anudha Mittal
3 min readOct 24, 2024

Docker built successfully from dockerfile in 20 seconds.

There was a suggested improvement in the dockerfile to add JSON syntax.

WARN: JSONArgsRecommended: JSON arguments recommended for CMD to prevent unintended behavior related to OS signals

Web server config file

There is a COPY command in the dockerfile used to build the image. The custom nginx.conf file is in the same dir as dockerfile, and is copied to the default location for config files, in the linux dir /etc.

COPY nginx.conf /etc/nginx/nginx.conf

Convenient because config file and installation instructions to recreate an environment can be kept at one location in the file system.

There is a bash script that launches the containers, this is where the scalability first presents itself.

#!/bin/bash

# Create a string, add an incremental character subsequently in the for loop to create unique ports.
BASE_PORT=8000

# This for loop defines the number of containers. It launches them sequentially.
for i in {1..50}; do
CONTAINER_NAME=”nginx-python-$i”
PYTHON_PORT=$((BASE_PORT + i)) # Each container gets a unique port

echo “Launching container $CONTAINER_NAME with Python server on port $PYTHON_PORT”

# Run the container
docker run -d — name $CONTAINER_NAME -p $((8080 + i)):80 -e PYTHON_PORT=$PYTHON_PORT IMAGE_NAME
done

To parallelize, add character ‘&’

# Run the container in the background
docker run -d — name $CONTAINER_NAME -p $((8080 + i)):80 -e PYTHON_PORT=$PYTHON_PORT IMAGE_NAME &
done

# Wait for all background processes to finish
wait

echo “All containers launched in parallel!”

OR use GNU parallel.

Modify the bash script:

  1. create a function to launch_container()
  2. export the function to GNU parallel

launch_container() {
i=$1
CONTAINER_NAME=”nginx-python-$i”
PYTHON_PORT=$((BASE_PORT + i)) # Each container gets a unique port

echo “Launching container $CONTAINER_NAME with Python server on port $PYTHON_PORT”
docker run -d — name $CONTAINER_NAME -p $((8080 + i)):80 -e PYTHON_PORT=$PYTHON_PORT my-nginx-python
}
export -f launch_container
parallel launch_container ::: {1..50}

When the containers are running, output from $ docker ps:

Halted running 48 of the containers using this command:

docker ps -q | head -n 48 | xargs docker stop

docker ps → shows only 2 containers.

The ‘docker stop’ above chooses 48 containers at random or based on some order; to user-define or admin-define which 48, need a for-loop, which can be run using a bash script.

The mapping of ports:

Mapping ports

Checking on the status of containers, ‘Status’ column can have value: created, up , and exited, in some cases with information on the time length since status changed.

Status from Docker Dashboard: docker ps

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response