Here, we will be building our own container images. For doing this, we have two different ways, namely manual method and automated method. Let us see both ways.
Exercise 1 Building Container Manually
- Open PowerShell in the DcokerVM and run some command to start a container. We are doing this to make use of that container to create some new container by making a few modifications to it. So, run the following command.
docker run -it Microsoft/nanoserver powershell
- We shall create some new file in our container now. This is to customize our container and work with it. Run the following command to create a new file inside the running container. This will create a file in the local disk c with the name sample.
New-Item -ItemType file -Name sample.txt -value sampletext
- Now, press CTRL+PQ to get out of the container which you are working in. Then get the running container's details by executing the following command.
docker ps
- This will now show you the running container details. Now stop this running container by executing the command
docker stop 1d
Replace 1d in the above code with first two letters of your container ID,
- Now we shall capture the stopped image which is modified by creating some text file in it. Here we will be using the base container image as the image which we have stopped. Run the following code to capture the image.
docker commit 1d my-custom-image
- Now check for all the available images in the container. This will show you all the base containers which you have pulled from the docker hub and also the image which you have captured now by creating a new text file inside it.
docker images
- To get the details like when the image has been created, run the following command.
docker history my-custom-image
- This is how we create our own containers using some base images. This one is a manual method of container creation.
Exercise 2 Creation of Dockerfile in container host Automated method of container creation
Now we shall create the Dockerfile in the container host that can be used to create containers in an automated fashion. This will work with a few sets of commands that will be saved in a file in the directory of container host.
- Let us create a directory where we can have our docker file. In this example, we shall work with the docker file based on web service. Hence, we shall create a directory called web in the container and upload our docker file in there. Run the commands,
mkdir web
- Now change into the created directory by running the below command.
Cd
- Again, create new file in that directory. But this time the file is not going to be given sny extension or data.
New-Item -ItemType file -Name Dockerfile
- Now, open the file that you have created in the C drive. It will be in the folder path C:\Users\CWTUSER\web. Path might differ for you if you have created in different location. Open the Dockerfile file with the notepad and paste the following commands inside it.
- # Sample Dockerfile to build a Windows Web Server
- # Indicates that the windowsservercore image will be used as the base image
- FROM microsoft/windowsservercore
- # Metadata indicating an image maintainer
- MAINTAINER @kishore_1702
- # Uses PowerShell to install the web server role
- RUN PowerShell.exe -Command Install-WindowsFeature Web-Server
- # Copies an HTML file to the web server root
- COPY ./websrc c:/inetpub/wwwroot
- # Sets a command or process that will run each time a container is run from the new image
- CMD [ "powershell" ]
- Now, we have successfully created a docker file in the container host machine with some set of codes in the docker file. When we deploy the following docker file, container will get created in an automated manner. Don’t forget to save the docker file.