In this guide we’ll learn about the Dockerfile and how to create Docker Images with Dockerfiles
docker build
command processes this file generating a Docker Image in your Local Image Cache, which you can then start-up using the docker run
command, or push to a permanent Image Repository.Creating a ***Dockerfile ***is as easy as creating a new file named “Dockerfile” with your text editor of choice and defining some instructions. The name of the file
does not really matter. Dockerfile is the default name but you can use any filename that you want (and even have multiple dockerfiles in the same folder)
#
# Each instruction in this file generates a new layer that gets pushed to your local image cache
#
#
# Lines preceeded by # are regarded as comments and ignored
#
#
# The line below states we will base our new image on the Latest Official Ubuntu
FROM ubuntu:latest
#
# Identify the maintainer of an image
LABEL maintainer="myname@somecompany.com"
#
# Update the image to the latest packages
RUN apt-get update && apt-get upgrade -y
#
# Install NGINX to test.
RUN apt-get install nginx -y
#
# Expose port 80
EXPOSE 80
#
# Last is the actual command to start up NGINX within our Container
CMD ["nginx", "-g", "daemon off;"]
There’s a free service that lets you quickly spin up Docker instances through a web interface called: “Play With Docker”
4. Next create/edit the Dockerfile. Run “vi Dockerfile”, press “i” to switch to “Insert Mode”, copy/paste the contents of our Dockerfile, press “Esc” to exit “Insert Mode”, and save+exit by typing “:x”
5. Build the new image using the command docker build <path>
. Path refers to the directory containing the Dockerfile.
6. At the end of the process you should see the message “Successfully built ”
7. Start the new image and test connectivity to NGINX. Run the command docker run -p 80:80 <image ID>
. The option -p 80:80
exposes the Container port 80 as the Host port 80 to the world
8. As a result a port 80 link should have become active next to the IP. Click on it to access your NGINX service
In the previous section we have seen an example Docker image for nginx. But what if you want to package your own application in a Docker image?
In this case you can create a **Dockerfile **in the same folder as your source code. Then put instructions in the *dockerfile *that mirror what you do locally on your workstation to compile/package the code.
The first step should be to find a public docker images that uses your programming language. Some examples are:
Once you find a proper base image you can use it to package your own application. Here is an example for Python
FROM python:3.6.4-alpine3.6
ENV FLASK_APP=minitwit
COPY . /app
WORKDIR /app
RUN pip install --editable .
RUN flask initdb
EXPOSE 5000
CMD [ "flask", "run", "--host=0.0.0.0" ]
Here is another example for Node.js
FROM node:10
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
Once you become familiar with building docker images you also need to pay attention to two more topics
Here is an example with a Node application that is using multi-stage builds:
FROM node:8.16 as build-deps
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn
COPY . ./
RUN yarn build
FROM nginx:1.12-alpine
COPY --from=build-deps /usr/src/app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Go have fun building your own Images!
#docker #devops #image