Modify container and create image
Commit a new image
Start a container
docker run -it -d --name custom-container python:3.11-slim-bullseye
Do something in container
First get into container
docker exec -it custom-container /bin/bash
Then we will create an image with Vim pre-installed
Check if Vim is installed
Install Vim
apt-get update
apt-get install vim -y
Vim is installed now
vi --version
Press CTRL + D or type exit to Exit the container
Commit
How commit command works
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Aliases:
docker container commit, docker commit
Options:
-a, --author string Author (e.g., "John Hannibal Smith <[email protected]>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
Get container ID
Commit a new image
docker commit <your container id> custom-image:vim-installed
docker commit <your container id> custom-image:do-something
docker commit <your container id> custom-image:latest
Try new image
docker run -it -d --name custom-container-2 custom-image:latest
docker exec -it custom-container-2 /bin/bash
vi --version
Publish image
Login
-
Create token
https://hub.docker.com/settings/security
-
Authenticate
docker login -u <your username>
Push
Now we have a custom image custom-image:latest.
Try push this image to DockerHub, got denied.
-
Tag image
Tag the image with your user name as prefix
# docker tag custom-image:latest <your username>/custom-image:latest
docker tag custom-image:latest uuboyscy/custom-image:latest -
Push
# docker push <your username>/custom-image:latest
docker push uuboyscy/custom-image:latestThen find your first image on DockerHub