How To Create And Manage Docker Images
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed docker system commands.
https://cloudaffaire.com/how-to-monitor-docker-container-using-docker-system-command/
In this blog post, we will discuss docker images. We will also create a docker image using Dockerfile.
Prerequisite for this demo:
- One AWS EC2 instance with Linux 2 AMI and internet access.
- Docker
What is docker image?
An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the Ubuntu image but installs the Apache web server and your application, as well as the configuration details needed to make your application run. You might create your own images or you might only use those created by others and published in a registry.
What is Dockerfile?
To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast when compared to other virtualization technologies.
How To Create And Manage Docker Images Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
##-------------------------- ## Docker: Image Management ##-------------------------- ## docker image [COMMAND] ## Create a Dockerfile ## Please refer below docker documentation for Dockerfile usage and syntax ## https://docs.docker.com/engine/reference/builder/ vi Dockerfile -------------------------------------------------- FROM ubuntu:16.04 # Install dependencies RUN apt-get update RUN apt-get -y install apache2 # Install apache and write hello world message RUN echo 'Hello World from docker' > /var/www/html/index.html # Configure apache RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh RUN echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh RUN echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh RUN echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh RUN chmod 755 /root/run_apache.sh EXPOSE 80 CMD /root/run_apache.sh -------------------------------------------------- :wq ## Build an image from a Dockerfile ## docker image build -t docker image build -t myimage . #Dockerfile defination given above ## list images docker image ls -a ## Display detailed information on one or more images ## docker image inspect docker image inspect myimage ## Show the history of an image ## docker image history docker image history myimage ## Import the contents from a tarball to create a filesystem image ## docker image import docker image import --message "New image imported from tarball" /home/ec2-user/myWEBSERVER_backup.tar ## Save one or more images to a tar archive ## docker image save -o docker image save -o myimage_backup.tar myimage ## load an image from a tar archive or STDIN ## docker image load < docker image load < myimage_backup.tar ## Create an account in https://hub.docker.com/ ## Log in to the Docker public registry on your local machine. docker login ## Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE ## docker image tag image:tag hub/image:tag docker image tag myimage:latest cloudaffaire/myimage:latest ## Push an image or a repository to a registry ## docker image push hub/image:tag docker image push cloudaffaire/myimage:latest |
1 2 3 4 5 6 7 8 9 10 |
## Pull an image or a repository from a registry ## docker image pull hub/image:tag docker image pull cloudaffaire/myimage:latest ## Remove unused images docker image prune ## Remove one or more images ## docker image rm docker image rm myimage |
Hope you have enjoyed this article. In the next blog post, we will discuss the docker network.
To get more details on docker, please refer below docker documentation