How to create a Docker Image?

Efficient Docker Image Creation: A Step-by-Step Guide

Muhammed ÇELİK

--

Docker simplifies the deployment of applications by packaging them into containers. Creating Docker images is a crucial step in this process, and an organized file structure makes the entire workflow more manageable. In this guide, we’ll walk through the steps for Docker image creation using various methods and tools.

Docker GitHub

Step 1: Organize Your Project Directory

Start by creating the necessary files and folders in your project directory. A well-organized structure simplifies image creation and management.

# Create a project directory
mkdir your-project
cd your-project

# Create a Dockerfile for your image
touch Dockerfile

# Create a Dockerfile for your image
touch docker-compose.yml

# Visual Studio Code (Dev Containers), Github Codespaces
mkdir .devcontainer
touch .devcontainer/devcontainer.json

# for Docker Desktop GUI (Dev Environments)
mkdir .docker
cp docker-compose.yml .docker/docker-compose.yml
# Optionally, add other files if needed (docker-compose.yml, .docker/config.json, etc.)

Step 2: Edit Configuration Files

Edit configuration files according to your project requirements. Depending on your preference, you can use a Dockerfile or docker-compose.yml file.

Dockerfile Example:

# Dockerfile
# Use an official base image with your desired runtime
FROM python:3.10-slim

# Add your project files
COPY . /app

# Set working directory
WORKDIR /app

# Copy requirements.txt and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Expose the port on which your application will run
EXPOSE 8000

# Specify commands to run your application
CMD ["python", "app.py"]

docker-compose.yml Example:

# docker-compose.yml
# Specifies the Docker Compose file version
version: '3'

# Defines the services that make up your devcontainer
services:
your-service:
# Here 2 Option (Image or Dockerfile)
# image: jupyter/pyspark-notebook:latest
build:
# Parent directory as sets the build context for the image creation and
# affects which files are available to Docker during the build process.
context: ./
# Path is relative to docker-compose.yml with defined parent context.
dockerfile: Dockerfile
args: {}
ports:
- "8000:8000" # Map the host port to the container port
volumes:
- .:/app
environment:
- DEBUG=true # Set environment variables if needed
# Add other configurations as needed

Note: “context: ./” define for your project requirements.

Step 3: Open Your Project

Open your project in your preferred development environment. This guide covers three methods: Terminal, Visual Studio Code, and Docker Desktop GUI (Dev Enviroments).

To effectively manage Docker image creation, follow these steps:

1. Terminal (Docker Desktop)
2. Visual Studio Code (Dev Containers), Github Codespaces
3. Docker Desktop GUI (Dev Environments)

1. Terminal Commands:

Build with Dockerfile

Open a terminal and run the following command to build your Docker image using a Dockerfile:

# [Optional] Syntax Image name: [docker-user-name/]image-name[:image-tag]
docker build -t your-image-name -f Dockerfile .

Build with Docker Compose

If you have a docker-compose.yml file, use the following command:

docker-compose -f docker-compose.yml build

Run Docker Image for testing

# with GPU support
# docker run --gpus all -it -p 9696:9696 [docker-user-name/]image-name[:image-tag]
  • -d: Runs the container in detached mode, meaning it runs in the background.
  • -it: Runs the container in interactive mode, meaning it allowing you to access the container's terminal.
  • -p 9696:9696: Port sharing between host and container, Maps port 9696 on the host to port 9696 on the container.
  • --gpus all: Enables GPU support for the container.
  • --rm: Automatically removes the container when it exits. This is useful for cleaning up temporary containers.

2. Visual Studio Code (Dev Containers), also usable Github Codespaces:

Take advantage of Visual Studio Code for a seamless development experience. Create the necessary files in a .devcontainer folder:

  • .devcontainer/devcontainer.json
  • docker-compose.yml
  • Dockerfile

Build with devcontainer.json

Here, We have three option IMAGE, docker-compose.yml or Dockerfile:

{
// More info: https://containers.dev/guide/dockerfile
// [Optionally] Can be use Dockerfile or Docker Compose file.
// "dockerComposeFile": "../docker-compose.yaml", "service": "app-dev", "features": {},
"image": "python:3.10-slim", "runArgs": ["--user", "root"],

// [Optional] Codespaces, Host hardware requirements.
"hostRequirements": {"cpus": 2},

// [Optional] Can be use terminal ports 'forwardPorts' option.
"forwardPorts": [8888], "portsAttributes": { "8888": {"label": "Jupyter Notebook", "requireLocalPort": true, "onAutoForward": "ignore"} },

// [Optional] A command string or list of command arguments to run on the host machine (when the container is creating).
// "initializeCommand": "",

// [Optional] A command string or list of command arguments to run respectively on the host machine (after the container is created).
// "onCreateCommand": "", "updateContentCommand": "", "postCreateCommand": "",
"postCreateCommand": "apt-get update && apt-get install -y git git-lfs"
}

Also can be added More Options for Image Creation via Different [SUBFOLDERs] for GitHub CODESPACES:

.devcontainer/[SUBFOLDERs]/.devcontainer.json

3. Docker Desktop GUI (Dev Environments):

For Docker Desktop GUI, copy docker-compose.yml file in .docker folder:

  • .docker/docker-compose.yml
  • Dockerfile

Docker Desktop (GUI Dev Environments 4.13 and later):

Changes to Dev Environments with Docker Desktop 4.13:

Simplified Configuration: Docker has simplified how you configure your dev environment project.

Compose File: All you need to get started is a compose-dev.yaml file.

Automatic Migration: If you have an existing project with a .docker/ folder, it is automatically migrated the next time you launch.

Migration from .docker/docker-compose.yaml: If you are using .docker/docker-compose.yaml, it is moved to ../compose-dev.yaml.

Migration from .docker/config.json: If you are using .docker/config.json, a ../compose-dev.yaml file is created with a single service named "app". It is configured to use the image or Dockerfile referenced in the JSON as a starting point.

Documentation: For more details, refer to the Docker Desktop documentation.

By following this structure and using the provided commands, you ensure that Visual Studio Code and Docker Desktop recognize and use the appropriate files for their functionalities.

This organized file structure allows for efficient management of configuration files and Docker-related files for both Visual Studio Code and Docker Desktop.

--

--