Docker Images: A Quick Guide
About docker images
2 min read
Docker images are pre-built software packages that include all of the necessary files and dependencies to run a specific application or service. These images are stored in a registry, such as Docker Hub, and can be easily downloaded and used to create new containers. They are used to package and distribute software in a consistent and portable way, making it easy to deploy and run applications in different environments.
How to create Docker images
We can create an image using a
Dockerfile is a script that contains instructions on how to build your image.
Here are the basic steps to create a Docker image:
Create a new folder and navigate to it.
Create a new file called
Dockerfileand open it in a text editor.
FROMinstruction to specify the base image that your image will be built on.
RUNinstruction to execute commands that will be run during the image build process.
COPYinstruction to copy files or folders from your host machine to the image.
CMDinstruction to specify the command that will be run when a container is created from the image.
Open a terminal window and navigate to the folder where the
Run the following command to build the image:
docker build -t my-image .
The -t flag is used to give the image a tag (name), in this case "my-image". The . at the end specifies the current directory as the build context.
Once the image is built, you can run it using the following command:
docker run -p 80:80 my-image
This command runs the image and maps port 80 on the host to port 80 in the container.
Here is an example Dockerfile that creates an image with the latest version of Ubuntu and installs the Apache web server:
FROM ubuntu:latest RUN apt-get update && apt-get install -y apache2 CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
To build the image, open a terminal window and navigate to the directory where the Dockerfile is located. Then run the following command:
docker build -t ubuntu-local .
Next, we will build a docker image for node.js application. Follow for more.