BOBOBK

Customize, Modify, and Upload Your Own Docker Image

MISCELLANEOUS

What is Docker?
Docker is a tool that can package applications along with their virtual containers and dependencies, allowing them to run on any Linux server. This helps achieve flexibility and portability, enabling applications to run anywhere, whether on public cloud, private cloud, or standalone machines.
After getting a free Docker instance from arukas.io, I started experimenting with creating my own public Docker image and finally running it inside Arukas’s Docker environment. This process involves knowledge of:

  1. Installing Docker
  2. Pulling a Docker image
  3. Modifying an existing Docker image and saving it
  4. Uploading the image to a Docker registry and verifying it
  5. Other common Docker commands

1. Install and Start Docker Service

yum install docker -y
service docker start

2. Pull a Docker Image

Here we use nginx as an example:

docker pull nginx

3. Modify Existing Docker Image and Save It

Start a container:

docker run -it -d --name my_nginx nginx

Command explanation:

  • -it: interactive terminal mode
  • -d: run container in detached (background) mode
  • --name: assign a name to the container; if not specified, Docker generates a random name
  • nginx: the image to use

Enter the container and add custom content to index.html:

docker exec -it my_nginx /bin/bash
echo "my first docker :my-nginxnby bobobk.com" > /usr/share/nginx/html/index.html

This simple Docker customization is now done. Exit the container.

Now you can create a new Docker image:
Save the existing container as a new image:

docker commit my_nginx chunjiangmuke/nginx:latest

Here, chunjiangmuke is the Docker Hub username.

4. Verify and Upload the Image to Docker Registry

List images:

docker images

You can see the image has been saved. Next is to upload it to the Docker registry.
First, login to Docker. If you don’t have an account, register at Docker official website.

docker login

Enter your username and password when prompted.

Upload the my_nginx image to the public registry:

docker push chunjiangmuke/nginx:latest

Open the browser and login to Docker to see the uploaded image:

Done, successfully uploaded.

5. Run nginx Docker container and map ports

docker run -p 8080:80 --name nginx -d chunjiangmuke/nginx
wget 127.0.0.1:8080
cat index.html

You should see the content:
my first docker :nginx

Parameters explanation:

  • -p port mapping, maps container port 80 to local port 8080 (-p 8080:80)
  • --name assigns a container name, can be any string
  • -d runs container in detached mode
  • The last argument is the image name, here the custom saved chunjiangmuke/nginx

6. Other Common Docker Commands

List all container IDs:

docker ps -aq

Stop all containers:

docker stop $(docker ps -aq)

Remove all containers:

docker rm $(docker ps -aq)

Remove all images:

docker rmi $(docker images -q)

This article introduced the complete process to modify and upload your first Docker image from scratch.

Related