Syntax:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Prerequsite:
- Should have Docker Installed.
- Access to Command Line or Terminal (macOS) with sudo access
- Basic understanding of Docker commands: ps, images, run, build e.t.c.
- 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

Related Posts
- Docker Alpine Linux and Apache2 Example
- How to stop and start a docker container
- Install Bash on Alpine Linux - Docker
- Remove a docker image
- How to create volume in Docker using Command
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!