About Dockerfile

If you are approaching Linux, especially Docker, you will find this article useful. In this article, SE Nguyen Ngoc Duc from NAL gives an overview of Dockerfile.

If you are approaching Linux, especially Docker, you will find this article useful. In this article, SE Nguyen Ngoc Duc from NAL gives an overview of Dockerfile.

A few basic commands commonly used in Docker

Before we start learning about Dockerfile, let's take a look at some common commands used in Docker.

docker pull image_name: Get an image from a Docker hub

docker run -it image_name /bin/bash: Run an image to create a new container

docker images: Lists the available images

docker rmi {image_id/name}: Delete an image

docker ps: Lists the running containers

docker ps -a: Lists containers that are disabled

docker rm -f {container_id/name}: Delete a container

docker start {new_container_name}: Start a container

docker exec -it {new_container_name} /bin/bash: Access the running container

What is a Dockerfile?

Simply put, a Dockerfile is a text file that contains a series of commands to help you build a docker image.

From the Dockerfile, you can see where the image was created from.

Next, we will show you how to create and run a Dockerfile.

. /demo to place the demo source code, which can be tested with cd. It will contain the following components

Create a Dockerfile

Dockerfile: Used to create an image to run the container.

App: The source code of the application to be run; after creating the Dockerfile, you can use the editor to actually program it, and all changes will be immediately updated in Docker.

Start.sh: Contains the command that will be executed when the container is enabled (like the example used to turn on the nginx server)

Now, let's start writing the Dockerfile

FROM ubuntu:18.04

MAINTAINER DucNN

RUN apt-get update

RUN apt-get install -y nginx

WORKDIR /opt

# ADD start.tar.gz /opt
# ADD start.sh /opt
COPY start.sh /opt

RUN chmod a+x /opt/start.sh

ENTRYPOINT ["/opt/start.sh"]

EXPOSE 80

FROM: Reports who the parent of this image is. In other words, it reports where it came from. when Docker reads this command, it will check if the image ubuntu:18.04 already exists in the client. If it does not exist, Docker will automatically pull this image. ubuntu is the image name and 18.04 is the tag. It is literally the version.

Maintainer: Reports the name of the author of the Dockerfile to be created.

Run: Runs a specific command while the image is being created.

CMD: Runs a command while the container is enabled. There can be only one CMD statement in each Dockerfile. If there are multiple CMD statements, only the last statement will be executed.

ENTRYPOINT: Similar to CMD, but allows multiple commands to be executed while the container is running. You can write many of these commands into a single bash script file and then execute that file.

WORKDIR: Defines the directory for the CMD

EXPOSE: The container will listen on the port specified at runtime

Copy: Copies a folder from the local machine or host machine to the image.

Add: Similar to COPY, but also supports two other properties. First, you can copy using a URL. Next, you can extract a tar file to a specified directory.

VOLUME: Mounts a directory from the client to the container.

The above are the basic commands to build a Dockerfile.

How to use Dockerfile

Create an image from a Dockerfile.

docker build -t

Create a container from the image.

 docker run -v : -p : -it /bin/bash

-v: Displays the mouse volume. The data in the directory can be accessed from the container directory.

-p: The network port of the machine that connects to the container's network port.

-t: Run the container and open a terminal with / bin / bash

Demo run

docker build -t demo-dockerfile .
docker run -p 9000:80 -it demo-dockerfile /bin/bash

http://localhost:9000/ to see the result.

If you want to run the code in the app directory, run the following command: .

docker run -v /CompetencyMatrix/competency-matrix/developer-roadmap/docker/demo/docker-dockerfile/app:/var/www/html -p 9000:80 -it demo-dockerfile /bin/bash

You can edit the code directly in the app directory, or you can go tohttp://localhost:9000/ to see the changes.

Original article:https://media.nal.vn/dockerfile

Comments are closed.