Continuing with my Docker for Noobs series, the Part 4 of this explores more on how you can work with Docker Images. In Part 3, we touch-based a little on the images but would deep dive a bit into them in this part.
Docker for Noobs Series:
Part 4- Working with Images and Containers
Images form the basis of Docker development or deployment workflow. These help you create an environment which has its individuality and can be used for development or sharing purposes. Hence, it is important to understand the concept of images so that one can start using or even creating them.
As you have recently installed Docker, you don’t have any images yet with you. In order to take your first step with images (and possibly Docker development), you can either download one or create your own. We would visit the latter option in a future series as it is a bit advanced concept at this stage. So, we would focus on using Docker images that are already available out there and play with them a bit.
Working with Pre-built Docker Images
For working with pre-built images, you need a location from where you can get one. The best place to find such ready to use Docker images is undoubtedly the Docker Hub. This is Docker’s own repository of images that not only consists of official images created by them but also the ones created by community or individuals like you. You can use these images as a Base Image to build your own images on top of it or even get some full-fledged images that can be used directly with your projects. We explore below how we can use the Docker Hub images and what are some of the major commands to keep in mind when working with Docker images.
1. Search Docker Registry for images
$ docker search [NAME]
This will search Docker Registry or Hub for images with matching name.
2. Pull an image from Docker Registry
$ docker pull [IMAGE_NAME]
If you have found the image that you searched for, or know the name already, use this command to pull it from Docker Hub and save on your machine.
This will pull the latest version of the image. In case you are looking for a specific version, you may add the tag along with the name. The command will looks something like below:
$ docker pull [NAME]:[TAG]
3. Run an Image into a Container
$ docker run [IMAGE_NAME]
This will run the image into a container. If the image is not present locally on your machine, it will pull it from the Hub, Create a Container from it and finally Start it as well.
Docker RUN command has several options which enhance the usage and provide you with more features. Some of the commonly used commands along with RUN are:
- $ docker run -it [IMAGE_NAME] – This will run a Docker container in an interactive mode, so that you may interact with it via the Terminal.
- $ docker run -d [IMAGE_NAME] – Runs a container in detached mode, such that it may continue running in the background.
- $ docker run -P [IMAGE_NAME] – Helps to allocate random ports to the container. One may use -p instead of -P option to pass parameters to specify custom ports to be used.
- $ docker run –name [MY_CONTAINER_NAME] [IMAGE_NAME] – Can be used to specify a name of choice to the container instead of using the one that is assigned by Docker itself.
4. Create a Container, but not Start it
$ docker create [IMAGE_NAME]
Unlike the RUN command above, this will not start the container. It will create a container from the specified image and will download it from the Hub if not present locally.
6. Get details of an image
$ docker inspect [IMAGE_NAME]
This command will give low-level details about the image and also works with inspecting containers.
7. Remove images from system
$ docker rmi [IMAGE_NAME]
The command removes image from the system. In case there is an error removing the image, you can use an optional flag -f to force deletion of image.
With these 6 commands talked about, we come to end of our Part 4 of the series. These commands will probably be the initial ones and the most used ones that you will be working with ad you work with Docker images.