Docker Base Image
- Docker containers are created using images.
- An image can be basic, with nothing but the operating system core or it can consist of a sophisticated pre-built application stack ready for launch.
- A base image is the one without any further layers below. This is the most basic form of a Docker image that has just the operating system.
- The Docker Hub has base images for all of the major Linux distros. But you are also free to create your own base image.
- You can use the scratch repository in the Docker hub. Use “FROM scratch” in Dockerfile. Alternatively the tar command can be used.
Dockerfile
A Dockerfile is a script composed of various commands (instructions) and arguments listed one after another. This will automatically perform actions on the base image in order to create a new one.
- A base Docker image doesn’t require many Dockerfile instructions
- If we add more and more layers to the base image, we will need to know more about Dockerfile instructions
How to create Dockerfile ?
1) List the instructions one after another in a plain text file
2) Run “docker build” command in the same directory
3) Image gets created
Dockerfile consists of :
1) comments (starting with #)
2) commands (with arguments) starting with an instruction (in CAPS)
Note : A dockerfile should always start with “FROM” instruction (indicates base image)
Important Dockerfile Instructions
1) ADD – copies files from source (on the host) into the container’s own file system at the set destination.
Example
#ADD src dest
1 |
ADD /source_path /docker_container_dest_path |
2) ENV – This is used for setting one or more environment variables
Example
#ENV variable value
1 |
ENV Answer 1 |
3) FROM – defines the base image to be used for the build process. It can be any image from Docker Hub or the images you have created previously. This should be the first instruction in the Dockerfile.
Example
1 2 |
#FROM image_name FROM ubuntu |
4) RUN – Indicates the central executing directive for Dockerfile. This instruction takes a command as its argument and runs it to create the image. The run command executes in the shell which is usually “/bin/sh -c”. To run more than one command, append “\” and continue on the next line.
Example
1 2 |
#RUN usage RUN apt-get update && apt-get install -y ruby ruby-dev |
5) EXPOSE – associates a specified port to enable networking between the running process inside the container and the outside (e.g. the host).
Example
1 2 |
#EXPOSE port_number EXPOSE 8090 |
6) VOLUME – For enabling access from a container to a directory on the host machine (mounting).
Example
1 2 |
#VOLUME usage VOLUME [“/var/www”, “/var/log/apache2”] |
7) MAINTAINER – This is a non-executing command, for declaring the author of the Dockerfile. This is not mandatory, but recommended.
Example
1 2 |
#MAINTAINER name MAINTAINER Tom <tommcdonald@abc.com> |
8) WORKDIR – Specifies working directory
Example
1 |
WORKDIR /opt/src/app |
9) ENTRYPOINT – Used for configuring an executable container
Example
1 |
ENTRYPOINT [“abcontainer1”] |
How to build and run the Dockerfile
1) Navigate to the folder where the Dockerfile is available and use the “sudo docker build” command. This will create the image.
2) Verify the image using “sudo docker images”
3) Run the image to create a container using “sudo docker run”