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
Facing issues? Have Questions? Post them here! I am happy to answer!
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
- Install Docker on Mac using brew cask
- How to know the Docker Sandbox ID of a Container Network?
- How to Rename Docker Image with none TAG and REPOSITORY?
- How to know list of images available on your device
- Docker Alpine Linux and Apache2 Example
- Install Bash on Alpine Linux - Docker
- Docker Run Command Examples - Part 1
- Install the minimal Linux on Docker (only 5 mb Alpine Linux)
- [fix] docker: Error response from daemon: dial unix docker.raw.sock: connect: no such file or directory.
- Install RabbitMQ on Docker
- How to know docker Engine details
- [Fix] Docker Error response from daemon: manifest for :latest not found: manifest unknown
- How to stop and start a docker container
- How to create volume in Docker using Command
- How to know the Docker Engine Version
- [docker] Error response from daemon: No such container
- Install Docker for Mac using Home-brew Cask
- Docker - Incompatible CPU detected - M1/M2 Mac (macOS Sonoma)
- [fix] Docker Desktop App not starting on Mac (macOS)
- Unable to find image docker latest locally
- How to Stop/Cancel/kill docker image pull
- List of what's new in Docker 4.23
- [Docker M1/M2 Mac] qemu-x86_64: Could not open /lib64/ld-linux-x86-64.so.2: No such file or directory AWS CLI
- Install Docker Desktop on M1/M2 Apple Silicon ARM Chip Mac
- Docker - Running in Resource Saver mode
- How to do a Print Screen on Windows 11 to Screencapture - Windows
- Where is Maven .m2 local repository located on Mac - HowTos
- Java: Convert String to InputStream - Java
- JSON Nest Objects Example: JSON Tutorial - Json-Tutorial
- How to clear Microsoft Teams Cache on Mac (macOS) - Teams
- Python: How to create a Dictionary of Lists with Examples - Python
- Android Shared Preferences API tutorial - Android
- How to Export a PowerShell Script/Cmdlet Output to a Text File - Powershell