Docker Commit Images

Committing your changes to the container, just like commiting code.

We'll cover the following

In the last lesson, we got access to the shell of the container and were able to run commands. Since we can use the container like a normal Linux machine, let’s work in it exactly as we work in any normal Linux machine.

If you have exited the container, type docker ps -a and check the first entry or the entry which has bash in its command column.

$ docker ps -a
CONTAINER ID    IMAGE       COMMAND  CREATED      STATUS            PORTS    NAMES
38c22dec7c2a    python:3.5  "bash"   32 seconds ago Exited (0) 6 seconds ago    laughing_grothendieck

Copy the container id and type docker start <container_id>.

$ docker start 38c22dec7c2a
38c22dec7c2a

$ docker ps
CONTAINER ID    IMAGE       COMMAND  CREATED      STATUS            PORTS    NAMES
38c22dec7c2a    python:3.5  "bash"   42 seconds ago Up 2 seconds    laughing_grothendieck

It will start the container, and you can then access the shell using docker exec -it <conatiner_id> bash.

$ docker exec -it 38c22dec7c2a bash
root@38c22dec7c2a:/#

Make sure you work with the same container every time. If the container is stopped start it using the docker start <container_id/name> command and work in it using docker exec -it <container_id/name> bash command.

Now, that we are in the container, we will install a couple of things in the container so that we can write some python script in it.

Type following commands to install a text editor called nano in our new mini Linux system.

  • apt update - Update current packages on system.
  • apt install nano - Install the nano editor.

Docker commit

To commit changes to the container and create a new image from it, we need to change something in a container. We will not make it complex to work with it. We will create a Python program that will print today’s date and run it. After that, we will commit the changes, and it will create a new image for us, which will have our Python program in it forever.

So, get back to the shell in the container if you are not in it.

Type nano todays_date.py to create a new file. It will open an editor in the shell.

If you don’t know Python language, instead of typing copy following code in ‘todays_date.py’ file and paste it in the editor. Type ctrl+x to exit the editor and type y to confirm or save changes.

Get hands-on with 1400+ tech skills courses.