Simple Docker build example

Example of simple docker build and run

This blog shows a very simple example of build a docker image and serve as http server using python.

This also covers the concepts of environment variable, volumes and installing packages in building the docker image.

Create Files

Create the following two files with the contents below:

Dockerfile

########################
FROM ubuntu:latest

# Install python3
RUN apt-get update
RUN apt-get install -y python3

# Set default values for environment variables
ENV PORT 8080

# Copy Default Content
COPY . /var/www/

# Change to working directory
WORKDIR /var/www/

# Expose Ports
EXPOSE $PORT

# Run Http Server
CMD python3 -m http.server $PORT
########################

sample-file.txt

My Sample Content.....

Build the image

docker build --pull -t example/python-http-server:latest -f Dockerfile .

Run the image as container

docker run --rm -it --name my-python-http-server -p "8080:8080" example/python-http-server:latest

Access the server

curl -s -X GET "http://localhost:8080/sample-file.txt"

Change the port by overriding environment variable

docker run --rm -it --name my-python-http-server -p "9080:9080" -e "PORT=9080" example/python-http-server:latest

curl -s -X GET "http://localhost:9080/sample-file.txt"

Change the content directory by overriding volume

docker run --rm -it --name my-python-http-server -p "8080:8080" -v "/home/some_directory/:/var/www/" example/python-http-server:latest

curl -s -X GET "http://localhost:8080"

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux