Start with dockerizing example
Add a Docker file to your project
A docker file
is a structural document that includes all requirements for running the project. We can specify the base image and more arguments to represent a new image just like inheritance. And base on that dockerfile
, docker can build a new container, which means dockerfile inherits and acts as a blueprint for container.
Docker file setup
Docker Hub
is the registry of docker images, we can ship our images to there and pull images down our machine just like github
.
- There's some other alternatives like
Azure Container Registry
andGithub Container Registry
and so on.
To specify the image we need, go to a registry and check different distributions. node
for example.
# set image distribution
FROM node:alpine
# copy source files to dest dir
COPY . /app
# set working dir for cmd
WORKDIR /app
# command to execute when docker image runs
CMD node app.js
2
3
4
5
6
7
8
Docker build
After setting dockerfile, run docker build -t <tag> <where_dockerfile_locate>
in terminal where dockerfile
locate. The generated image is not stored in current folder, we don't have to care about where it was stored. Use docker images
or docker image ls
to check existing images on your machine.
REPOSITORY TAG IMAGE ID CREATED SIZE
js-dockerize latest fcc3e847d662 About a minute ago 187MB
2
Run docker image
Run docker image using docker run <image-name>
! The CMD
will auto run.
Pull docker image
docker pull <image_name>
do the work. And then you can go with your pulled image. docker run <image-name>
can be a shortcut, if image does not exists in local, docker automatically pull from registry.