跳至主要内容

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

Untitled

Then we will create an image with Vim pre-installed

Check if Vim is installed

Untitled

Install Vim

apt-get update
apt-get install vim -y

Untitled

Vim is installed now

vi --version

Untitled

Press CTRL + D or type exit to Exit the container

Untitled

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

Untitled

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

Untitled

Untitled

Try new image

docker run -it -d --name custom-container-2 custom-image:latest
docker exec -it custom-container-2 /bin/bash
vi --version

Untitled

Publish image

Login

Push

Now we have a custom image custom-image:latest.

Try push this image to DockerHub, got denied.

Untitled

  • 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

    Untitled

  • Push

    # docker push <your username>/custom-image:latest
    docker push uuboyscy/custom-image:latest

    Untitled

    Then find your first image on DockerHub

    Untitled