Linux · 1月 23, 2023 0

How to Create Docker Image with Dockerfile

内容纲要

A Dockerfile is a script on how to build a Docker image.These instructions are, in fact, a group of commands executed automatically in the Docker environment.

How to Create a Dockerfile

The first thing you need to do is to create a directory.

mkdir MyDockerImages
cd MyDockerImages
touch Dockerfile

3.Open the file with a text editor.

nano Dockerfile

4.After that, add the following content:

FROM ubuntu

MAINTAINER sofija

RUN apt-get update

CMD ["echo", "Hello World"]

example of dockerfile used to build simple docker image

FROM – Defines the base of the image you create.

RUN : instructions to execute a command while building an image on top of it.You can have more than one instruction in a Dockerfile.

CMD – There can be only one CMD instruction .Its purpose is to provide defaults for an executing container.

5.Save and exit.

Build a Docker Image with Dockerfile

The basic syntax used to build an image with a Dockerfile is:

docker build [OPTIONS] PATH | URL | -

To build a docker image, you would use:

docker build [location of your dockerfile]

If you are already in the directory where the Dockerfile is located.

docker build .

By adding the -t flag, you can tag the new image with a name.

docker build -t my_first_image .

Once the image is successfully built, you can verify whether it is on the list.

docker images

Create a New Container

Launch a new Docker container based on the image you created.

docker run --name test my_first_image

example of launching a new container based on the docker image

%d 博主赞过: