Codex knows Docker! Successfully Generating An Entire Docker Project (ft. Dockerfile, Docker-Compose.yml, etc.)

:star2: Hello OpenAI Community! :star2:


:beginner: Intro: :beginner:

In this post, I will be exploring the Docker space with Codex to test its ability to generate Dockerfiles and Docker-Compose.yml files to see how it performs when working with Docker-related project files. I’ll try and provide some processes I personally follow that help me talk to Codex to generate desirable output! Docker uses Dockerfiles, Docker-Compose.yml, and other files which act as instructions to create Docker containers with the ability to configure them with volumes, networks, and etc. to allow for easy multi-container set-up and re-deployment on demand!


:whale: What is Docker? :whale:

Docker is an application that runs containers (packaged applications) on your system. Here’s a picture below that visualizes how Docker runs on a system:


Source: Docker.com - Containers


:question::question: What are Dockerfiles and Docker Compose files? :question::question:

Dockerfile Overview:

Definition:

Docker builds images automatically by reading the instructions from a Dockerfile – a text file that contains all commands, in order, needed to build a given image. A Dockerfile adheres to a specific format and set of instructions which you can find at Dockerfile reference.

Basic Dockerfile Example:

# Pull latest Ubuntu image from Docker Hub
FROM ubuntu:latest

# Set environment variables (accessible with $VAR_NAME)
ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$abc

# Expose port 80
EXPOSE 80

# Print each variable: $abc, $def, and $ghi in the terminal
CMD echo abc: $abc \
		def: $def \
		ghi: $ghi

# Reference the variables $def and $ghi to make the directories ./(value stored at $def and $ghi)
RUN mkdir ./$def \
		./$ghi

Run the Dockerfile above like so:

  1. Create a file called ‘dockerfile’ and paste code in
  2. Open terminal and set current directory to path where the Dockerfile is located (e.g. My dockerfile is located inside of 'C:\Docker_Data\Dockerfile_Example')
# cd "Path\to\Dockerfile"
cd C:\Docker_Data\Dockerfile_Example
  1. We can build and run the container by running this code:
# 1. (docker build -q .) returns the name of the docker container
# 2. docker run --rm [name of docker container] will run the docker container that just built and then delete it after running!
docker run --rm (docker build -q .)

>abc: bye def: hello ghi: bye

After the above code runs, the container is deleted because of the --rm flag (See Dockerfile references if you’d like to learn more!)

Docker Compose Overview:

Definition:

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.

Structure:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker-compose up and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary.
    With that being said, I wanted to dive into how Codex would perform when tasked with creating both a Dockerfile and Docker-Compose.yml file.

Docker Compose Example (Really simple Django webserver!):

  1. Create the project directory (anywhere you’d like)
  2. Create the Dockerfile, Docker-Compose.yml file, and requirements.txt file as instructed in the tutorial
# File: requirements.txt
Django>=3.0,<4.0

# File: Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/


# File: Docker-Compose.yml
version: "3"
   
services:
  django_app:
    container_name: django_webserver
    build: .
    command: > 
        python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
  1. Run this inside of the terminal (set working directory to where the files are located first)
# Run app "django_app", then django-admin will create project composeexample
docker-compose run django_app django-admin startproject composeexample .
# Then we officially run the container
docker-compose up --build
  1. Now we can navigate to localhost:8000 and viola!

:test_tube: What is Codex capable of? Well, glad you asked :wink: :test_tube:


:medal_sports: Codex Successfully Creates the Entire Project, the Docker Commands to Set Up the Django Project Correctly, & Runs the Container Smoothly!! :medal_sports:

Prompt:

[Remove everything but ``` (Pre-formatted text conflict in this post)]
You are a Python developer using Docker to create a simple Django webserver in a Docker container. Please create the 3 files and list just the terminal commands needed for the project:

Django Webserver - Docker Files:
# Requirements.txt - 1. List only the module or modules needed to run Django with default settings.
# Dockerfile - 1. Use Python 3. 2. Copy Requirements.txt to '/code' directory. 3. Install Python modules from Requirements.txt file. 4. Copy files from current host directory to '/code'.
# Docker-Compose.yml - 1. Name the Docker container 'django_webserver'. 2. Set the Docker volume for container to the '/code' directory. 3. Expose the default Django port. 4. Run Django server.

Django Webserver - Terminal Commands (Code only):
# List the 'docker-compose' and 'django-admin' command to run the Docker container and then start a new Django project named 'composeexample' inside the current directory '.'
# List the 'docker-compose' command to rebuild and launch the Docker container

Comment 'Finished'
[Remove everything but ``` (Pre-formatted text conflict in this post)]

# Docker Files:

# Requirements.txt (No comments, just code):

Completion:

# Docker Files:

# Requirements.txt (No comments, just code):
Django==2.2.3

# Dockerfile (No comments, just code):
FROM python:3.7.5-stretch

WORKDIR /code

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

# Docker-Compose.yml (No comments, just code):
version: "3"

services:
  django_webserver:
    build: .
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]

# Terminal Commands (Code only):

# List the 'docker-compose' and 'django-admin' command to run the Docker container and then start a new Django project named 'composeexample' inside the current directory '.'
docker-compose run django_webserver django-admin startproject composeexample .

# List the 'docker-compose' command to rebuild and launch the Docker container
docker-compose up -d

# Comment '

Validation Test:

  1. After creating each project file, I opened up Command Prompt and set the current directory to where the files are stored (e.g C:\Docker_Data\Docker-Compose_Example)
  2. Ran the terminal commands I instructed Codex to provide:
# This runs our Docker container named 'django_webserver' and then creates the Django project 'composeexample' inside the current directory!
docker-compose run django_webserver django-admin startproject composeexample .
# Then we can build the Django webserver:
docker-compose up -d

:rocket: Houston, We Have Liftoff! :rocket:

Try it out here! Experiment with Codex and Docker right away with my Complete Docker Project Creation Preset

Tips & Tricks:

Depending on how specific your project is, then Codex may not provide the up-to-date specifics, but helping Codex by guiding it along the way will seriously improve code output. Since I have found a good way to talk to Codex, I decided to see just how powerful Codex can be given certain instructions. Feel free to ask questions so I can help y’all out however I can. I recommend providing an initial overall task, broken down into subtasks to give Codex the context of the entire project and the unique steps needed to help Codex produce code that closely follows the prompt!

It will help if you expose yourself to Docker and read up on how it works and the syntax behind it. Providing Codex with the desired goal is only half the battle; by using proper terminology and keywords that are used frequently by Docker enthusiasts, you’ll be tuning into the core data that Codex was trained on to help it understand what you’re wanting.

Start small if you’re getting serious about generating Docker content, since it can be challenging at first and will take getting used to. Practice makes perfect, so overtime you’ll learn how to improve your Docker skills as well as helping Codex improve on itself!


:gift_heart: Thank you OpenAI Staff for everything y’all do! :gift_heart:

-Nicholas H. (HelpfulDev)

8 Likes