Yes you can create a custom docker image using a running container.
There are two ways of creating a custom docker image:
- Using a Running Docker container.
- Using a Dockerfile.
To create an image from a running docker container :
- First start the container in attached mode:
docker run -it --name forcommitcontainer ubuntu
- Where "forcommitcontainer" is the name of the container created from "ubuntu" image.
- Once the container is started in the attached mode, you can configure whatever you want, make the necessary installations and configurations.
- Say you want an ubuntu image with Vim installed, we would be making the necessary installations and saving the state of the container. In order to install Vim in the container,
apt-get install vim
- Now you just need to save the state of the container. Everytime you install this custom ubuntu image, vim package will be installed automatically.
- Come out of the container:
Ctrl+p Ctrl+q
- And to save the state of the container
docker commit forcommitcontainer devopstrainer/ubuntu:vim
- "forcommitcontainer" -> name of the container, "devopstrainer" -> username , "ubuntu" -> repository name along with a tag "vim", so that by seeing itself you understand that its an ubuntu image with vim package installed.
- I can push it to my repositories on dockerhub later on if required.
docker images
- A new image is created (devopstrainer/ubuntu) which can be pushed to the dockerhub. Now run this
docker run -it devopstrainer/ubuntu:vim
- When you check you will find that vim will be automatically installed on the new container that is created using the custom ubuntu image.
- You can check the status of the container :
docker history devopstrainer/ubuntu:vim
This will show the date when the container is created, image, created by, size etc.