Docker on Ubuntu 16.04 LTS – [Part 02] – Images
In my previous article, I stopped at the Docker Container management. In this article basically I will be touching the Docker Images.
A typical traditional Linux system to run, it basically needs two file systems:
- boot file system (bootfs)
- root file system (rootfs)
The bootfs contains the boot loader and the kernel. The user never makes any changes to the boot file system. In fact, soon after the boot process is complete, the entire kernel is in memory, and the boot file system is unmounted to free up the RAM associated with the initrd disk image.
The rootfs includes the typical directory structure we associate with Unix-like operating systems:
/dev, /proc, /bin, /etc, /lib, /usr, and
/tmp plus all the configuration files, binaries and libraries required to run user applications.
Here the root file system is mounted read-only and then switched to read-write after boot. In Docker, the root file system stays in read-only mode, and Docker takes advantage of a union mount to add more read-only filesystems onto the root file system and appear as only one file system. This gives the complete control of the all the file systems, which are added to the Docker container. Finally when a container is created/ launched, Docker will mount a read-write file system on top of all the other file system image layers. All the changes made to underneath images are basically stored in this read-write layer. However, the original copy is retained in underneath layers without and changes written to them. This read-write layer + other layers underneath + base layer basically form a Docker container. (See the image below)
In Part 01 of this article, we created a container with an ubuntu image. You can see all the available images by,
$ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 07f8e8c5e660 4 weeks ago 188.3 MB
Seems now you have the “latest” ubuntu image with you. If you want a specific version image then you need to specify it as a TAG. i.e. ubuntu:12.04. So lets try that now.
$ sudo docker run -t -i --name new_container ubuntu:12.04 /bin/bash
Now, check the image status
$ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 07f8e8c5e660 4 weeks ago 188.3 MB ubuntu 12.04 ac6b0eaa3203 4 weeks ago 132.5 MB
Further, if you want to delete one of the created images you can use,
$ sudo docker rmi <image-id>
While interacting with multiple images, there can be many unnamed and unwanted (dangling) images are being created. These can take a lot of space in the disk. Hence periodically it is required to purge them from the system. Use the following to do the trick:
$ docker rmi $(docker images -q -f dangling=true)
Up to now, we used Docker run command to create containers. While creating it downloads the given image from the Docker Hub. This downloading to the local basically takes some time. If you want to save this time when you are creating the container, you can have the alternate route by first pulling the required template from the Docker Hub and then creating the container using the downloaded image. So here are the steps
// Pulling the image from Docker Hub $ sudo docker pull fedora // Creating a Docker Container using the pulled image $ sudo docker run -i -t fedora /bin/bash
Now if you see, you will have 3 containers.
$ sudo docker ps -a 86476cec9907 fedora:latest --- 4d8b96d1f8b1 ubuntu:12.04 --- c607547adce2 ubuntu:latest ---
Building your own Docker Images
There are two ways to do this.
Method (1). Via docker commit command
Method (2). Via docker build command with a Dockerfile (This is the recommended method)
To test method (1), first create a container using an already pulled image and then do some alteration to the image and then execute docker commit.
// Creating a Docker Container using an image $ sudo docker run -i -t ubuntu:14.04 /bin/bash // Alter the image $ apt-get -yqq update $ apt-get -y install apache2 // Committing the changes to the image // Here, crishantha is the account created // in the Docker Hub repository // you may use Docker Hub or any other Docker repo // 9b48a2b8850f is the Container ID of the contatiner $ sudo docker commit 9b48a2b8850f crishantha/apache2 // List the Docker images // Here the Docker altered image ID is shown $ sudo docker images crishantha/apache2 crishantha/apache2 latest 0a33454e78e4 ....
To test method (2), you may create a Dockerfile at a given directory and specify the required changes needed for the image. For example, the Dockerfile can have the following lines, for an Ubuntu 14.04 image. FROM basically pulls the ubuntu 14.04 image and then RUN commands basically executes and add more layers to the image. EXPOSE will basically expose port 80 from the container.
Before executing the Dockerfile, it is good to create a new directory and create the DockerFile within that directory. Here the directory is called static_web.
FROM ubuntu:14.04 RUN apt-get update RUN apt-get install -y apache2 EXPOSE 80
Once this is done, you can execute the Dockerfile by,
$ sudo docker build -t="crishantha/static_web" .
If all successful, it will return a image ID and further you can see it using docker images crishantha/static_web
Checking the Docker Image History
You can further check the history of the image by executing docker history <image Name/ image ID>
$ sudo docker history crishantha/static_web
Now you can execute the container by,
$ sudo docker run -d -p 80 --name static_web crishantha/static_web /usr/sbin/apache2ctl -D FOREGROUND
The above will run as a detached process and you would see this by executing docker ps and you would see it running in the background as a Docker process.
If you use Nginx instead of Apache2 as the web server, you may add nginx -g “daemon off;” to the command. The
daemon off; directive tells Nginx to stay in the foreground. For containers this is useful as best practice is for one container = one process. One server (container) has only one service.
Pushing Docker Images
Once an image is created we can always push it a Docker repository. If you are registered with Docker Hub, it is quite easy to push your image to it. Since it is a public repository, then if anyone interested can just pull it to his/her own Docker repository.
// If you have not already not logged in, // Here the username is the one you registered // with Docker Hub $ sudo docker login Username: crishantha Password: xxxxxxxxxx // If login is successful $ sudo docker push crishantha/static_web
If all successful, you may see it is available in the Docker Hub.
Pulling Docker Images
Once it is push to Docker Hub, you may pull to to any other instance which runs Docker.
$ sudo docker pull crishantha/static_web
Automated Builds in Docker Hub Repositories
In addition to push our images from our set ups to Docker Hub, it allow us to automate Docker image builds within Docker Hub by connecting to external repositories. (private or public)
However, the Docker Hub automated builds should have a Dockerfile attached to it in the specific build folder. The build will go through based on the Dockerfile build that you specify here. Once the build is completed you can see the build log as well.
Comments are closed.