Skip to main content

Command Palette

Search for a command to run...

Dockerfile

Updated
•3 min read•View as Markdown

Dockerfile is a file that uses instructions to create container images. Each instruction has a command and is followed by arguments. Top Commands to look for in Dockerfile: WORKDIR /path/to/workdir sets the workdir for the other instructions. If the WORKDIR doesn't exist, it will be created even if it's not used in Dockerfile. The file must start with a FROM instruction. FROM points the base image used to create the container image Any line that starts with # is treated as a comment, unless infront of a parser directive. Leading whitespaces before instructions and commands are ignored and discouraged.

ADD/COPY used to supply resources/files to your image ADD source can be only host COPY source can either be host or remote url CMD instruction shows how to start the application RUN instruction runs the arguments as mentioned ..white spaces are preserved here along with the default back slashes. ENV instruction maps environment key to value . This can be changed even inline too. EXPOSE instruction that specifies container listens on specific n/w ports at runtime. Default = TCP Note: Parser directives are written at the top of Dockerfile. They are not case-sensitive, but written in lower-case, always followed by blank line. Line continuation characters are not supported. Once the dockerfile is written, docker image is build by docker build -t . . -> if Dockerfile is in current folder, else provide the path of Dockerfile docker images will list the images that are created, some times it even lists the reference image from the FROM command To remove images, docker rmi To create a container out of a image, run docker create - name docker ps will only list containers that are running instead use docker ps -a* to list all containers. The output lists below columns:

  • CONTAINER ID
  • IMAGE
  • COMMAND
  • CREATED
  • STATUS
  • PORTS
  • NAMES start container using docker start stop container using docker stop remove container using docker rm Create and start container directly using docker run - name add '-d' to created a detached container docker run -d - name see logs of container docker logs follow container logs docker logs -f (use CTRL+C to stop following) use rm with -f to carefully remove the running container docker rm -f docker run -d - name -p 8080:8080 to run commands inside a running container, use docker exec -it /bin/bash use cat /etc/os-release to check the os version. press exit to return To push an image to dockerhub, you need to create an account and tag your image docker tag / this could be done directly during build docker build -t / . docker images will list both and / with the same imageiD login to dockerhub using docker login and push using docker push /