Docker Images
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
. A Dockerfile
is a script that contains instructions on how to build your image.
Here is the basic steps to create a Docker image:
- Create a new folder and navigate to it.
- Create a new file called
Dockerfile
and open it in a text editor. - Use the
FROM
instruction to specify the base image that your image will be built on. - Use the
RUN
instruction to execute commands that will be run during the image build process. - Use the
COPY
instruction to copy files or folders from your host machine to the image. - Use the
CMD
instruction to specify the command that will be run when a container is created from the image. - Save the
Dockerfile
. - Open a terminal window and navigate to the folder where the
Dockerfile
is located. - 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.
Example
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.
Top comments (0)