What is docker?
Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, standalone executable packages that contain everything an application needs to run, including code, runtime, system tools, libraries, and settings.
Docker allows developers to package their applications and dependencies in a container, which can then be easily deployed and run on any machine that has the Docker runtime installed. This allows for consistency and reproducibility across development, testing, and production environments.
Docker also provides a centralized hub, called the Docker Hub, where developers can store and share their container images with others. This makes it easy for teams to collaborate and use pre-built images, instead of building everything from scratch.
We have already discussed docker and docker hub in our previous blog
How to run docker images as a container
So in order to build our custom image we need to write a docker file. Docker takes the values from these files to build our docker image.
What is a docker file?
A Dockerfile is a script that contains a set of instructions for building a Docker image. Each instruction in the Dockerfile creates a new layer in the image, and the final image includes all the layers from all of the instructions. This is similar to a bash script we need to tell the docker what actions we need to do while creating our image.
So there are many instructions and arguments which need to be included in the docker file which serves different purposes we need to understand the basics instruction and their use cases. Let’s get started
Basic instructions in the docker file.
- FROM: specifies the base image for the build.
- RUN: runs a command during the build process.
- COPY: copies files from the host machine to the container.
- ENV: sets environment variables in the image.
- EXPOSE: exposes a port or ports for the container.
- CMD: specifies the command that will be run when the container starts.
- ADD: copies files from the host machine to the container and also support downloading files from a remote location.
- ENTRYPOINT: Specifies the default command that will be run when the container starts.
- LABEL: adds metadata to the image.
- USER: sets the default user for the container.
Writing a sample Docker file.
Using the above instructions we can write our first Docker file to install python3 in a ubuntu container and print a Hello world to the console.
RUN apt-get update && apt-get install -y python3
COPY my_script.py /app/
CMD [“python3”, “/app/my_script.py”]
- The first line specifies that the base image for the container should be Ubuntu 20.04.
- The next line runs the command
apt-get updateto update the package lists and then runs
apt-get install -y python3to install Python 3 on the container.
COPYcommand then copies the file
my_script.pyfrom the host machine to the
/app/a directory within the container.
- Finally, the
CMDthe command sets the command that will be run when the container is started, which is
["python3", "/app/my_script.py"]which runs the python script my_script.py using the python3 interpreter.
We need a sample my_script.py file within the directory where the docker file is present.
Build the image and run the container.
Next, you can create the image using the docker build command and run your container using the docker run command.
docker build -t image:latest .
docker run -ti image:latest
That’s super easy to create your docker file and run the container. This is just a sample application we can build our complex multitier applications using a docker file and create our containers.