Docker Commit Command with Examples


Syntax:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Prerequsite:

  1. Should have Docker Installed.
  2. Access to Command Line or Terminal (macOS) with sudo access
  3. Basic understanding of Docker commands: ps, images, run, build e.t.c.
  4. Have some images available or use docker pull to get some.

Options:

% docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith ")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)

Examples:

First let's pull an Alpine Linux Image from the docker hub,

$ docker pull alpine:latest

Let us run the image and create a container with name myHttpServer with -i interactive and -t pseudo terminal flags,

% docker run -it --name myHttpServer alpine

Next, let's install Apache2 HTTP Server and curl on it.

# apk add apache2

fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/aarch64/APKINDEX.tar.gz
(1/6) Installing libuuid (2.38-r1)
(2/6) Installing apr (1.7.0-r2)
...
...
Executing apache2-2.4.54-r0.pre-install
Executing busybox-1.35.0-r17.trigger
OK: 9 MiB in 20 packages


# apk add curl

Let us move to the /var/www/localhost/htdocs directory where we would create an html file.

# cd /var/www/localhost/htdocs

# echo "<Hello from Docker!</h2>" > index.html

Now exit the container. Note the image should be in stopped/exited mode.

% docker ps -a
CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS  PORTS     NAMES
ddf725665dd3   alpine    "/bin/sh"   5 minutes ago   Exited (0)   myHttpServer

Create new image using Commit

Now that we have our container with all the changes we needed, we are good to create a new image of it using docker commit command.

% docker commit --author "Code2care" -m "Image with Apache2 configured" ddf725665dd3 mynewhttpserver:latest
sha256:b6c42b0a63305151c6ad1f498ff007a92e59934e796976fe82db0dc9e2c8ebf8 

Let's checkout our new image,

% docker images

REPOSITORY          TAG       IMAGE ID       CREATED              SIZE
mynewhttpserver     latest    894f6ace7158   12 seconds ago       11.2MB

We are all good to test the image now,

% docker run -it --rm mynewhttpserver

# /usr/sbin/httpd

# curl -I localhost
HTTP/1.1 200 OK
Date: Wed, 23 Nov 2022 05:41:00 GMT
Server: Apache/2.4.54 (Unix)
Last-Modified: Wed, 23 Nov 2022 05:17:48 GMT
ETag: "1c-5ee1c68c83700"
Accept-Ranges: bytes
Content-Length: 28
Content-Type: text/html
Docker Commit Command Example

Related Posts

  1. Docker Alpine Linux and Apache2 Example
  2. How to stop and start a docker container
  3. Install Bash on Alpine Linux - Docker
  4. Remove a docker image
  5. How to create volume in Docker using Command

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap