ScanSkill

docker commit

This docker commit command creates a new image from the container’s image when changes in the file are applied. This allows you to debug the container by running interactive mode.

Syntax

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

Note: By default, the container and its processes will be paused while the image associated with the container is being committed.

Here,

Options:

  • --author or -a → Name of author
  • --change or -c → Apply Dockerfile instruction
  • --message or -m → Commit message
  • --pause or -p → Pause container

Examples

  • Committing the container
$ docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS           PORTS       NAMES
a6bece9ff16c   de38122b9c8a   "/docker-entrypoint.…"   13 seconds ago  Up 12 seconds    8080/tcp    happy_varahamihira
$ docker commit a6bece9ff16c sagar/testingimage:version1

ha256:2891ed32189aa7a0172e5012392a9d6d2d2fa6741d5d479ee0e136973a02bf

Now see the image you just committed:

$ docker images

REPOSITORY                    TAG               IMAGE ID       CREATED          SIZE
sagar/testingimage            version1          2891ed32189a   21 seconds ago   23.4MB
nginxinc/nginx-unprivileged   1-alpine          de38122b9c8a   2 months ago     23.4MB
  • Committing the container with new configurations
$ docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS           PORTS       NAMES
a6bece9ff16c   de38122b9c8a   "/docker-entrypoint.…"   9 minutes ago   Up 9 minutes     8080/tcp    happy_varahamihira
$ docker inspect -f "{{ .Config.Env }}" a6bece9ff16c

[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin NGINX_VERSION=1.21.6 NJS_VERSION=0.7.2 PKG_RELEASE=1]

Now, change and commit with the new configurations:

$ docker commit --change "ENV DEBUG=true" a6bece9ff16c  sagar/testingimage:version2

sha256:068f7dfbb5e9b82b5db5fc7544036beaa9cf5c81aee643efac67ffd6bab18881

Now see the image you just committed:

$ docker images

REPOSITORY                    TAG               IMAGE ID       CREATED          SIZE
sagar/testingimage            version2          068f7dfbb5e9   36 seconds ago   23.4MB
sagar/testingimage            version1          2891ed32189a   21 seconds ago   23.4MB
nginxinc/nginx-unprivileged   1-alpine          de38122b9c8a   2 months ago     23.4MB

Inspect new configurations:

$ docker inspect -f "{{ .Config.Env }}" 068f7dfbb5e9

[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin NGINX_VERSION=1.21.6 NJS_VERSION=0.7.2 PKG_RELEASE=1 DEBUG=true]

Here, you can see an image with the new configuration “DEBUG=true”.

  • Commit the container with new CMD and EXPOSE instructions
$ docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS      NAMES
a6bece9ff16c   de38122b9c8a   "/docker-entrypoint.…"   25 minutes ago   Up 25 minutes   8080/tcp   happy_varahamihira

change CMD and EXPOSE:

$ docker commit --change='CMD ["/usr/sbin/nginx", "-g", "daemon off;"]' -c "EXPOSE 8000" a6bece9ff16c  sagar/testingimage:version3

sha256:89ce600ad7ba58f8228cc9db0e910299f9ffb312ff997fc1e169f5249fbe7f56
$ docker images

REPOSITORY                    TAG               IMAGE ID       CREATED          SIZE
sagar/testingimage            version3          89ce600ad7ba   45 seconds ago   23.4MB
sagar/testingimage            version2          068f7dfbb5e9   17 minutes ago   23.4MB
sagar/testingimage            version1          2891ed32189a   22 minutes ago   23.4MB
nginxinc/nginx-unprivileged   1-alpine          de38122b9c8a   2 months ago     23.4MB
$ docker run -d sagar/testingimage:version3

8fd32254c0209ab3d4f42a2c12d2b230ae8786f80cc8b679acb8c5c959cb7940
$ docker ps

CONTAINER ID   IMAGE                         COMMAND                  CREATED          STATUS          PORTS                NAMES
8fd32254c020   sagar/testingimage:version3   "/usr/sbin/nginx -g …"   27 seconds ago   Up 26 seconds   8000/tcp, 8080/tcp   epic_robinson
a6bece9ff16c   de38122b9c8a                  "/docker-entrypoint.…"   31 minutes ago   Up 31 minutes   8080/tcp             happy_varahamihira

Here, you can see CMD is changed to the new one and the extra port is also exposed.