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
Have Questions? Post them here!
- How to docker remove a container when it exits
- Install Python on Alpine Linux - Docker
- How to Rename Docker Image with none TAG and REPOSITORY?
- Remove all stopped containers in Docker using prune command
- [Fix] Docker Error response from daemon: manifest for :latest not found: manifest unknown
- [fix] Error response from daemon: conflict unable to remove repository reference ubuntu container is using its referenced image
- [Docker] Install Python3 on Alpine Linux
- Unable to find image docker latest locally
- How to Stop/Cancel/kill docker image pull
- How to know the Docker Engine Version
- [fix] Docker: OCI runtime exec failed unable to start container process
- [Solution] Alpine Docker apt-get: not found
- Install and Run Cassandra on Docker Desktop
- Install node on Alpine Linux Docker
- How to Copy files from Docker Container to Host System
- Install the minimal Linux on Docker (only 5 mb Alpine Linux)
- How to stop and start a docker container
- Docker - Error response from daemon: You cannot remove a running container
- Open Docker Desktop from macOS Terminal
- How to check installed docker version command
- How to know the Docker Sandbox ID of a Container Network?
- [fix] docker: Error response from daemon: dial unix docker.raw.sock: connect: no such file or directory.
- [Docker] Install Git on Alpine Linux
- Docker MySQL Compose File with Volume Example
- [fix] Docker: Alpine Linux - /bin/sh: bash: not found
- Eclipse Error The JVM Shared Library JavaVirtualMachines does not contain the JNI_CreateJavaVM symbol - Eclipse
- Move from Zsh to Bash shell macOS - MacOS
- How to Set Permanent ruler in Sublime Text - Sublime-Text
- Install OpenSSL on Linux/Ubuntu - Linux
- Java code to check Internet Connection on Android Device Programmatically - Android
- How to exit from nano command - Linux
- How to turn on channel notifications for Microsoft Teams - Teams
- Android Shared Preferences API tutorial - Android