1. Run a software into Docker Container

  1. Pull the ubuntu image

    docker pull ubuntu:24.04 
    docker run --name my_ubuntu -it ubuntu:24.04 bash
    
    2. run a container from the ubuntu image

    docker exec -it my_ubuntu bash
    
    3. install golang inside the container

    apt update
    apt install golang
    go version           # verify install
    
    4. copy file from host machine to docker container

    1. make a directory in container alt text

    2. copy file from host to container
      alt text

    3. Verify the copied file alt text

  2. run the file

    alt text alt text

server.go
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "Hello World!")
    })

    fmt.Println("Server is running on :8080")
    http.ListenAndServe(":8080", nil)
}

so Finally we run a software (server.go) into Docker Container.


2. Create a Docker Image from a Running or Stopped Container

When to Use docker commit

  • To quickly save changes made inside a container
  • For experiments, debugging, or learning purposes

Step to create

1. Commit a Container to an Image

General Syntax : docker commit <container_id_or_name> <image_name>

Example

docker ps
docker commit ed06a473b329 go_server
alt text

This command creates a new image named go_server from the current state of the container ed06a473b329 (named my_ubuntu).

2. Verify the Created Image

docker images

alt text

3. Important Observation

The go_server image is exactly the snapshot of the my_ubuntu container at the time of commit.

  • All installed packages
  • All file changes
  • All configurations

Everything is baked into the go_server image.


Limitation of docker commit

Suppose:

  • Your application file (e.g., server.go) changes

Then you must:

  1. Run a container again
  2. Manually edit files
  3. Reinstall dependencies (Go, libraries)
  4. Commit the container again

This process is manual, error-prone, and not reproducible.

Recommended Solution: Dockerfile

Instead of using docker commit, use a Dockerfile

Why Dockerfile is Better

docker commit Dockerfile
Manual process Automated
Not reproducible Fully reproducible
Hard to maintain Easy to maintain
Not version-controlled Version-controlled

Using Dockerfile : Run a software into Docker Container

  1. Create the Dockerfile

    Dockerfile

    FROM ubuntu:24.04 
    RUN apt update
    RUN apt install -y golang
    WORKDIR /here
    COPY ./server.go  ./server.go
    

    Dockerfile Instruction Explanation
    FROM ubuntu:24.04 Sets Ubuntu 24.04 as the base image that provides the Linux environment for the container.
    RUN apt update Updates the system package index so new software can be installed correctly.
    RUN apt install -y golang Installs the Go inside the container. The -y flag avoids manual confirmation.
    WORKDIR /here Creates (if not exists) and switches to /here as the default working directory.
    COPY ./server.go ./server.go Copies server.go from the host machine into the container’s working directory.
  2. Build the Dockerfile and set name alt text

  3. Verfiy the image alt text

  4. Run the newly build image alt text

  5. Verify the output alt text